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 for seed in seedlines:
56 seed = seed.strip('\n')
57 self.seeds.append(GKEY.make_packed(seed))
58 #except Exception as err:
59 #logger.debug("Seed: load; Error splitting seed: %s" % seed)
60 #logger.debug("Seed: load; ...............parts: %s" % str(parts))
62 logger.debug("Seed: load; Completed loading seed file %s" % self.filename)
66 def save(self, filename=None):
67 '''Save the seeds to the file'''
69 self.filename = filename
71 logger.debug("Seed: save; Not a valid filename: '%s'" % str(self.filename))
73 logger.debug("Seed: save; Begin saving seed file %s" % self.filename)
75 with open(self.filename, 'w') as seedfile:
76 seedlines = [x.packed_string for x in self.seeds]
77 seedfile.write('\n'.join(seedlines))
79 except IOError as err:
86 '''Add a new seed key to memory'''
87 if isinstance(gkey, GKEY):
88 self.seeds.append(gkey)
94 def delete(self, gkey=None, index=None):
95 '''Delete the key from the seeds in memory
97 @param gkey: GKEY, the matching GKEY to delete
98 @param index: int, '''
101 self.seeds.remove(gkey)
106 self.seeds.pop(index)
110 def list(self, **kwargs):
111 '''List the key or keys matching the kwargs argument or all
113 @param kwargs: dict of GKEY._fields and values
118 # discard any invalid keys
119 keys = set(list(kwargs)).intersection(GKEY._fields)
120 result = self.seeds[:]
122 result = [x for x in result if getattr(x , key) == kwargs[key]]
126 def search(self, pattern):
127 '''Search for the keys matching the regular expression pattern'''
131 def index(self, gkey):
132 '''The index of the gkey in the seeds list
134 @param gkey: GKEY, the matching GKEY to delete
138 index = self.seeds.index(gkey)
144 def _error(self, err):
145 '''Class error logging function'''
146 logger.error("Seed: Error processing seed file %s" % self.filename)
147 logger.error("Seed: Error was: %s" % str(err))