Changeset View
Standalone View
pykolab/utils.py
Show All 26 Lines | |||||
import pykolab | import pykolab | ||||
from pykolab import constants | from pykolab import constants | ||||
from pykolab.translate import _ | from pykolab.translate import _ | ||||
log = pykolab.getLogger('pykolab.utils') | log = pykolab.getLogger('pykolab.utils') | ||||
conf = pykolab.getConf() | conf = pykolab.getConf() | ||||
def ask_question(question, default="", password=False, confirm=False): | def ask_question(question, default="", password=False, confirm=False): | ||||
Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
""" | """ | ||||
Ask a question on stderr. | Ask a question on stderr. | ||||
Since the answer to the question may actually be a password, cover that | Since the answer to the question may actually be a password, cover that | ||||
case with a getpass.getpass() prompt. | case with a getpass.getpass() prompt. | ||||
Accepts a default value, but ignores defaults for password prompts. | Accepts a default value, but ignores defaults for password prompts. | ||||
Usage: pykolab.utils.ask_question("What is the server?", default="localhost") | Usage: pykolab.utils.ask_question("What is the server?", default="localhost") | ||||
Lint: PEP8 E501 line too long (85 > 79 characters) Lint: PEP8 E501: line too long (85 > 79 characters) | |||||
""" | """ | ||||
if not default == "" and not default == None and conf.cli_keywords.answer_default: | if not default == "" and not default == None and conf.cli_keywords.answer_default: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
Lint: PEP8 E501 line too long (86 > 79 characters) Lint: PEP8 E501: line too long (86 > 79 characters) | |||||
if not conf.cli_keywords.quiet: | if not conf.cli_keywords.quiet: | ||||
print ("%s [%s]: " % (question, default)) | print ("%s [%s]: " % (question, default)) | ||||
return default | return default | ||||
if password: | if password: | ||||
if default == "" or default == None: | if default == "" or default == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
answer = getpass.getpass("%s: " % (question)) | answer = getpass.getpass("%s: " % (question)) | ||||
else: | else: | ||||
answer = getpass.getpass("%s [%s]: " % (question, default)) | answer = getpass.getpass("%s [%s]: " % (question, default)) | ||||
else: | else: | ||||
if default == "" or default == None: | if default == "" or default == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
answer = raw_input("%s: " % (question)) | answer = raw_input("%s: " % (question)) | ||||
else: | else: | ||||
answer = raw_input("%s [%s]: " % (question, default)) | answer = raw_input("%s [%s]: " % (question, default)) | ||||
if not answer == "": | if not answer == "": | ||||
if confirm: | if confirm: | ||||
answer_confirm = None | answer_confirm = None | ||||
answer_confirmed = False | answer_confirmed = False | ||||
while not answer_confirmed: | while not answer_confirmed: | ||||
if password: | if password: | ||||
answer_confirm = getpass.getpass(_("Confirm %s: ") % (question)) | answer_confirm = getpass.getpass(_("Confirm %s: ") % (question)) | ||||
Lint: PEP8 E501 line too long (84 > 79 characters) Lint: PEP8 E501: line too long (84 > 79 characters) | |||||
else: | else: | ||||
answer_confirm = raw_input(_("Confirm %s: ") % (question)) | answer_confirm = raw_input(_("Confirm %s: ") % (question)) | ||||
if not answer_confirm == answer: | if not answer_confirm == answer: | ||||
print >> sys.stderr, _("Incorrect confirmation. " + \ | print >> sys.stderr, _("Incorrect confirmation. " + \ | ||||
Lint: PEP8 E502 the backslash is redundant between brackets Lint: PEP8 E502: the backslash is redundant between brackets | |||||
"Please try again.") | "Please try again.") | ||||
Lint: PEP8 E128 continuation line under-indented for visual indent Lint: PEP8 E128: continuation line under-indented for visual indent | |||||
if password: | if password: | ||||
if default == "" or default == None: | if default == "" or default == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
answer = getpass.getpass(_("%s: ") % (question)) | answer = getpass.getpass(_("%s: ") % (question)) | ||||
else: | else: | ||||
answer = getpass.getpass(_("%s [%s]: ") % (question, default)) | answer = getpass.getpass(_("%s [%s]: ") % (question, default)) | ||||
Lint: PEP8 E501 line too long (90 > 79 characters) Lint: PEP8 E501: line too long (90 > 79 characters) | |||||
else: | else: | ||||
if default == "" or default == None: | if default == "" or default == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
answer = raw_input(_("%s: ") % (question)) | answer = raw_input(_("%s: ") % (question)) | ||||
else: | else: | ||||
answer = raw_input(_("%s [%s]: ") % (question, default)) | answer = raw_input(_("%s [%s]: ") % (question, default)) | ||||
Lint: PEP8 E501 line too long (84 > 79 characters) Lint: PEP8 E501: line too long (84 > 79 characters) | |||||
else: | else: | ||||
answer_confirmed = True | answer_confirmed = True | ||||
if answer == "": | if answer == "": | ||||
return default | return default | ||||
else: | else: | ||||
return answer | return answer | ||||
def ask_confirmation(question, default="y", all_inclusive_no=True): | def ask_confirmation(question, default="y", all_inclusive_no=True): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
""" | """ | ||||
Create a confirmation dialog, including a default option (capitalized), | Create a confirmation dialog, including a default option (capitalized), | ||||
and a "yes" or "no" parsing that can either require an explicit, full | and a "yes" or "no" parsing that can either require an explicit, full | ||||
"yes" or "no", or take the default or any YyNn answer. | "yes" or "no", or take the default or any YyNn answer. | ||||
""" | """ | ||||
default_answer = None | default_answer = None | ||||
if default in [ "y", "Y" ]: | if default in [ "y", "Y" ]: | ||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
default_answer = True | default_answer = True | ||||
default_no = "n" | default_no = "n" | ||||
default_yes = "Y" | default_yes = "Y" | ||||
elif default in [ "n", "N" ]: | elif default in [ "n", "N" ]: | ||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
default_answer = False | default_answer = False | ||||
default_no = "N" | default_no = "N" | ||||
default_yes = "y" | default_yes = "y" | ||||
else: | else: | ||||
# This is a 'yes' or 'no' question the user | # This is a 'yes' or 'no' question the user | ||||
# needs to provide the full yes or no for. | # needs to provide the full yes or no for. | ||||
default_no = "'no'" | default_no = "'no'" | ||||
default_yes = "Please type 'yes'" | default_yes = "Please type 'yes'" | ||||
if conf.cli_keywords.answer_yes or (conf.cli_keywords.answer_default and default_answer is not None): | if conf.cli_keywords.answer_yes or (conf.cli_keywords.answer_default and default_answer is not None): | ||||
Lint: PEP8 E501 line too long (105 > 79 characters) Lint: PEP8 E501: line too long (105 > 79 characters) | |||||
if not conf.cli_keywords.quiet: | if not conf.cli_keywords.quiet: | ||||
print ("%s [%s/%s]: " % (question,default_yes,default_no)) | print ("%s [%s/%s]: " % (question,default_yes,default_no)) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
if conf.cli_keywords.answer_yes: | if conf.cli_keywords.answer_yes: | ||||
return True | return True | ||||
if conf.cli_keywords.answer_default: | if conf.cli_keywords.answer_default: | ||||
return default_answer | return default_answer | ||||
answer = False | answer = False | ||||
while answer == False: | while answer == False: | ||||
Lint: PEP8 E712 comparison to False should be 'if cond is False:' or 'if not cond:' Lint: PEP8 E712: comparison to False should be 'if cond is False:' or 'if not cond:' | |||||
answer = raw_input("%s [%s/%s]: " % (question,default_yes,default_no)) | answer = raw_input("%s [%s/%s]: " % (question,default_yes,default_no)) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
# Parse answer and set back to False if not appropriate | # Parse answer and set back to False if not appropriate | ||||
if all_inclusive_no: | if all_inclusive_no: | ||||
if answer == "" and not default_answer == None: | if answer == "" and not default_answer == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
return default_answer | return default_answer | ||||
elif answer in [ "y", "Y", "yes" ]: | elif answer in [ "y", "Y", "yes" ]: | ||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
return True | return True | ||||
elif answer in [ "n", "N", "no" ]: | elif answer in [ "n", "N", "no" ]: | ||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
return False | return False | ||||
else: | else: | ||||
answer = False | answer = False | ||||
print >> sys.stderr, _("Please answer 'yes' or 'no'.") | print >> sys.stderr, _("Please answer 'yes' or 'no'.") | ||||
else: | else: | ||||
if not answer in [ "y", "Y", "yes" ]: | if not answer in [ "y", "Y", "yes" ]: | ||||
Lint: PEP8 E713 test for membership should be 'not in' Lint: PEP8 E713: test for membership should be 'not in' | |||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
return False | return False | ||||
else: | else: | ||||
return True | return True | ||||
def ask_menu(question, options={}, default=''): | def ask_menu(question, options={}, default=''): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if not default == '' and conf.cli_keywords.answer_default: | if not default == '' and conf.cli_keywords.answer_default: | ||||
if not conf.cli_keywords.quiet: | if not conf.cli_keywords.quiet: | ||||
print question + " [" + default + "]:" | print question + " [" + default + "]:" | ||||
return default | return default | ||||
if not default == '': | if not default == '': | ||||
print question + " [" + default + "]:" | print question + " [" + default + "]:" | ||||
else: | else: | ||||
Show All 14 Lines | def ask_menu(question, options={}, default=''): | ||||
while not answer_correct: | while not answer_correct: | ||||
for key in keys: | for key in keys: | ||||
key_length = len("%s" % key) | key_length = len("%s" % key) | ||||
if key_length > max_key_length: | if key_length > max_key_length: | ||||
max_key_length = key_length | max_key_length = key_length | ||||
str_format = "%%%ds" % max_key_length | str_format = "%%%ds" % max_key_length | ||||
if default == '' or not default in options.keys(): | if default == '' or not default in options.keys(): | ||||
Lint: PEP8 E713 test for membership should be 'not in' Lint: PEP8 E713: test for membership should be 'not in' | |||||
for key in keys: | for key in keys: | ||||
if options[key] == key: | if options[key] == key: | ||||
print " - " + key | print " - " + key | ||||
else: | else: | ||||
print " - " + eval("str_format % key") + ": " + options[key] | print " - " + eval("str_format % key") + ": " + options[key] | ||||
Lint: PEP8 E501 line too long (80 > 79 characters) Lint: PEP8 E501: line too long (80 > 79 characters) | |||||
answer = raw_input(_("Choice") + ": ") | answer = raw_input(_("Choice") + ": ") | ||||
else: | else: | ||||
answer = raw_input(_("Choice (type '?' for options)") + ": ") | answer = raw_input(_("Choice (type '?' for options)") + ": ") | ||||
if answer == '?': | if answer == '?': | ||||
for key in keys: | for key in keys: | ||||
if options[key] == key: | if options[key] == key: | ||||
print " - " + key | print " - " + key | ||||
else: | else: | ||||
print " - " + eval("str_format % key") + ": " + options[key] | print " - " + eval("str_format % key") + ": " + options[key] | ||||
Lint: PEP8 E501 line too long (80 > 79 characters) Lint: PEP8 E501: line too long (80 > 79 characters) | |||||
continue | continue | ||||
if answer == '' and default in options.keys(): | if answer == '' and default in options.keys(): | ||||
answer = default | answer = default | ||||
if answer in [str(x) for x in options.keys()]: | if answer in [str(x) for x in options.keys()]: | ||||
answer_correct = True | answer_correct = True | ||||
return answer | return answer | ||||
def decode(key, enc): | def decode(key, enc): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if key == None: | if key == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
return enc | return enc | ||||
dec = [] | dec = [] | ||||
enc = base64.urlsafe_b64decode(enc) | enc = base64.urlsafe_b64decode(enc) | ||||
for i in range(len(enc)): | for i in range(len(enc)): | ||||
key_c = key[i % len(key)] | key_c = key[i % len(key)] | ||||
dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256) | dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256) | ||||
dec.append(dec_c) | dec.append(dec_c) | ||||
return "".join(dec) | return "".join(dec) | ||||
def encode(key, clear): | def encode(key, clear): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if key == None: | if key == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
return clear | return clear | ||||
enc = [] | enc = [] | ||||
for i in range(len(clear)): | for i in range(len(clear)): | ||||
key_c = key[i % len(key)] | key_c = key[i % len(key)] | ||||
enc_c = chr((ord(clear[i]) + ord(key_c)) % 256) | enc_c = chr((ord(clear[i]) + ord(key_c)) % 256) | ||||
enc.append(enc_c) | enc.append(enc_c) | ||||
return base64.urlsafe_b64encode("".join(enc)) | return base64.urlsafe_b64encode("".join(enc)) | ||||
def ensure_directory(_dir, _user='root', _group='root'): | def ensure_directory(_dir, _user='root', _group='root'): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if not os.path.isdir(_dir): | if not os.path.isdir(_dir): | ||||
os.makedirs(_dir) | os.makedirs(_dir) | ||||
try: | try: | ||||
try: | try: | ||||
(ruid, euid, suid) = os.getresuid() | (ruid, euid, suid) = os.getresuid() | ||||
(rgid, egid, sgid) = os.getresgid() | (rgid, egid, sgid) = os.getresgid() | ||||
except AttributeError, errmsg: | except AttributeError, errmsg: | ||||
Show All 37 Lines | try: | ||||
) = pwd.getpwnam(_user) | ) = pwd.getpwnam(_user) | ||||
except KeyError: | except KeyError: | ||||
print >> sys.stderr, _("User %s does not exist") % (_user) | print >> sys.stderr, _("User %s does not exist") % (_user) | ||||
sys.exit(1) | sys.exit(1) | ||||
# Set real and effective user if not the same as current. | # Set real and effective user if not the same as current. | ||||
Lint: PEP8 E303 too many blank lines (2) Lint: PEP8 E303: too many blank lines (2) | |||||
if not user_uid == ruid: | if not user_uid == ruid: | ||||
os.chown(_dir, user_uid, -1) | os.chown(_dir, user_uid, -1) | ||||
except: | except: | ||||
print >> sys.stderr, _("Could not change the permissions on %s") % (_dir) | print >> sys.stderr, _("Could not change the permissions on %s") % (_dir) | ||||
Lint: PEP8 E501 line too long (81 > 79 characters) Lint: PEP8 E501: line too long (81 > 79 characters) | |||||
def generate_password(): | def generate_password(): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
import subprocess | import subprocess | ||||
p1 = subprocess.Popen(['head', '-c', '200', '/dev/urandom'], stdout=subprocess.PIPE) | p1 = subprocess.Popen(['head', '-c', '200', '/dev/urandom'], stdout=subprocess.PIPE) | ||||
Lint: PEP8 E501 line too long (88 > 79 characters) Lint: PEP8 E501: line too long (88 > 79 characters) | |||||
p2 = subprocess.Popen(['tr', '-dc', '_A-Z-a-z-0-9'], stdin=p1.stdout, stdout=subprocess.PIPE) | p2 = subprocess.Popen(['tr', '-dc', '_A-Z-a-z-0-9'], stdin=p1.stdout, stdout=subprocess.PIPE) | ||||
Lint: PEP8 E501 line too long (97 > 79 characters) Lint: PEP8 E501: line too long (97 > 79 characters) | |||||
p3 = subprocess.Popen(['head', '-c', '15'], stdin=p2.stdout, stdout=subprocess.PIPE) | p3 = subprocess.Popen(['head', '-c', '15'], stdin=p2.stdout, stdout=subprocess.PIPE) | ||||
Lint: PEP8 E501 line too long (88 > 79 characters) Lint: PEP8 E501: line too long (88 > 79 characters) | |||||
p1.stdout.close() | p1.stdout.close() | ||||
p2.stdout.close() | p2.stdout.close() | ||||
output = p3.communicate()[0] | output = p3.communicate()[0] | ||||
return output | return output | ||||
def multiline_message(message): | def multiline_message(message): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if hasattr(conf, 'cli_keywords') and hasattr(conf.cli_keywords, 'quiet'): | if hasattr(conf, 'cli_keywords') and hasattr(conf.cli_keywords, 'quiet'): | ||||
if conf.cli_keywords.quiet: | if conf.cli_keywords.quiet: | ||||
return "" | return "" | ||||
column_width = 80 | column_width = 80 | ||||
# First, replace all occurences of "\n" | # First, replace all occurences of "\n" | ||||
message = message.replace(" ", "") | message = message.replace(" ", "") | ||||
Show All 10 Lines | for word in message.split(): | ||||
line = word | line = word | ||||
else: | else: | ||||
line += " %s" % (word) | line += " %s" % (word) | ||||
lines.append(line) | lines.append(line) | ||||
return "\n%s\n" % ("\n".join(lines)) | return "\n%s\n" % ("\n".join(lines)) | ||||
def stripped_message(message): | def stripped_message(message): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
return "\n" + message.strip() + "\n" | return "\n" + message.strip() + "\n" | ||||
def str2unicode(s, encoding='utf-8'): | def str2unicode(s, encoding='utf-8'): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if isinstance(s, unicode): | if isinstance(s, unicode): | ||||
return s | return s | ||||
try: | try: | ||||
return unicode(s, encoding) | return unicode(s, encoding) | ||||
except: | except: | ||||
pass | pass | ||||
return s | return s | ||||
def normalize(_object): | def normalize(_object): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if type(_object) == list: | if type(_object) == list: | ||||
result = [] | result = [] | ||||
elif type(_object) == dict: | elif type(_object) == dict: | ||||
result = {} | result = {} | ||||
else: | else: | ||||
return _object | return _object | ||||
if type(_object) == list: | if type(_object) == list: | ||||
for item in _object: | for item in _object: | ||||
result.append(item.lower()) | result.append(item.lower()) | ||||
result = list(set(result)) | result = list(set(result)) | ||||
return result | return result | ||||
elif type(_object) == dict: | elif type(_object) == dict: | ||||
for key in _object.keys(): | def _strip(value): | ||||
try: | |||||
return value.strip() | |||||
except: | |||||
return value | |||||
for key in _object: | |||||
if type(_object[key]) == list: | if type(_object[key]) == list: | ||||
if _object[key] == None: | if _object[key] is None: | ||||
continue | continue | ||||
if len(_object[key]) == 1: | val = map(_strip, _object[key]) | ||||
result[key.lower()] = ''.join(_object[key]) | |||||
if len(val) == 1: | |||||
result[key.lower()] = ''.join(val) | |||||
else: | else: | ||||
result[key.lower()] = _object[key] | result[key.lower()] = val | ||||
else: | else: | ||||
if _object[key] == None: | if _object[key] is None: | ||||
continue | continue | ||||
# What the heck? | result[key.lower()] = _strip(_object[key]) | ||||
result[key.lower()] = _object[key] | |||||
if result.has_key('objectsid') and not result['objectsid'][0] == "S": | if result.has_key('objectsid') and not result['objectsid'][0] == "S": | ||||
result['objectsid'] = sid_to_string(result['objectsid']) | result['objectsid'] = sid_to_string(result['objectsid']) | ||||
if result.has_key('sn'): | if result.has_key('sn'): | ||||
result['surname'] = result['sn'].replace(' ', '') | result['surname'] = result['sn'].replace(' ', '') | ||||
if result.has_key('mail'): | if result.has_key('mail'): | ||||
if isinstance(result['mail'], list): | if isinstance(result['mail'], list): | ||||
result['mail'] = result['mail'][0] | result['mail'] = result['mail'][0] | ||||
if len(result['mail']) > 0: | if len(result['mail']) > 0: | ||||
if len(result['mail'].split('@')) > 1: | if len(result['mail'].split('@')) > 1: | ||||
result['domain'] = result['mail'].split('@')[1] | result['domain'] = result['mail'].split('@')[1] | ||||
if not result.has_key('domain') and result.has_key('standard_domain'): | if not result.has_key('domain') and result.has_key('standard_domain'): | ||||
result['domain'] = result['standard_domain'] | result['domain'] = result['standard_domain'] | ||||
return result | return result | ||||
def parse_input(_input, splitchars= [ ' ' ]): | def parse_input(_input, splitchars= [ ' ' ]): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
""" | """ | ||||
Split the input string using the split characters defined | Split the input string using the split characters defined | ||||
in splitchars, and remove the empty list items, then unique the | in splitchars, and remove the empty list items, then unique the | ||||
list items. | list items. | ||||
Takes a string as input, and a list of characters the string should be | Takes a string as input, and a list of characters the string should be | ||||
split with (list of delimiter characters). | split with (list of delimiter characters). | ||||
""" | """ | ||||
Show All 9 Lines | def parse_input(_input, splitchars= [ ' ' ]): | ||||
for item in _parse_list: | for item in _parse_list: | ||||
if not item == '': | if not item == '': | ||||
if _output_list.count(item) < 1: | if _output_list.count(item) < 1: | ||||
_output_list.append(item) | _output_list.append(item) | ||||
return _output_list | return _output_list | ||||
def parse_ldap_uri(uri): | def parse_ldap_uri(uri): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
""" | """ | ||||
Parse an LDAP URI and return it's components. | Parse an LDAP URI and return it's components. | ||||
Returns a tuple containing; | Returns a tuple containing; | ||||
- protocol (ldap, ldaps), | - protocol (ldap, ldaps), | ||||
- server (address or None), | - server (address or None), | ||||
- base_dn, | - base_dn, | ||||
Show All 28 Lines | try: | ||||
else: | else: | ||||
_port = "389" | _port = "389" | ||||
if _server == '': | if _server == '': | ||||
_server = None | _server = None | ||||
if _attr == '': | if _attr == '': | ||||
_attrs = [] | _attrs = [] | ||||
else: | else: | ||||
_attrs = [ _attr ] | _attrs = [ _attr ] | ||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
if _scope == '': | if _scope == '': | ||||
_scope = 'sub' | _scope = 'sub' | ||||
if _filter == '': | if _filter == '': | ||||
_filter = "(objectclass=*)" | _filter = "(objectclass=*)" | ||||
return (_protocol, _server, _port, _base_dn, _attr, _scope, _filter) | return (_protocol, _server, _port, _base_dn, _attr, _scope, _filter) | ||||
except: | except: | ||||
return None | return None | ||||
def pop_empty_from_list(_input_list): | def pop_empty_from_list(_input_list): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
_output_list = [] | _output_list = [] | ||||
for item in _input_list: | for item in _input_list: | ||||
if not item == '': | if not item == '': | ||||
_output_list.append(item) | _output_list.append(item) | ||||
def sid_to_string(sid): | def sid_to_string(sid): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
srl = ord(sid[0]) | srl = ord(sid[0]) | ||||
number_sub_id = ord(sid[1]) | number_sub_id = ord(sid[1]) | ||||
iav = struct.unpack('!Q', '\x00\x00' + sid[2:8])[0] | iav = struct.unpack('!Q', '\x00\x00' + sid[2:8])[0] | ||||
sub_ids = [] | sub_ids = [] | ||||
for i in range(number_sub_id): | for i in range(number_sub_id): | ||||
sub_ids.append(struct.unpack('<I',sid[8+4*i:12+4*i])[0]) | sub_ids.append(struct.unpack('<I',sid[8+4*i:12+4*i])[0]) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
result = 'S-%d-%d-%s' % ( | result = 'S-%d-%d-%s' % ( | ||||
srl, | srl, | ||||
iav, | iav, | ||||
'-'.join([str(s) for s in sub_ids]), | '-'.join([str(s) for s in sub_ids]), | ||||
) | ) | ||||
return result | return result | ||||
def standard_root_dn(domain): | def standard_root_dn(domain): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
return 'dc=%s' % (',dc='.join(domain.split('.'))) | return 'dc=%s' % (',dc='.join(domain.split('.'))) | ||||
def translate(mystring, locale_name='en_US'): | def translate(mystring, locale_name='en_US'): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
import locale | import locale | ||||
import subprocess | import subprocess | ||||
log.debug(_("Transliterating string %r with locale %r") % (mystring, locale_name), level=8) | log.debug(_("Transliterating string %r with locale %r") % (mystring, locale_name), level=8) | ||||
Lint: PEP8 E501 line too long (95 > 79 characters) Lint: PEP8 E501: line too long (95 > 79 characters) | |||||
if len(locale.normalize(locale_name).split('.')) > 1: | if len(locale.normalize(locale_name).split('.')) > 1: | ||||
(locale_name,locale_charset) = locale.normalize(locale_name).split('.') | (locale_name,locale_charset) = locale.normalize(locale_name).split('.') | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
else: | else: | ||||
locale_charset = 'utf-8' | locale_charset = 'utf-8' | ||||
try: | try: | ||||
log.debug(_("Attempting to set locale"), level=8) | log.debug(_("Attempting to set locale"), level=8) | ||||
locale.setlocale(locale.LC_ALL, (locale_name,locale_charset)) | locale.setlocale(locale.LC_ALL, (locale_name,locale_charset)) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
log.debug(_("Success setting locale"), level=8) | log.debug(_("Success setting locale"), level=8) | ||||
except: | except: | ||||
log.debug(_("Failure to set locale"), level=8) | log.debug(_("Failure to set locale"), level=8) | ||||
pass | pass | ||||
command = [ '/usr/bin/iconv', | command = [ '/usr/bin/iconv', | ||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
'-f', 'UTF-8', | '-f', 'UTF-8', | ||||
'-t', 'ASCII//TRANSLIT', | '-t', 'ASCII//TRANSLIT', | ||||
'-s' ] | '-s' ] | ||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
log.debug(_("Executing '%s | %s'") % (r"%s" % (mystring), ' '.join(command)), level=8) | log.debug(_("Executing '%s | %s'") % (r"%s" % (mystring), ' '.join(command)), level=8) | ||||
Lint: PEP8 E501 line too long (90 > 79 characters) Lint: PEP8 E501: line too long (90 > 79 characters) | |||||
process = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, env={'LANG': locale.normalize(locale_name)}) | process = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, env={'LANG': locale.normalize(locale_name)}) | ||||
Lint: PEP8 E501 line too long (155 > 79 characters) Lint: PEP8 E501: line too long (155 > 79 characters) | |||||
try: | try: | ||||
print >> process.stdin, r"%s" % mystring | print >> process.stdin, r"%s" % mystring | ||||
except UnicodeEncodeError, errmsg: | except UnicodeEncodeError, errmsg: | ||||
pass | pass | ||||
result = process.communicate()[0].strip() | result = process.communicate()[0].strip() | ||||
if '?' in result or (result == '' and not mystring == ''): | if '?' in result or (result == '' and not mystring == ''): | ||||
log.warning(_("Could not translate %s using locale %s") % (mystring, locale_name)) | log.warning(_("Could not translate %s using locale %s") % (mystring, locale_name)) | ||||
Lint: PEP8 E501 line too long (90 > 79 characters) Lint: PEP8 E501: line too long (90 > 79 characters) | |||||
from pykolab import translit | from pykolab import translit | ||||
result = translit.transliterate(mystring, locale_name) | result = translit.transliterate(mystring, locale_name) | ||||
return result | return result | ||||
def true_or_false(val): | def true_or_false(val): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if val == None: | if val == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
return False | return False | ||||
if isinstance(val, bool): | if isinstance(val, bool): | ||||
return val | return val | ||||
if isinstance(val, basestring) or isinstance(val, str): | if isinstance(val, basestring) or isinstance(val, str): | ||||
val = val.lower() | val = val.lower() | ||||
if val in [ "true", "yes", "y", "1" ]: | if val in [ "true", "yes", "y", "1" ]: | ||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
return True | return True | ||||
else: | else: | ||||
return False | return False | ||||
if isinstance(val, int) or isinstance(val, float): | if isinstance(val, int) or isinstance(val, float): | ||||
if val >= 1: | if val >= 1: | ||||
return True | return True | ||||
else: | else: | ||||
return False | return False | ||||
def is_service(services): | def is_service(services): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
""" | """ | ||||
Checks each item in list services to see if it has a RC script in | Checks each item in list services to see if it has a RC script in | ||||
pykolab.constants.RC_DIR to see if it's a service, and returns | pykolab.constants.RC_DIR to see if it's a service, and returns | ||||
the name of the service for the first service it can find. However, | the name of the service for the first service it can find. However, | ||||
it also checks whether the other services exist and issues a warning if | it also checks whether the other services exist and issues a warning if | ||||
more then one service exists. | more then one service exists. | ||||
Usage: utils.is_service(['dirsrv', 'ldap']) | Usage: utils.is_service(['dirsrv', 'ldap']) | ||||
""" | """ | ||||
_service = None | _service = None | ||||
_other_services = [] | _other_services = [] | ||||
for service in services: | for service in services: | ||||
if os.path.isfile(os.path.join(constants.RC_DIR, service)): | if os.path.isfile(os.path.join(constants.RC_DIR, service)): | ||||
if _service == '': | if _service == '': | ||||
_service = service | _service = service | ||||
else: | else: | ||||
_other_services.append(service) | _other_services.append(service) | ||||
return (_service,_other_services) | return (_service,_other_services) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' |
expected 2 blank lines, found 1