summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Compat.py
blob: 99ae0f677be8156d7652ddd2c574408106071343 (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
""" 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=E0601,E0602,E0611,W0611,W0622,C0103

from email.utils import formatdate

# urllib imports
from urllib.parse import quote_plus
from urllib.request import urlretrieve
from urllib.parse import urljoin, urlparse
from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, build_opener, install_opener, urlopen
from urllib.error import HTTPError, URLError

from io import StringIO

import configparser as ConfigParser

import pickle as cPickle

from queue import Queue, Empty, Full

# xmlrpc imports
import xmlrpc.client as xmlrpclib
import xmlrpc.server as SimpleXMLRPCServer

# socketserver import
import socketserver as SocketServer

# httplib imports
import http.client as httplib

try:
    str = str
except NameError:
    str = str


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


def ensure_binary(string, encoding='utf-8'):
    if type(string) == str:
        return string.encode(encoding)
    return string


from functools import wraps

# base64 compat
from base64 import b64encode as _b64encode, b64decode as _b64decode


@wraps(_b64encode)
def b64encode(val, **kwargs):  # pylint: disable=C0111
    try:
        return _b64encode(val, **kwargs)
    except TypeError:
        return _b64encode(val.encode('UTF-8'), **kwargs).decode('UTF-8')


@wraps(_b64decode)
def b64decode(val, **kwargs):  # pylint: disable=C0111
    return _b64decode(val.encode('UTF-8'), **kwargs).decode('UTF-8')


input = input


from functools import reduce


from collections.abc import 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)


from pkgutil import walk_packages


all = all
any = any


from hashlib import md5


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', '')


long = int


def cmp(a, b):
    """ Py3k implementation of cmp() """
    return (a > b) - (a < b)


from ast import literal_eval