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'''
26 def __init__(self, filepath=None):
27 '''Seeds class init function
29 @param filepath: string of the file to load
31 self.filename = filepath
35 def load(self, filename=None):
36 '''Load the seed file into memory'''
38 self.filename = filename
40 logger.debug("Seed.load() Not a valid filename: '%s'" % str(self.filename))
42 logger.debug("Seeds: Begin loading seed file %s" % self.filename)
46 with open(self.filename) as seedfile:
47 seedlines = seedfile.readlines()
48 except IOError as err:
52 for seed in seedlines:
54 parts = self._split_seed(seed)
55 self.seeds.append(GKEY._make(parts))
56 except Exception as err:
58 logger.debug("Completed loading seed file %s" % self.filename)
62 def save(self, filename=None):
63 '''Save the seeds to the file'''
65 self.filename = filename
67 logger.debug("Seed.load() Not a valid filename: '%s'" % str(self.filename))
69 logger.debug("Begin saving seed file %s" % self.filename)
71 with open(self.filename, 'w') as seedfile:
72 seedlines = [x.value_string() for x in self.seeds]
73 seedfile.write('\n'.join(seedlines))
75 except IOError as err:
82 '''Add a new seed key to memory'''
83 if isinstance(gkey, GKEY):
84 self.seeds.append(gkey)
90 def delete(self, gkey=None, index=None):
91 '''Delete the key from the seeds in memory
93 @param gkey: GKEY, the matching GKEY to delete
94 @param index: int, '''
97 self.seeds.remove(gkey)
102 self.seeds.pop(index)
106 def list(self, **kwargs):
107 '''List the key or keys matching the kwargs argument or all
109 @param kwargs: dict of GKEY._fields and values
114 # discard any invalid keys
115 keys = set(list(kwargs)).intersection(GKEY._fields)
116 result = self.seeds[:]
118 result = [x for x in result if getattr(x , key) == kwargs[key]]
122 def search(self, pattern):
123 '''Search for the keys matching the regular expression pattern'''
127 def index(self, gkey):
128 '''The index of the gkey in the seeds list
130 @param gkey: GKEY, the matching GKEY to delete
134 index = self.seeds.index(gkey)
140 def _error(self, err):
141 '''Class error logging function'''
142 logger.error("Error processing seed file %s" % self.filename)
143 logger.error("Error was: %s" % str(err))
147 def _split_seed(seed):
148 '''Splits the seed string and
149 replaces all occurances of 'None' with the python type None'''
150 iterable = seed.split()
151 for i in range(len(iterable)):
152 if iterable[i] == 'None':