From c7c2cd9758351b873324b5faaf06e2a34f39b22d Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Sat, 13 Jul 2013 23:04:26 -0700 Subject: [PATCH] Split out common keypath code to it's own function Add a reset task() to clear/reset additional args. Code list_keys() --- gkeys/lib.py | 58 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/gkeys/lib.py b/gkeys/lib.py index cae7f07..0f7ae5e 100644 --- a/gkeys/lib.py +++ b/gkeys/lib.py @@ -36,6 +36,28 @@ class GkeysGPG(GPG): GPG.__init__(self, config) self.config = config self.keydir = keydir + self.task = None + self.task_value = None + + + def set_keypath(self, keyring, task=None): + logger.debug("keydir: %s, keyring: %s" % (self.keydir, keyring)) + self.task = task + keypath = pjoin(self.keydir, keyring) + # --keyring file | Note that this adds a keyring to the current list. + # If the intent is to use the specified keyring alone, + # use --keyring along with --no-default-keyring. + self.task_value = ['--no-default-keyring', '--keyring', keypath] + task.extend(self.task_value) + return + + + def reset_task(self): + if self.task: + for item in self.task_value: + self.task.remove(item) + self.task = None + self.task_value = None def add_key(self, gkey): @@ -44,14 +66,8 @@ class GkeysGPG(GPG): @param gkey: GKEY namedtuple with (name, keyid/longkeyid, keyring, fingerprint,) ''' - logger.debug("keydir: %s, keyring: %s" % (self.keydir, gkey.keyring)) - keypath = pjoin(self.keydir, gkey.keyring) - # --keyring file | Note that this adds a keyring to the current list. - # If the intent is to use the specified keyring alone, - # use --keyring along with --no-default-keyring. - self.config['tasks']['recv-keys'] = [ - '--no-default-keyring', '--keyring', keypath, - ] + self.set_keypath(gkey.keyring, self.config['tasks']['recv-keys']) + # prefer the longkeyid if available #logger.debug("LIB: add_key; keyids %s, %s" # % (str(gkey.longkeyid), str(gkey.keyid))) @@ -94,13 +110,13 @@ class GkeysGPG(GPG): @param gkey: GKEY namedtuple with (name, keyid/longkeyid, fingerprint) ''' - pass + return [] def del_keyring(self, keyring): '''Delete the specified key to the specified keyring ''' - pass + return [] def update_key(self, gkey, keyring): @@ -109,22 +125,36 @@ class GkeysGPG(GPG): @param key: tuple of (name, keyid, fingerprint) @param keyring: the keyring to add the key to ''' - pass + return [] - def list_keys(self, keyring=None): + def list_keys(self, keyring): '''List all keys in the specified keyring or all key in all keyrings if keyring=None @param keyring: the keyring to add the key to ''' - pass + if not keyring: + logger.debug("LIB: list_keys(), invalid keyring parameter: %s" + % str(keyring)) + return [] + if '--with-colons' in self.config['tasks']['list-keys']: + self.config['tasks']['list-keys'].remove('--with-colons') + + self.set_keypath(keyring, self.config['tasks']['list-keys']) + logger.debug("** Calling runGPG with Running 'gpg %s --list-keys %s'" + % (' '.join(self.config['tasks']['list-keys']), keyring) + ) + result = self.runGPG(task='list-keys', inputfile=keyring) + logger.info('GPG return code: ' + str(result.returncode)) + self.reset_task() + return result def list_keyrings(self): '''List all available keyrings ''' - pass + return [] def verify_key(self, gkey): -- 2.26.2