File: compat2and3.py

package info (click to toggle)
streamtuner2 2.2.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,432 kB
  • sloc: python: 8,976; makefile: 91; php: 51; sh: 7; perl: 3
file content (76 lines) | stat: -rw-r--r-- 1,665 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# encoding: UTF-8
# api: python 
# type: functions
# title: Python2 and Python3 compatibility
# version: 0.2
#
# Renames some Python3 modules into their Py2 equivalent.
# Slim local alternative to `six` module.


import sys


# Python 2
if sys.version_info < (3,0):

    # version tags
    PY2 = 1
    PY3 = 0

    # basic functions
    xrange = xrange
    range = xrange
    unicode = unicode

    # urllib modules
    import urllib
    import urllib2
    from urllib import quote_plus as urlencode, unquote as urldecode, quote as urlquote
    import urlparse
    import cookielib
    
    # filesys
    from StringIO import StringIO
    from gzip import GzipFile
    def gzip_decode(bytes):
        return GzipFile(fileobj=StringIO(bytes)).read()
        # return zlib.decompress(bytes, 16 + zlib.MAX_WBITS)    # not fully compatible


# Python 3
else:

    # version tags
    PY2 = 0
    PY3 = 1

    # basic functions
    xrange = range
    unicode = str
    unichr = chr

    # urllib modules
    import urllib.request as urllib
    import urllib.request as urllib2
    from urllib.parse import quote_plus as urlencode, unquote as urldecode, quote as urlquote
    import urllib.parse as urlparse
    from http import cookiejar as cookielib
    
    # filesys
    from io import StringIO
    from gzip import decompress as gzip_decode


# Both

# find_executable() is only needed by channels/configwin
try:
    from distutils.spawn import find_executable
except:
    import os
    def find_executable(bin):
        exists = [os.path.exists(dir+"/"+bin) for dir in os.environ.get("PATH").split(":")+["/"]]
        return exists[0] if len(exists) else None