From: Pavlos Ratis Date: Mon, 19 May 2014 23:38:01 +0000 (+0300) Subject: misc improvements X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=030a76d75fe5cdad175355c90680361cd5b59723;p=gentoo-keys.git misc improvements * Rename some variables to a more easy-to-understand name * Improve some strings and fix typos --- diff --git a/gkeyldap/actions.py b/gkeyldap/actions.py index 27b35bf..55c3e13 100644 --- a/gkeyldap/actions.py +++ b/gkeyldap/actions.py @@ -13,7 +13,7 @@ import os import re -from gkeys.config import GKEY, KEYID, LONGKEYID, FINGERPRINT, KEY_LEN +from gkeys.config import GKEY, KEYID, LONGKEYID, FINGERPRINT, KEYLEN_MAP from gkeys.seed import Seeds from gkeyldap.search import (LdapSearch, UID, gkey2ldap_map, gkey2SEARCH) @@ -21,22 +21,21 @@ from gkeyldap.search import (LdapSearch, UID, gkey2ldap_map, gkey2SEARCH) Available_Actions = ['ldapsearch', 'updateseeds'] -def get_key_ids(key, info): +def get_key_ids(key_len, keyids): '''Small utility function to return only keyid (short) or longkeyid's - @param key: string, the key length desired - @param info: list of keysid's to process + @param key_len: string, the key length desired + @param keyids: list of keysid's to process @return list of the desired key length id's ''' result = [] - for x in info: - if x.startswith('0x'): - mylen = KEY_LEN[key] + 2 - else: - mylen = KEY_LEN[key] - if len(x) == mylen: - result.append(x) + for keyid in keyids: + target_len = KEYLEN_MAP[key_len] + if keyid.startswith('0x'): + target_len = KEYLEN_MAP[key_len] + 2 + if len(keyid) == target_len: + result.append(keyid) return result @@ -53,11 +52,11 @@ class Actions(object): def ldapsearch(self, args): l = LdapSearch() - self.logger.info("Search...establishing connection") - self.output("Search...establishing connection") + self.logger.info("Search... Establishing connection") + self.output("Search... Establishing connection") if not l.connect(): - self.logger.info("Aborting Search...Connection failed") - self.output("Aborting Search...Connection failed") + self.logger.info("Aborting search... Connection failed") + self.output("Aborting search... Connection failed") return False self.logger.debug("MAIN: _action_ldapsearch; args = %s" % str(args)) x, target, search_field = self.get_args(args) @@ -66,26 +65,26 @@ class Actions(object): for dev in sorted(devs): self.output(dev, devs[dev]) self.output("============================================") - self.output("Total number of devs in results:", len(devs)) + self.output("Total number of developers in results:", len(devs)) self.logger.info("============================================") - self.logger.info("Total number of devs in results: %d" % len(devs)) + self.logger.info("Total number of developers in results: %d" % len(devs)) return True def updateseeds(self, args): - self.logger.info("Beginning ldap search...") - self.output("Beginning ldap search...") + self.logger.info("Beginning LDAP search...") + self.output("Beginning LDAP search...") l = LdapSearch() if not l.connect(): - self.output("Aborting Update...Connection failed") - self.logger.info("Aborting Update...Connection failed") + self.output("Aborting update... Connection failed") + self.logger.info("Aborting update... Connection failed") return False results = l.search('*', UID) info = l.result2dict(results, 'uid') self.logger.debug( "MAIN: _action_updateseeds; got results :) converted to info") if not self.create_seedfile(info): - self.logger.error("Dev seed file update failure: " + self.logger.error("Developer seed file update failure: " "Original seed file is intact & untouched.") filename = self.config['dev-seedfile'] old = filename + '.old' @@ -108,12 +107,12 @@ class Actions(object): os.rename(filename + '.new', filename) except IOError: raise - self.output("Developer Seed file updated") + self.output("Developer seed file updated!") return True def create_seedfile(self, devs): - self.output("Creating seeds from ldap data...") + self.output("Creating seeds from LDAP data...") filename = self.config['dev-seedfile'] + '.new' self.seeds = Seeds(filename) count = 0 @@ -131,22 +130,22 @@ class Actions(object): else: error_count += 1 self.output("Total number of seeds created:", count) - self.output("Seeds created...saving file: %s" % filename) - self.output("Total number of Dev's with gpg errors:", error_count) + self.output("Seeds created... Saving file: %s" % filename) + self.output("Total number of Dev's with GPG errors:", error_count) self.logger.info("Total number of seeds created: %d" % count) - self.logger.info("Seeds created...saving file: %s" % filename) - self.logger.info("Total number of Dev's with gpg errors: %d" % error_count) + self.logger.info("Seeds created... Saving file: %s" % filename) + self.logger.info("Total number of Dev's with GPG errors: %d" % error_count) return self.seeds.save() @staticmethod def get_args(args): - for x in ['nick', 'name', 'gpgkey', 'fingerprint', 'status']: - if x: - target = getattr(args, x) - search_field = gkey2SEARCH[x] + for attr in ['nick', 'name', 'gpgkey', 'fingerprint', 'status']: + if attr: + target = getattr(args, attr) + search_field = gkey2SEARCH[attr] break - return (x, target, search_field) + return (attr, target, search_field) def build_gkeylist(self, info): @@ -156,8 +155,8 @@ class Actions(object): # assume it's good until an error is found is_good = True #self.logger.debug("Actions: build_gkeylist; info = %s" % str(info)) - for x in GKEY._fields: - field = gkey2ldap_map[x] + for attr in GKEY._fields: + field = gkey2ldap_map[attr] if not field: keyinfo.append(None) continue @@ -167,24 +166,24 @@ class Actions(object): if values and field in ['uid', 'cn' ]: value = values[0] # separate out short/long key id's - elif values and x in ['keyid', 'longkeyid']: - value = get_key_ids(x, values) + elif values and attr in ['keyid', 'longkeyid']: + value = get_key_ids(attr, values) if len(value): keyid_found = True - elif values and x in ['fingerprint']: + elif values and attr in ['fingerprint']: value = [v.replace(' ', '') for v in values] else: value = values if 'undefined' in values: - self.logger.error('ERROR in ldap info for: %s, %s' + self.logger.error('ERROR in LDAP info for: %s, %s' %(info['uid'][0],info['cn'][0])) self.logger.error(' %s = "undefined"' %(field)) is_good = False keyinfo.append(value) except KeyError: - self.logger.debug('Ldap info for: %s, %s' + self.logger.debug('LDAP info for: %s, %s' %(info['uid'][0],info['cn'][0])) - self.logger.debug(' MISSING or EMPTY ldap field ' + + self.logger.debug(' MISSING or EMPTY LDAP field ' + '[%s] GPGKey field [%s]' %(field, x)) if x in ['keyid', 'longkeyid']: keyid_missing = True @@ -195,21 +194,21 @@ class Actions(object): fingerprint = None try: fingerprint = info[gkey2ldap_map['fingerprint']] - self.logger.debug(' Generate gpgkey, Found ldap fingerprint field') + self.logger.debug(' Generate gpgkey, Found LDAP fingerprint field') except KeyError: - gpgkey = 'Missing fingerprint from ldap info' - self.logger.debug(' Generate gpgkey, ldap fingerprint KeyError') + gpgkey = 'Missing fingerprint from LDAP info' + self.logger.debug(' Generate gpgkey, LDAP fingerprint KeyError') if fingerprint: values = [y.strip('\n') for y in fingerprint] value = [v.replace(' ', '') for v in values] # assign it to gpgkey to prevent a possible # "gpgkey" undefined error - gpgkey = ['0x' + x[-KEY_LEN['longkeyid']:] for x in value] + gpgkey = ['0x' + x[-KEYLEN_MAP['longkeyid']:] for x in value] keyinfo[LONGKEYID] = gpgkey self.logger.debug(' Generate gpgkey, NEW keyinfo[LONGKEYID] = %s' % str(keyinfo[LONGKEYID])) else: - gpgkey = 'Missing or Bad fingerprint from ldap info' + gpgkey = 'Missing or Bad fingerprint from LDAP info' is_good = False if not keyinfo[LONGKEYID]: self.logger.error('ERROR in ldap info for: %s, %s' @@ -239,7 +238,7 @@ class Actions(object): index = len(y.lstrip('0x')) if y.lstrip('0x').upper() not in \ [x[-index:].upper() for x in keyinfo[FINGERPRINT]]: - self.logger.error('ERROR in ldap info for: %s, %s' + self.logger.error('ERROR in LDAP info for: %s, %s' %(info['uid'][0],info['cn'][0])) self.logger.error(' ' + str(keyinfo)) self.logger.error(' GPGKey id %s not found in the ' @@ -251,19 +250,19 @@ class Actions(object): def _check_fingerprint_integrity(self, info, keyinfo): # assume it's good until found an error is found is_good = True - for x in keyinfo[FINGERPRINT]: + for fingerprint in keyinfo[FINGERPRINT]: # check fingerprint integrity - if len(x) != 40: - self.logger.error('ERROR in ldap info for: %s, %s' + if len(fingerprint) != 40: + self.logger.error('ERROR in LDAP info for: %s, %s' %(info['uid'][0],info['cn'][0])) self.logger.error(' GPGKey incorrect fingerprint ' + - 'length (%s) for fingerprint: %s' %(len(x), x)) + 'length (%s) for fingerprint: %s' %(len(fingerprint), fingerprint)) is_good = False continue - if not self.fingerprint_re.match(x): + if not self.fingerprint_re.match(fingerprint): self.logger.error('ERROR in ldap info for: %s, %s' %(info['uid'][0],info['cn'][0])) self.logger.error(' GPGKey: Non hexadecimal digits in ' + - 'fingerprint for fingerprint: ' + x) + 'fingerprint for fingerprint: ' + fingerprint) is_good = False return is_good diff --git a/gkeyldap/search.py b/gkeyldap/search.py index 2cbc4b9..087bee6 100644 --- a/gkeyldap/search.py +++ b/gkeyldap/search.py @@ -60,7 +60,7 @@ gkey2SEARCH = { class LdapSearch(object): - '''Class to perform searches on the configured ldap server + '''Class to perform searches on the configured LDAP server ''' def __init__(self, server=None, fields=None, criteria=None): @@ -74,9 +74,9 @@ class LdapSearch(object): def connect(self, server=None,): - '''Creates our ldap server connection - + '''Creates our LDAP server connection ''' + if server: self.server = server logger.debug('LdapSearch: connect; new server: %s' % self.server) @@ -93,9 +93,8 @@ class LdapSearch(object): return True - def search(self, target, search_field=UID, fields=None, criteria=None): - '''Perform the ldap search + '''Perform the LDAP search ''' if not target: logger.debug('LdapSearch: search; invalid target: "%s"' % target) diff --git a/gkeys/actions.py b/gkeys/actions.py index 85c0e0e..79690fc 100644 --- a/gkeys/actions.py +++ b/gkeys/actions.py @@ -76,7 +76,7 @@ class Actions(object): success = self.seeds.add(gkey) if success: success = self.seeds.save() - return ["Successfully Added new seed: %s" % str(success), gkey] + return ["Successfully added new seed: %s" % str(success), gkey] else: messages = ["Matching seeds found in seeds file", "Aborting... \nMatching seeds:"] @@ -91,20 +91,20 @@ class Actions(object): self.logger.debug("ACTIONS: removeseed; gkey: %s" % str(searchkey)) gkeys = self.listseed(args) if not gkeys: - return ["Failed to Removed seed: No gkeys returned from listseed()", + return ["Failed to remove seed: No gkeys returned from listseed()", None] if len(gkeys) == 1: self.logger.debug("ACTIONS: removeseed; now deleting gkey: %s" % str(gkeys[0])) success = self.seeds.delete(gkeys[0]) if success: success = self.seeds.save() - return ["Successfully Removed seed: %s" % str(success), + return ["Successfully removed seed: %s" % str(success), gkeys[0]] elif len(gkeys): messages = ["Too many seeds found to remove"] messages.extend(gkeys) return messages - return ["Failed to Remove seed:", searchkey, + return ["Failed to remove seed:", searchkey, "No matching seed found"] @@ -138,7 +138,7 @@ class Actions(object): messages = ["Too many seeds found to move"] messages.extend(sourcekeys) return messages - messages.append("Failed to Move seed:") + messages.append("Failed to move seed:") messages.append(searchkey) messages.append('\n') messages.append("Source seeds found...") diff --git a/gkeys/config.py b/gkeys/config.py index 1ff5a18..b20d5cd 100644 --- a/gkeys/config.py +++ b/gkeys/config.py @@ -120,7 +120,7 @@ KEYDIR = 4 FINGERPRINT = 5 # set some defaults -KEY_LEN = { +KEYLEN_MAP = { 'keyid': 8, 'longkeyid': 16, } diff --git a/gkeys/seed.py b/gkeys/seed.py index 1dd8fc4..d5a92e3 100644 --- a/gkeys/seed.py +++ b/gkeys/seed.py @@ -92,7 +92,6 @@ class Seeds(object): return False - def delete(self, gkey=None, index=None): '''Delete the key from the seeds in memory diff --git a/gkeys/seedhandler.py b/gkeys/seedhandler.py index 2506277..7f73857 100644 --- a/gkeys/seedhandler.py +++ b/gkeys/seedhandler.py @@ -13,7 +13,7 @@ import re from gkeys.config import (GKEY, NICK, NAME, KEYID, LONGKEYID, FINGERPRINT, - KEY_LEN) + KEYLEN_MAP) class SeedHandler(object): @@ -40,11 +40,11 @@ class SeedHandler(object): @staticmethod def build_gkeydict(args): keyinfo = {} - for x in GKEY._fields: + for attr in GKEY._fields: try: - value = getattr(args, x) + value = getattr(args, attr) if value: - keyinfo[x] = value + keyinfo[attr] = value except AttributeError: pass return keyinfo @@ -56,20 +56,20 @@ class SeedHandler(object): # assume it's good until an error is found is_good = True #self.logger.debug("SeedHandler: build_gkeylist; args = %s" % str(args)) - for x in GKEY._fields: - if GKEY.field_types[x] is str: + for attr in GKEY._fields: + if GKEY.field_types[attr] is str: try: - value = getattr(args, x) + value = getattr(args, attr) except AttributeError: value = None - elif GKEY.field_types[x] is list: + elif GKEY.field_types[attr] is list: try: - values = [y for y in getattr(args, x).split(':')] + values = [y for y in getattr(args, attr).split(':')] value = [v.replace(' ', '') for v in values] except AttributeError: value = None keyinfo.append(value) - if x in ["keyid", "longkeyid"] and value: + if attr in ["keyid", "longkeyid"] and value: keyid_found = True if not keyid_found and needkeyid: fingerprint = keyinfo[FINGERPRINT] @@ -78,7 +78,7 @@ class SeedHandler(object): 'fingerprint in args') # assign it to gpgkey to prevent a possible # "gpgkey" undefined error - gpgkey = ['0x' + x[-KEY_LEN['longkeyid']:] for x in fingerprint] + gpgkey = ['0x' + x[-KEYLEN_MAP['longkeyid']:] for x in fingerprint] keyinfo[LONGKEYID] = gpgkey self.logger.debug(' Generate gpgkey longkeyid, NEW ' 'keyinfo[LONGKEYID] = %s' % str(keyinfo[LONGKEYID])) @@ -139,19 +139,19 @@ class SeedHandler(object): def _check_fingerprint_integrity(self, keyinfo): # assume it's good until an error is found is_good = True - for x in keyinfo[FINGERPRINT]: + for fingerprint in keyinfo[FINGERPRINT]: # check fingerprint integrity - if len(x) != 40: + if len(fingerprint) != 40: self.logger.error('ERROR in keyinfo for: %s, %s' %(keyinfo[NICK], keyinfo[NAME])) self.logger.error(' GPGKey incorrect fingerprint ' + 'length (%s) for fingerprint: %s' %(len(x), x)) is_good = False continue - if not self.fingerprint_re.match(x): + if not self.fingerprint_re.match(fingerprint): self.logger.error('ERROR in keyinfo info for: %s, %s' %(keyinfo[NICK], keyinfo[NAME])) self.logger.error(' GPGKey: Non hexadecimal digits in ' + - 'fingerprint for fingerprint: ' + x) + 'fingerprint for fingerprint: ' + fingerprint) is_good = False return is_good