1 # Samba common functions
3 # Copyright (C) Matthieu Patou <mat@matws.net>
4 # Copyright (C) Lumir Balhar <lbalhar@redhat.com> 2017
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 Replacement for built-in function cmp that was removed in Python 3
25 Compare the two objects x and y and return an integer according to
26 the outcome. The return value is negative if x < y, zero if x == y
27 and strictly positive if x > y.
30 return (x
> y
) - (x
< y
)
33 def confirm(msg
, forced
=False, allow_all
=False):
34 """confirm an action with the user
36 :param msg: A string to print to the user
37 :param forced: Are the answer forced
40 print("%s [YES]" % msg
)
54 mapping
['ALL'] = 'ALL'
55 mapping
['NONE'] = 'NONE'
56 prompt
= '[y/N/all/none]'
59 v
= input(msg
+ ' %s ' % prompt
)
63 print("Unknown response '%s'" % v
)
66 def normalise_int32(ivalue
):
67 """normalise a ldap integer to signed 32 bit"""
68 if int(ivalue
) & 0x80000000 and int(ivalue
) > 0:
69 return str(int(ivalue
) - 0x100000000)
73 # Sometimes in PY3 we have variables whose content can be 'bytes' or
74 # 'str' and we can't be sure which. Generally this is because the
75 # code variable can be initialised (or reassigned) a value from different
76 # api(s) or functions depending on complex conditions or logic. Or another
77 # common case is in PY2 the variable is 'type <str>' and in PY3 it is
78 # 'class <str>' and the function to use e.g. b64encode requires 'bytes'
79 # in PY3. In such cases it would be nice to avoid excessive testing in
80 # the client code. Calling such a helper function should be avoided
81 # if possible but sometimes this just isn't possible.
82 # If a 'str' object is passed in it is encoded using 'utf8' or if 'bytes'
83 # is passed in it is returned unchanged.
84 # Using this function is PY2/PY3 code should ensure in most cases
85 # the PY2 code runs unchanged in PY2 whereas the code in PY3 possibly
86 # encodes the variable (see PY2 implementation of this function below)
87 def get_bytes(bytesorstring
):
89 if isinstance(bytesorstring
, str):
90 tmp
= bytesorstring
.encode('utf8')
91 elif not isinstance(bytesorstring
, bytes
):
92 raise ValueError('Expected bytes or string for %s:%s' % (type(bytesorstring
), bytesorstring
))
95 # helper function to get a string from a variable that maybe 'str' or
96 # 'bytes' if 'bytes' then it is decoded using 'utf8'. If 'str' is passed
97 # it is returned unchanged
98 # Using this function is PY2/PY3 code should ensure in most cases
99 # the PY2 code runs unchanged in PY2 whereas the code in PY3 possibly
100 # decodes the variable (see PY2 implementation of this function below)
101 def get_string(bytesorstring
):
103 if isinstance(bytesorstring
, bytes
):
104 tmp
= bytesorstring
.decode('utf8')
105 elif not isinstance(bytesorstring
, str):
106 raise ValueError('Expected bytes or string for %s:%s' % (type(bytesorstring
), bytesorstring
))