4 '''Gentoo-keys - seed.py
5 This is gentoo-keys superclass which wraps the pyGPG lib
6 with gentoo-keys specific convienience functions.
8 Distributed under the terms of the GNU General Public License v2
12 Distributed under the terms of the GNU General Public License v2
15 Brian Dolbec <dolsen@gentoo.org>
19 from gkeys.log import logger
20 from gkeys.config import GKEY
24 '''Handles all seed key file operations'''
27 def __init__(self, filepath=None):
28 '''Seeds class init function
30 @param filepath: string of the file to load
32 self.filename = filepath
36 def load(self, filename=None):
37 '''Load the seed file into memory'''
39 self.filename = filename
41 logger.debug("Seed: load; Not a valid filename: '%s'" % str(self.filename))
43 logger.debug("Seeds: load; Begin loading seed file %s" % self.filename)
47 with open(self.filename) as seedfile:
48 seedlines = seedfile.readlines()
49 except IOError as err:
50 logger.debug("Seed: load; IOError occurred while loading file")
54 # initialize a dummy instance, so it can make new ones
55 gkey = GKEY._make([None,None,None,None,None,None])
56 for seed in seedlines:
58 seed = seed.strip('\n')
59 self.seeds.append(gkey.make_packed(seed))
60 #except Exception as err:
61 #logger.debug("Seed: load; Error splitting seed: %s" % seed)
62 #logger.debug("Seed: load; ...............parts: %s" % str(parts))
64 logger.debug("Seed: load; Completed loading seed file %s" % self.filename)
68 def save(self, filename=None):
69 '''Save the seeds to the file'''
71 self.filename = filename
73 logger.debug("Seed: save; Not a valid filename: '%s'" % str(self.filename))
75 logger.debug("Seed: save; Begin saving seed file %s" % self.filename)
77 with open(self.filename, 'w') as seedfile:
78 seedlines = [x.packed_string for x in self.seeds]
79 seedfile.write('\n'.join(seedlines))
81 except IOError as err:
88 '''Add a new seed key to memory'''
89 if isinstance(gkey, GKEY):
90 self.seeds.append(gkey)
95 def delete(self, gkey=None, index=None):
96 '''Delete the key from the seeds in memory
98 @param gkey: GKEY, the matching GKEY to delete
99 @param index: int, '''
102 self.seeds.remove(gkey)
107 self.seeds.pop(index)
111 def list(self, **kwargs):
112 '''List the key or keys matching the kwargs argument or all
114 @param kwargs: dict of GKEY._fields and values
119 if kwargs['nick'] == '*':
121 # proceed with the search
122 # discard any invalid keys
123 keys = set(list(kwargs)).intersection(GKEY._fields)
124 result = self.seeds[:]
126 result = [x for x in result if getattr(x , key) == kwargs[key]]
130 def search(self, pattern):
131 '''Search for the keys matching the regular expression pattern'''
135 def index(self, gkey):
136 '''The index of the gkey in the seeds list
138 @param gkey: GKEY, the matching GKEY to delete
142 index = self.seeds.index(gkey)
148 def _error(self, err):
149 '''Class error logging function'''
150 logger.error("Seed: Error processing seed file %s" % self.filename)
151 logger.error("Seed: Error was: %s" % str(err))