s3:printing: Allow to run samba-bgqd as a standalone systemd service
[Samba.git] / python / samba / common.py
blobeafc4175b4cab74363a9f8cb4442eb47632db66f
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/>.
21 def cmp(x, y):
22 """
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.
28 """
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
38 """
39 if forced:
40 print("%s [YES]" % msg)
41 return True
43 mapping = {
44 'Y': True,
45 'YES': True,
46 '': False,
47 'N': False,
48 'NO': False,
51 prompt = '[y/N]'
53 if allow_all:
54 mapping['ALL'] = 'ALL'
55 mapping['NONE'] = 'NONE'
56 prompt = '[y/N/all/none]'
58 while True:
59 v = input(msg + ' %s ' % prompt)
60 v = v.upper()
61 if v in mapping:
62 return mapping[v]
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)
70 return str(ivalue)
73 # Sometimes 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.
77 # If a 'str' object is passed in it is encoded using 'utf8' or if 'bytes'
78 # is passed in it is returned unchanged.
79 def get_bytes(bytesorstring):
80 tmp = bytesorstring
81 if isinstance(bytesorstring, str):
82 tmp = bytesorstring.encode('utf8')
83 elif not isinstance(bytesorstring, bytes):
84 raise ValueError('Expected bytes or string for %s:%s' % (type(bytesorstring), bytesorstring))
85 return tmp
87 # helper function to get a string from a variable that maybe 'str' or
88 # 'bytes' if 'bytes' then it is decoded using 'utf8'. If 'str' is passed
89 # it is returned unchanged
90 def get_string(bytesorstring):
91 tmp = bytesorstring
92 if isinstance(bytesorstring, bytes):
93 tmp = bytesorstring.decode('utf8')
94 elif not isinstance(bytesorstring, str):
95 raise ValueError('Expected bytes or string for %s:%s' % (type(bytesorstring), bytesorstring))
96 return tmp