Changeset View
Changeset View
Standalone View
Standalone View
pykolab/utils.py
Show First 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | if not answer == "": | ||||
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)) | ||||
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 | |||||
"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: | ||||
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: | ||||
Show All 28 Lines | def ask_confirmation(question, default="y", all_inclusive_no=True): | ||||
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): | ||||
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: | ||||
return default_answer | return default_answer | ||||
elif answer in [ "y", "Y", "yes" ]: | elif answer in [ "y", "Y", "yes" ]: | ||||
return True | return True | ||||
elif answer in [ "n", "N", "no" ]: | elif answer in [ "n", "N", "no" ]: | ||||
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' | |||||
return False | return False | ||||
else: | else: | ||||
return True | return True | ||||
def ask_menu(question, options={}, default=''): | def ask_menu(question, options={}, default=''): | ||||
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 + "]:" | ||||
Show All 19 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] | ||||
answer = raw_input(_("Choice") + ": ") | answer = raw_input(_("Choice") + ": ") | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 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) | ||||
def generate_password(): | def generate_password(): | ||||
import subprocess | import subprocess | ||||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | elif type(_object) == dict: | ||||
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'] | ||||
if 'objectclass' in result and result['objectclass'] is not None: | |||||
oclist = result['objectclass'] if isinstance(result['objectclass'], list) else [result['objectclass']] | |||||
vanmeeuwenUnsubmitted Not Done Inline ActionsThis isn't generally our coding style -- difficult to read. This can just be: if not result.has_key('objectclass'): result['objectclass'] = [] if result['objectclass'] is None: result['objectclass'] = [] if not isinstance(result['objectclass'], list): result['objectclass'] = [result['objectclass']] result['objectclass'] = [x.lower() for x in result['objectclass']] vanmeeuwen: This isn't generally our coding style -- difficult to read.
This can just be:
if not result. | |||||
result['objectclass'] = [x.lower() for x in oclist] | |||||
else: | |||||
result['objectclass'] = [] | |||||
return result | return result | ||||
def parse_input(_input, splitchars= [ ' ' ]): | def parse_input(_input, splitchars= [ ' ' ]): | ||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
""" | """ | ||||
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 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | |||||
def sid_to_string(sid): | def sid_to_string(sid): | ||||
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): | ||||
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'): | ||||
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) | ||||
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', | ||||
'-f', 'UTF-8', | '-f', 'UTF-8', | ||||
'-t', 'ASCII//TRANSLIT', | '-t', 'ASCII//TRANSLIT', | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | def is_service(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 ',' |
the backslash is redundant between brackets