diff options
author | grabner <pjg.github@ubergrabner.net> | 2014-04-15 22:34:11 -0400 |
---|---|---|
committer | grabner <pjg.github@ubergrabner.net> | 2014-04-15 22:34:11 -0400 |
commit | dcc485ac936cca67492ff01bf25e7a98dc473eda (patch) | |
tree | 4a2d36bbd92e0ef40c82d5d2457043ca8db74fa6 | |
parent | 6852f2cefc64b4a853ee64ec7159cbd857c8ed41 (diff) | |
download | iniherit-dcc485ac936cca67492ff01bf25e7a98dc473eda.tar.gz |
added python 3 compatibility
-rw-r--r-- | iniherit/cli.py | 10 | ||||
-rw-r--r-- | iniherit/mixin.py | 11 | ||||
-rw-r--r-- | iniherit/parser.py | 18 | ||||
-rw-r--r-- | iniherit/test.py | 11 | ||||
-rwxr-xr-x | setup.py | 2 |
5 files changed, 27 insertions, 25 deletions
diff --git a/iniherit/cli.py b/iniherit/cli.py index 14d3abc..ba19aa8 100644 --- a/iniherit/cli.py +++ b/iniherit/cli.py @@ -7,6 +7,8 @@ #------------------------------------------------------------------------------ import sys, os, logging, time, argparse, gettext +import six + import iniherit, iniherit.parser log = logging.getLogger(__name__) @@ -17,12 +19,8 @@ log = logging.getLogger(__name__) # data... it should probably buffer it! #------------------------------------------------------------------------------ -if sys.version_info[0] == 3: - def isstr(obj): - return isinstance(obj, str) -else: - def isstr(obj): - return isinstance(obj, basestring) +def isstr(obj): + return isinstance(obj, six.string_types) #------------------------------------------------------------------------------ def _(message, *args, **kw): diff --git a/iniherit/mixin.py b/iniherit/mixin.py index c58fa96..d8ee0f9 100644 --- a/iniherit/mixin.py +++ b/iniherit/mixin.py @@ -6,14 +6,15 @@ # copy: (C) Copyright 2013 Cadit Health Inc., All Rights Reserved. #------------------------------------------------------------------------------ -try: - import ConfigParser as CP -except ImportError: - import configparser as CP +import six +from six.moves import configparser as CP # TODO: what is the PY3 version of 'new'?... import new + from iniherit.parser import IniheritMixin +#------------------------------------------------------------------------------ + attrs = [attr for attr in dir(IniheritMixin) if not attr.startswith('__')] #------------------------------------------------------------------------------ @@ -31,7 +32,7 @@ def install_globally(): setattr(CP.RawConfigParser, '_iniherit_' + attr, getattr(CP.RawConfigParser, attr)) meth = getattr(IniheritMixin, attr) - if callable(meth): + if six.callable(meth): meth = new.instancemethod(meth.im_func, None, CP.RawConfigParser) setattr(CP.RawConfigParser, attr, meth) diff --git a/iniherit/parser.py b/iniherit/parser.py index 7a8a3b8..d05b7fc 100644 --- a/iniherit/parser.py +++ b/iniherit/parser.py @@ -6,11 +6,11 @@ # copy: (C) Copyright 2013 Cadit Health Inc., All Rights Reserved. #------------------------------------------------------------------------------ -import io, os.path, urllib -try: - import ConfigParser as CP -except ImportError: - import configparser as CP +import io +import os.path +import six +from six.moves import configparser as CP +from six.moves import urllib # TODO: PY3 added a `ConfigParser.read_dict` that should probably # be overridden as well... @@ -52,7 +52,7 @@ class IniheritMixin(object): #---------------------------------------------------------------------------- def read(self, filenames, encoding=None): - if isinstance(filenames, basestring): + if isinstance(filenames, six.string_types): filenames = [filenames] read_ok = [] for filename in filenames: @@ -100,7 +100,7 @@ class IniheritMixin(object): optional = curname.startswith('?') if optional: curname = curname[1:] - curname = os.path.join(dirname, urllib.unquote(curname)) + curname = os.path.join(dirname, urllib.parse.unquote(curname)) try: curfp = self._load(curname, encoding=encoding) except IOError: @@ -123,8 +123,8 @@ class IniheritMixin(object): fromsect = section if '[' in curname and curname.endswith(']'): curname, fromsect = curname.split('[', 1) - fromsect = urllib.unquote(fromsect[:-1]) - curname = os.path.join(dirname, urllib.unquote(curname)) + fromsect = urllib.parse.unquote(fromsect[:-1]) + curname = os.path.join(dirname, urllib.parse.unquote(curname)) try: curfp = self._load(curname, encoding=encoding) except IOError: diff --git a/iniherit/test.py b/iniherit/test.py index e595036..dc66e96 100644 --- a/iniherit/test.py +++ b/iniherit/test.py @@ -6,7 +6,10 @@ # copy: (C) Copyright 2013 Cadit Health Inc., All Rights Reserved. #------------------------------------------------------------------------------ -import unittest, io +import unittest +import io +import six + from iniherit.parser import Loader, ConfigParser, SafeConfigParser #------------------------------------------------------------------------------ @@ -17,7 +20,7 @@ class ByteLoader(Loader): def load(self, name, encoding=None): if name not in self.items: raise IOError(2, 'No such file or directory', name) - ret = io.BytesIO(self.items[name]) + ret = six.StringIO(self.items[name]) ret.name = name return ret @@ -132,7 +135,7 @@ kw6 = extend-kw6 #---------------------------------------------------------------------------- def test_iniherit_invalidInterpolationValues(self): files = [ - ('config.ini', '[logger]\ntimefmt=%H:%M:%S\n'), + ('config.ini', '[logger]\ntimefmt=%H:%M:%S\n'), ] parser = SafeConfigParser(loader=ByteLoader(dict(files))) parser.read('config.ini') @@ -145,7 +148,7 @@ kw6 = extend-kw6 from iniherit.mixin import install_globally, uninstall_globally files = [ - ('base.ini', '[DEFAULT]\nkw = base-kw\n'), + ('base.ini', '[DEFAULT]\nkw = base-kw\n'), ('config.ini', '[DEFAULT]\n%inherit = base.ini\n'), ] loader = ByteLoader(dict(files)) @@ -25,7 +25,7 @@ test_requires = [ ] requires = [ - 'distribute >= 0.6.24', + 'distribute >= 0.7.3', 'six >= 1.6.1', ] |