From: Brian Dolbec Date: Tue, 25 Jun 2013 15:38:41 +0000 (-0700) Subject: Refactor GKEY calss to handle all packing and unpacking of data in order to handle... X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=cf1a206c19d679e67884633e0dda62454cad88c7;p=gentoo-keys.git Refactor GKEY calss to handle all packing and unpacking of data in order to handle list type field data. --- diff --git a/gkeys/config.py b/gkeys/config.py index f775d23..8b8360f 100644 --- a/gkeys/config.py +++ b/gkeys/config.py @@ -10,7 +10,7 @@ @license: GNU GNU GPL2, see COPYING for details. """ - +import os import ConfigParser from collections import namedtuple @@ -52,7 +52,7 @@ class GKeysConfig(GPGConfig): def _add_gkey_defaults(self): - self.defaults['key-sdir'] = path([self.root, EPREFIX, '/var/gentoo/gkeys']) + self.defaults['keysdir'] = path([self.root, EPREFIX, '/var/gentoo/gkeys']) self.defaults['dev-keydir'] = '%(keysdir)s/devs' self.defaults['release-keydir'] = '%(keysdir)s/release' self.defaults['overlays-keydir'] = '%(keysdir)s/overlays' @@ -70,40 +70,104 @@ class GKeysConfig(GPGConfig): self.defaults['config'] = self.defaults['config'] \ % {'configdir': self.defaults['configdir']} defaults = self.get_defaults() + # remove some defaults from being entered into the configparser + for key in ['gpg_defaults', 'only_usable', 'refetch', 'tasks']: + defaults.pop(key) self.configparser = ConfigParser.ConfigParser(defaults) self.configparser.add_section('MAIN') self.configparser.read(defaults['config']) def get_key(self, key, subkey=None): - return self._get_(key) + return self._get_(key, subkey) - def _get_(self, key): + def _get_(self, key, subkey=None): if self.configparser and self.configparser.has_option('MAIN', key): + logger.debug("Found %s in configparser... %s" + % (key, str(self.configparser.get('MAIN', key)))) + logger.debug("type(key)= %s" + % str(type(self.configparser.get('MAIN', key)))) return self.configparser.get('MAIN', key) - elif key in self.options: - return self.options[key] - elif key in self.defaults: - return self.defaults[key] - logger.error("GKeysConfig: _get_; didn't find :", key) - return None + else: + return super(GKeysConfig, self)._get_(key, subkey) + #elif key in self.options: + #logger.debug("Found %s in options... %s" + #% (key, str(self.options[key]))) + #return self.options[key] + #elif key in self.defaults: + #logger.debug("type(key)= %s" %str(type(self.defaults[key]))) + #logger.debug("Found %s in defaults... %s" + #% (key, str(self.defaults[key]))) + #logger.debug("type(key)= %s" %str(type(self.defaults[key]))) + #return self.defaults[key] + #logger.error("GKeysConfig: _get_; didn't find :", key) + #return None class GKEY(namedtuple('GKEY', ['nick', 'name', 'keyid', 'longkeyid', 'keyring', 'fingerprint'])): '''Class to hold the relavent info about a key''' + field_types = {'nick': str, 'name': str, 'keyid': list, + 'longkeyid': list, 'keyring': str, 'fingerprint': list} + field_separator = "|" + list_separator = ":" __slots__ = () - def values(self): + def _packed_values(self): '''Returns a list of the field values''' v = [] for f in self._fields: - v.append(getattr(self, f)) + v.append(self._pack(f)) return v - def value_string(self, separator=' '): - '''Returns a space separated string of the field values''' - return separator.join([str(x) for x in self.values()]) - + @property + def packed_string(self): + '''Returns a separator joined string of the field values''' + return self.field_separator.join([x for x in self._packed_values()]) + + def _unpack_string(self, packed_data): + '''Returns a list of the separator joined string of the field values''' + values = [] + data = packed_data.split(self.field_separator) + for x in self._fields: + values.append(self._unpack(x, data.pop(0))) + return values + + def _pack(self, field): + '''pack field data into a string''' + if self.field_types[field] == str: + return getattr(self, field) + elif self.field_types[field] == list: + info = getattr(self, field) + if info: + return self.list_separator.join(info) + else: + # force an empty list to None + return 'None' + else: + raise "ERROR packing %s" %str(getattr(self, field)) + + def _unpack(self, field, data): + '''unpack field data to the desired type''' + if self.field_types[field] == str: + result = data + if result == 'None': + result = None + else: + if data == 'None': + # make it an empty list + result = [] + else: + result = data.split(self.list_separator) + return result + + def make_packed(self, packed_string): + '''Creates a new instance of Gkey from the packed + value string + + @param packed_string: string of data separated by field_separator + @return new GKEY instance containing the data + ''' + return self._make(self._unpack_string(packed_string)) diff --git a/gkeys/seed.py b/gkeys/seed.py index 5cfa5fe..ba3614f 100644 --- a/gkeys/seed.py +++ b/gkeys/seed.py @@ -23,7 +23,6 @@ from gkeys.config import GKEY class Seeds(object): '''Handles all seed key file operations''' - separator = '|' def __init__(self, filepath=None): '''Seeds class init function @@ -53,14 +52,13 @@ class Seeds(object): return False for seed in seedlines: - try: - seed = seed.strip('\n') - parts = self._split_seed(seed, self.separator) - self.seeds.append(GKEY._make(parts)) - except Exception as err: - logger.debug("Seed: load; Error splitting seed: %s" % seed) - logger.debug("Seed: load; ...............parts: %s" % str(parts)) - self._error(err) + #try: + seed = seed.strip('\n') + self.seeds.append(GKEY.make_packed(seed)) + #except Exception as err: + #logger.debug("Seed: load; Error splitting seed: %s" % seed) + #logger.debug("Seed: load; ...............parts: %s" % str(parts)) + #self._error(err) logger.debug("Seed: load; Completed loading seed file %s" % self.filename) return True @@ -75,7 +73,7 @@ class Seeds(object): logger.debug("Seed: save; Begin saving seed file %s" % self.filename) try: with open(self.filename, 'w') as seedfile: - seedlines = [x.value_string(self.separator) for x in self.seeds] + seedlines = [x.packed_string for x in self.seeds] seedfile.write('\n'.join(seedlines)) seedfile.write("\n") except IOError as err: @@ -148,14 +146,3 @@ class Seeds(object): logger.error("Seed: Error processing seed file %s" % self.filename) logger.error("Seed: Error was: %s" % str(err)) - - @staticmethod - def _split_seed(seed, separator): - '''Splits the seed string and - replaces all occurances of 'None' with the python type None''' - iterable = seed.split(separator) - for i in range(len(iterable)): - if iterable[i] == 'None': - iterable[i] = None - return iterable -