switch seed handling from list to dictionary
authorPavlos Ratis <dastergon@gentoo.org>
Sat, 24 May 2014 17:35:04 +0000 (20:35 +0300)
committerPavlos Ratis <dastergon@gentoo.org>
Sat, 24 May 2014 17:38:48 +0000 (20:38 +0300)
gkeyldap/actions.py
gkeys/seed.py

index 4b28a19918d149ebc44537706571fc4e9765db9a..712f090c2c5117c80f42aa70f806facde1886432 100644 (file)
@@ -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
index ecbbf835f0295b7bcafa47a2c338342569aa4dd4..24748ddd6cb7420babc82cf202d42be77a624230 100644 (file)
@@ -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)