summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Compat.py
blob: 57f96274f33c93e53cd145ec3d0ffc783f545da8 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
""" Compatibility imports, mostly for Py3k support, but also for
Python 2.4 and such-like """

###################################################
#                                                 #
#   IF YOU ADD SOMETHING TO THIS FILE, YOU MUST   #
#   DOCUMENT IT IN docs/development/compat.txt    #
#                                                 #
###################################################

import sys

# pylint: disable=E0611,W0611,W0622,C0103

try:
    from email.Utils import formatdate
except ImportError:
    from email.utils import formatdate

# urllib imports
try:
    from urllib import quote_plus
    from urlparse import urljoin, urlparse
    from urllib2 import HTTPBasicAuthHandler, \
        HTTPPasswordMgrWithDefaultRealm, build_opener, install_opener, \
        urlopen, HTTPError, URLError
except ImportError:
    from urllib.parse import urljoin, urlparse, quote_plus
    from urllib.request import HTTPBasicAuthHandler, \
        HTTPPasswordMgrWithDefaultRealm, build_opener, install_opener, urlopen
    from urllib.error import HTTPError, URLError

try:
    from cStringIO import StringIO
except ImportError:
    from io import StringIO

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

try:
    import cPickle
except ImportError:
    import pickle as cPickle

try:
    from Queue import Queue, Empty, Full
except ImportError:
    from queue import Queue, Empty, Full

# xmlrpc imports
try:
    import xmlrpclib, SimpleXMLRPCServer
except ImportError:
    import xmlrpc.client as xmlrpclib
    import xmlrpc.server as SimpleXMLRPCServer

# socketserver import
try:
    import SocketServer
except ImportError:
    import socketserver as SocketServer

# httplib imports
try:
    import httplib
except ImportError:
    import http.client as httplib

try:
    unicode = unicode
except NameError:
    unicode = str

def u_str(string, encoding=None):
    """ print to file compatibility """
    if sys.hexversion >= 0x03000000:
        if encoding is not None:
            return string.encode(encoding)
        else:
            return string
    else:
        if encoding is not None:
            return unicode(string, encoding)
        else:
            return unicode(string)

# base64 compat
if sys.hexversion >= 0x03000000:
    from base64 import b64encode as _b64encode, b64decode as _b64decode
    b64encode = lambda s: _b64encode(s.encode('UTF-8')).decode('UTF-8')
    b64decode = lambda s: _b64decode(s.encode('UTF-8')).decode('UTF-8')
else:
    from base64 import b64encode, b64decode

try:
    input = raw_input
except NameError:
    input = input

try:
    reduce = reduce
except NameError:
    from functools import reduce

try:
    from collections import MutableMapping
except ImportError:
    from UserDict import DictMixin as MutableMapping


class CmpMixin(object):
    """ In Py3K, :meth:`object.__cmp__` is no longer magical, so this
    mixin can be used to define the rich comparison operators from
    ``__cmp__`` -- i.e., it makes ``__cmp__`` magical again. """

    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)

try:
    from pkgutil import walk_packages
except ImportError:
    try:
        from pkgutil import iter_modules
        # iter_modules was added in python 2.5; use it to get an exact
        # re-implementation of walk_packages if possible

        def walk_packages(path=None, prefix='', onerror=None):
            """ Implementation of walk_packages for python 2.5 """
            def seen(path, seenpaths={}):  # pylint: disable=W0102
                """ detect if a path has been 'seen' (i.e., considered
                for inclusion in the generator).  tracks what has been
                seen through the magic of python default arguments """
                if path in seenpaths:
                    return True
                seenpaths[path] = True

            for importer, name, ispkg in iter_modules(path, prefix):
                yield importer, name, ispkg

                if ispkg:
                    try:
                        __import__(name)
                    except ImportError:
                        if onerror is not None:
                            onerror(name)
                    except Exception:
                        if onerror is not None:
                            onerror(name)
                        else:
                            raise
                    else:
                        path = getattr(sys.modules[name], '__path__', [])

                        # don't traverse path items we've seen before
                        path = [p for p in path if not seen(p)]

                        for item in walk_packages(path, name + '.', onerror):
                            yield item
    except ImportError:
        import os

        def walk_packages(path=None, prefix='', onerror=None):
            """ Imperfect, incomplete implementation of
            :func:`pkgutil.walk_packages` for python 2.4. Differences:

            * Requires a full path, not a path relative to something
              in sys.path.  Anywhere we care about that shouldn't be
              an issue.
            * The first element of each tuple is None instead of an
              importer object.
            """
            if path is None:
                path = sys.path
            for mpath in path:
                for fname in os.listdir(mpath):
                    fpath = os.path.join(mpath, fname)
                    if (os.path.isfile(fpath) and fname.endswith(".py") and
                        fname != '__init__.py'):
                        yield None, prefix + fname[:-3], False
                    elif os.path.isdir(fpath):
                        mname = prefix + fname
                        if os.path.exists(os.path.join(fpath, "__init__.py")):
                            yield None, mname, True
                        try:
                            __import__(mname)
                        except ImportError:
                            if onerror is not None:
                                onerror(mname)
                        except Exception:
                            if onerror is not None:
                                onerror(mname)
                            else:
                                raise
                        else:
                            for item in walk_packages([fpath],
                                                      prefix=mname + '.',
                                                      onerror=onerror):
                                yield item


try:
    all = all
    any = any
except NameError:
    def all(iterable):
        """ implementation of builtin all() for python 2.4 """
        for element in iterable:
            if not element:
                return False
        return True

    def any(iterable):
        """ implementation of builtin any() for python 2.4 """
        for element in iterable:
            if element:
                return True
        return False

try:
    from hashlib import md5
except ImportError:
    from md5 import md5


try:
    from functools import wraps
except ImportError:
    def wraps(wrapped):  # pylint: disable=W0613
        """ implementation of functools.wraps() for python 2.4 """
        return lambda f: f


def oct_mode(mode):
    """ Convert a decimal number describing a POSIX permissions mode
    to a string giving the octal mode.  In Python 2, this is a synonym
    for :func:`oct`, but in Python 3 the octal format has changed to
    ``0o000``, which cannot be used as an octal permissions mode, so
    we need to strip the 'o' from the output.  I.e., this function
    acts like the Python 2 :func:`oct` regardless of what version of
    Python is in use.

    :param mode: The decimal mode to convert to octal
    :type mode: int
    :returns: string """
    return oct(mode).replace('o', '')


try:
    long = long
except NameError:
    # longs are just ints in py3k
    long = int