Source code

Download source archive ↓

src/xlrd/timemachine.py

1##
2# <p>Copyright (c) 2006-2012 Stephen John Machin, Lingfo Pty Ltd</p>
3# <p>This module is part of the xlrd package, which is released under a BSD-style licence.</p>
4##
5
6# timemachine.py -- adaptation for single codebase.
7# Currently supported: 2.6 to 2.7, 3.2+
8# usage: from timemachine import *
9
10from __future__ import print_function
11
12import sys
13
14python_version = sys.version_info[:2] # e.g. version 2.6 -> (2, 6)
15
16if python_version >= (3, 0):
17    # Python 3
18    BYTES_LITERAL = lambda x: x.encode('latin1')
19    UNICODE_LITERAL = lambda x: x
20    BYTES_ORD = lambda byte: byte
21    from io import BytesIO as BYTES_IO
22    def fprintf(f, fmt, *vargs):
23        fmt = fmt.replace("%r", "%a")
24        if fmt.endswith('\n'):
25            print(fmt[:-1] % vargs, file=f)
26        else:
27            print(fmt % vargs, end=' ', file=f)
28    EXCEL_TEXT_TYPES = (str, bytes, bytearray) # xlwt: isinstance(obj, EXCEL_TEXT_TYPES)
29    REPR = ascii
30    xrange = range
31    unicode = lambda b, enc: b.decode(enc)
32    ensure_unicode = lambda s: s
33    unichr = chr
34else:
35    # Python 2
36    BYTES_LITERAL = lambda x: x
37    UNICODE_LITERAL = lambda x: x.decode('latin1')
38    BYTES_ORD = ord
39    from cStringIO import StringIO as BYTES_IO
40    def fprintf(f, fmt, *vargs):
41        if fmt.endswith('\n'):
42            print(fmt[:-1] % vargs, file=f)
43        else:
44            print(fmt % vargs, end=' ', file=f)
45    try:
46        EXCEL_TEXT_TYPES = basestring # xlwt: isinstance(obj, EXCEL_TEXT_TYPES)
47    except NameError:
48        EXCEL_TEXT_TYPES = (str, unicode)
49    REPR = repr
50    xrange = xrange
51    # following used only to overcome 2.x ElementTree gimmick which
52    # returns text as `str` if it's ascii, otherwise `unicode`
53    ensure_unicode = unicode # used only in xlsx.py