From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 38BAB1381F3 for ; Sat, 6 Jul 2013 19:02:30 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id D5735E0ABF; Sat, 6 Jul 2013 19:02:28 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 3A99CE0AB3 for ; Sat, 6 Jul 2013 19:02:28 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id E969033E9A7 for ; Sat, 6 Jul 2013 19:02:26 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 911D0E468F for ; Sat, 6 Jul 2013 19:02:25 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: <1373134598.cf1a206c19d679e67884633e0dda62454cad88c7.dol-sen@gentoo> Subject: [gentoo-commits] proj/gentoo-keys:master commit in: gkeys/ X-VCS-Repository: proj/gentoo-keys X-VCS-Files: gkeys/config.py gkeys/seed.py X-VCS-Directories: gkeys/ X-VCS-Committer: dol-sen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: cf1a206c19d679e67884633e0dda62454cad88c7 X-VCS-Branch: master Date: Sat, 6 Jul 2013 19:02:25 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: c535c98f-b003-4400-b62b-043af69f3763 X-Archives-Hash: a7611e2294ea96d571e4a3c9b0062259 commit: cf1a206c19d679e67884633e0dda62454cad88c7 Author: Brian Dolbec gentoo org> AuthorDate: Tue Jun 25 15:38:41 2013 +0000 Commit: Brian Dolbec gmail com> CommitDate: Sat Jul 6 18:16:38 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/gentoo-keys.git;a=commit;h=cf1a206c Refactor GKEY calss to handle all packing and unpacking of data in order to handle list type field data. --- gkeys/config.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++---------- gkeys/seed.py | 29 +++++------------ 2 files changed, 88 insertions(+), 37 deletions(-) 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 -