diff options
Diffstat (limited to 'src/lib/Bcfg2/Bcfg2Py3k.py')
-rw-r--r-- | src/lib/Bcfg2/Bcfg2Py3k.py | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/src/lib/Bcfg2/Bcfg2Py3k.py b/src/lib/Bcfg2/Bcfg2Py3k.py index 6af8b3e5c..be5175e62 100644 --- a/src/lib/Bcfg2/Bcfg2Py3k.py +++ b/src/lib/Bcfg2/Bcfg2Py3k.py @@ -14,6 +14,7 @@ try: from urllib2 import install_opener from urllib2 import urlopen from urllib2 import HTTPError + from urllib2 import URLError except ImportError: from urllib.parse import urljoin, urlparse from urllib.request import HTTPBasicAuthHandler @@ -22,6 +23,7 @@ except ImportError: from urllib.request import install_opener from urllib.request import urlopen from urllib.error import HTTPError + from urllib.error import URLError try: from cStringIO import StringIO @@ -62,6 +64,12 @@ try: except ImportError: import http.client as httplib +# py3k compatibility +if sys.hexversion >= 0x03000000: + unicode = str +else: + unicode = unicode + # print to file compatibility def u_str(string, encoding=None): if sys.hexversion >= 0x03000000: @@ -75,7 +83,49 @@ def u_str(string, encoding=None): else: return unicode(string) -if sys.hexversion >= 0x03000000: - from io import FileIO as file -else: - file = file +try: + unicode = unicode +except: + unicode = str + +# base64 compat +from base64 import b64encode as _b64encode, b64decode as _b64decode +b64encode = lambda s: _b64encode(s.encode('ascii')).decode('ascii') +b64decode = lambda s: _b64decode(s.encode('ascii')).decode('ascii') + +try: + input = raw_input +except: + input = input + +try: + reduce = reduce +except NameError: + from functools import reduce + +try: + from collections import MutableMapping +except ImportError: + from UserDict import DictMixin as MutableMapping + + +# in py3k __cmp__ is no longer magical, so we define a mixin that can +# be used to define the rich comparison operators from __cmp__ +class CmpMixin(object): + def __lt__(self, other): + return self.__cmp__(other) < 0 + + def __gt__(self, other): + return self.__cmp__(other) > 0 + + def __eq__(self, other): + return self.__cmp__(other) == 0 + + def __ne__(self, other): + return not self.__eq__(other) + + def __ge__(self, other): + return self.__gt__(other) or self.__eq__(other) + + def __le__(self, other): + return self.__lt__(other) or self.__eq__(other) |