# HG changeset patch # User Paul Boddie # Date 1384207582 -3600 # Node ID f34896c7ea763eea10bb3bd5d58874310239ef5e # Parent 8d27c74f274e9a9cf9a252bb37a268c75835cdbc Added methods to list key and signature details. Made the key details processing a common method for various operations. diff -r 8d27c74f274e -r f34896c7ea76 MoinMessage.py --- a/MoinMessage.py Mon Nov 11 19:29:29 2013 +0100 +++ b/MoinMessage.py Mon Nov 11 23:06:22 2013 +0100 @@ -358,6 +358,28 @@ text = self.run(["--armor", "--export", keyid]) return MIMEApplication(text, "pgp-keys", encode_noop) + def listKeys(self, keyid=None): + + """ + Return a list of key details for keys on the keychain, selecting only + one specific key if 'keyid' is specified. + """ + + text = self.run(["--list-keys", "--with-colons", "--with-fingerprint"] + + (keyid and ["0x%s" % keyid] or [])) + return self._getKeysFromResult(text) + + def listSignatures(self, keyid=None): + + """ + Return a list of key and signature details for keys on the keychain, + selecting only one specific key if 'keyid' is specified. + """ + + text = self.run(["--list-sigs", "--with-colons", "--with-fingerprint"] + + (keyid and ["0x%s" % keyid] or [])) + return self._getKeysFromResult(text) + def getKeysFromMessagePart(self, part): """ @@ -369,16 +391,27 @@ def getKeysFromString(self, s): - "Return a list of key details extracted from the given string 's'." + """ + Return a list of key details extracted from the given key block string + 's'. + """ text = self.run(["--with-colons", "--with-fingerprint"], s) + return self._getKeysFromResult(text) + + def _getKeysFromResult(self, text): + + """ + Return a list of key details extracted from the given command result + 'text'. + """ + keys = [] - for line in text.split("\n"): try: recordtype, trust, keylength, algorithm, keyid, cdate, expdate, serial, ownertrust, _rest = line.split(":", 9) except ValueError: - break + continue if recordtype == "pub": userid, _rest = _rest.split(":", 1) @@ -386,7 +419,7 @@ "type" : recordtype, "trust" : trust, "keylength" : keylength, "algorithm" : algorithm, "keyid" : keyid, "cdate" : cdate, "expdate" : expdate, "userid" : userid, "ownertrust" : ownertrust, - "fingerprint" : None, "subkeys" : [] + "fingerprint" : None, "subkeys" : [], "signatures" : [] }) elif recordtype == "sub" and keys: keys[-1]["subkeys"].append({ @@ -397,6 +430,12 @@ elif recordtype == "fpr" and keys: fingerprint, _rest = _rest.split(":", 1) keys[-1]["fingerprint"] = fingerprint + elif recordtype == "sig" and keys: + userid, _rest = _rest.split(":", 1) + keys[-1]["signatures"].append({ + "keyid" : keyid, "cdate" : cdate, "expdate" : expdate, + "userid" : userid + }) return keys