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'''
28 def __init__(self, filepath=None):
29 '''Seeds class init function
31 @param filepath: string of the file to load
33 self.filename = filepath
37 def load(self, filename=None):
38 '''Load the seed file into memory'''
40 self.filename = filename
42 logger.debug("Seed.load() Not a valid filename: '%s'" % str(self.filename))
44 logger.debug("Seeds: Begin loading seed file %s" % self.filename)
48 with open(self.filename) as seedfile:
49 seedlines = seedfile.readlines()
50 except IOError as err:
54 for seed in seedlines:
56 parts = self._split_seed(seed)
57 self.seeds.append(GKEY._make(parts))
58 except Exception as err:
60 logger.debug("Completed loading seed file %s" % self.filename)
64 def save(self, filename=None):
65 '''Save the seeds to the file'''
67 self.filename = filename
69 logger.debug("Seed.load() Not a valid filename: '%s'" % str(self.filename))
71 logger.debug("Begin saving seed file %s" % self.filename)
73 with open(self.filename, 'w') as seedfile:
74 seedlines = [x.value_string(self.separator) for x in self.seeds]
75 seedfile.write('\n'.join(seedlines))
77 except IOError as err:
84 '''Add a new seed key to memory'''
85 if isinstance(gkey, GKEY):
86 self.seeds.append(gkey)
92 def delete(self, gkey=None, index=None):
93 '''Delete the key from the seeds in memory
95 @param gkey: GKEY, the matching GKEY to delete
96 @param index: int, '''
99 self.seeds.remove(gkey)
104 self.seeds.pop(index)
108 def list(self, **kwargs):
109 '''List the key or keys matching the kwargs argument or all
111 @param kwargs: dict of GKEY._fields and values
116 # discard any invalid keys
117 keys = set(list(kwargs)).intersection(GKEY._fields)
118 result = self.seeds[:]
120 result = [x for x in result if getattr(x , key) == kwargs[key]]
124 def search(self, pattern):
125 '''Search for the keys matching the regular expression pattern'''
129 def index(self, gkey):
130 '''The index of the gkey in the seeds list
132 @param gkey: GKEY, the matching GKEY to delete
136 index = self.seeds.index(gkey)
142 def _error(self, err):
143 '''Class error logging function'''
144 logger.error("Error processing seed file %s" % self.filename)
145 logger.error("Error was: %s" % str(err))
149 def _split_seed(seed):
150 '''Splits the seed string and
151 replaces all occurances of 'None' with the python type None'''
152 iterable = seed.split(self.separator)
153 for i in range(len(iterable)):
154 if iterable[i] == 'None':