From 2fde2e6a825d0aad0992eccf3e0b4e69e1a68d23 Mon Sep 17 00:00:00 2001 From: Pavlos Ratis Date: Sat, 24 May 2014 20:35:04 +0300 Subject: [PATCH] switch seed handling from list to dictionary --- gkeyldap/actions.py | 4 +--- gkeys/seed.py | 39 ++++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/gkeyldap/actions.py b/gkeyldap/actions.py index 4b28a19..712f090 100644 --- a/gkeyldap/actions.py +++ b/gkeyldap/actions.py @@ -118,7 +118,6 @@ class Actions(object): self.seeds = Seeds(filename) count = 0 error_count = 0 - gkeyattr_dev = defaultdict() for dev in sorted(devs): if devs[dev]['gentooStatus'][0] not in ['active']: continue @@ -126,8 +125,7 @@ class Actions(object): # "%s, %s" % (str(dev), str(devs[dev]))) developer_attrs = self.build_gkeydict(devs[dev]) if developer_attrs: - gkeyattr_dev[dev] = developer_attrs - self.seeds.add(gkeyattr_dev) + self.seeds.add(dev, developer_attrs) count += 1 else: error_count += 1 diff --git a/gkeys/seed.py b/gkeys/seed.py index ecbbf83..24748dd 100644 --- a/gkeys/seed.py +++ b/gkeys/seed.py @@ -17,7 +17,6 @@ with gentoo-keys specific convienience functions. ''' import json -from collections import defaultdict from gkeys.log import logger from gkeys.config import GKEY @@ -32,7 +31,7 @@ class Seeds(object): @param filepath: string of the file to load ''' self.filename = filepath - self.seeds = [] + self.seeds = {} def load(self, filename=None): @@ -44,7 +43,7 @@ class Seeds(object): return False logger.debug("Seeds: load; Begin loading seed file %s" % self.filename) seedlines = None - self.seeds = [] + self.seeds = {} try: with open(self.filename) as seedfile: seedlines = json.load(seedfile) @@ -54,7 +53,7 @@ class Seeds(object): return False for seed in seedlines.items(): #try: - self.seeds.append(GKEY(**seed[1])) + self.seeds[seed[0]] = GKEY(**seed[1]) #except Exception as err: #logger.debug("Seed: load; Error splitting seed: %s" % seed) #logger.debug("Seed: load; ...............parts: %s" % str(parts)) @@ -81,13 +80,11 @@ class Seeds(object): return True - def add(self, gkey): + def add(self, dev, gkey): '''Add a new seed key to memory''' - if isinstance(gkey, defaultdict): - self.seeds.append(gkey) + if isinstance(gkey, dict) or isinstance(gkey, GKEY): + self.seeds[dev] = gkey return True - elif isinstance(gkey, GKEY): - pass return False @@ -98,7 +95,7 @@ class Seeds(object): @param index: int, ''' if gkey: try: - self.seeds.remove(gkey) + self.seeds.pop(getattr(gkey[0], 'nick')[0], None) except ValueError: return False return True @@ -113,17 +110,15 @@ class Seeds(object): @param kwargs: dict of GKEY._fields and values @returns list ''' - if not kwargs: - return self.seeds - if kwargs['nick'] == '*': - return self.seeds[:] + if not kwargs or kwargs['nick'] == '*': + return self.seeds.values()[0] # proceed with the search # discard any invalid keys - keys = set(list(kwargs)).intersection(GKEY._fields) - result = self.seeds[:] + keys = kwargs + result = self.seeds for key in keys: - result = [x for x in result if getattr(x , key) == kwargs[key]] - return result + result = {dev: gkey for dev, gkey in result.items() if kwargs[key] in getattr(gkey, key)} + return result.values() def search(self, pattern): @@ -151,4 +146,10 @@ class Seeds(object): def _seeds2json(self, seeds): - return json.dumps(self.seeds[0], sort_keys=True, indent=4) + is_gkey=False + if isinstance(seeds.values()[0], GKEY): + is_gkey = True + for dev, value in seeds.items(): + if is_gkey: + seeds[dev] = dict(value._asdict()) + return json.dumps(seeds, sort_keys=True, indent=4) -- 2.26.2