Index: compat2and3.py ================================================================== --- compat2and3.py +++ compat2and3.py @@ -1,10 +1,10 @@ # encoding: UTF-8 # api: python # type: functions # title: Python2 and Python3 compatibility -# version: 0.1 +# version: 0.2 # # Renames some Python3 modules into their Py2 equivalent. # Slim local alternative to `six` module. @@ -30,10 +30,14 @@ 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: @@ -52,7 +56,19 @@ 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: + 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 Index: config.py ================================================================== --- config.py +++ config.py @@ -24,23 +24,15 @@ import sys import json import gzip import platform import re +from compat2and3 import gzip_decode, find_executable import zlib import zipfile import inspect import pkgutil - -# find_executable() is only needed by channels/configwin -try: - from distutils.spawn import find_executable -except: - 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 - # export symbols __all__ = ["conf", "__print__", "dbg", "plugin_meta", "module_list", "get_data", "find_executable"] @@ -260,15 +252,15 @@ # Retrieve content from install path or pyzip archive (alias for pkgutil.get_data) # -def get_data(fn, decode=False, z=False, file_base="config"): +def get_data(fn, decode=False, gz=False, file_base="config"): try: bin = pkgutil.get_data(file_base, fn) - if z: - bin = zlib.decompress(bin) + if gz: + bin = gzip_decode(bin) if decode: return bin.decode("utf-8") else: return str(bin) except: