Skip to content

Commit dd01066

Browse files
committed
Fix #250: capture uid info in a uid_map attribute of ScanKeys/ListKeys.
1 parent 846b674 commit dd01066

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Released: Not yet
7979

8080
* Fix #249: Handle fetching GPG version when not the first item in the configuration.
8181

82+
* Fix #250: Capture uid info in a uid_map attribute of ScanKeys/ListKeys.
83+
8284

8385
0.5.4
8486
-----

docs/index.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,67 @@ is a list of the key fingerprints associated with the listed keys.
641641
straight to the key info, whether the fingerprint you have is for a key or a
642642
subkey.
643643

644+
.. versionadded:: 0.5.5
645+
The returned value from :meth:`~gnupg.GPG.list_keys` now has a new
646+
attribute, ``uid_map``, which is a dictionary mapping uids to dicts with
647+
detailed information about the corresponding uid. The keys of the information
648+
provided are listed in the table above. Refer to the `GnuPG documentation
649+
<https://github.com/gpg/gnupg/blob/master/doc/DETAILS>`_ for more information.
650+
651+
You could use this information as in the following example:
652+
653+
.. code-block:: pycon
654+
655+
>>> from pprint import pprint
656+
>>> keys = gpg.list_keys()
657+
>>> pprint(keys.uids)
658+
['Andrew Able (A test user) <[email protected]>',
659+
'Barb Bruin <[email protected]>',
660+
'Babs Broon <[email protected]>',
661+
'Barbara Brown (A test user) <[email protected]>',
662+
'Charlie Clark (A test user) <[email protected]>',
663+
'Donna Davis (A test user) <[email protected]>']
664+
>>> pprint(keys.uid_map('Barbara Brown (A test user) <[email protected]>')
665+
{'algo': '',
666+
'date': '1739485458',
667+
'dummy': '8B989767967370B894C53279A3BDF655F00CD4DE',
668+
'expires': '',
669+
'keyid': '',
670+
'length': '',
671+
'ownertrust': '',
672+
'sig': '',
673+
'trust': 'u',
674+
'type': 'uid',
675+
'uid': 'Barbara Brown (A test user) <[email protected]>'}
676+
>>> pprint(keys.uid_map['Barb Bruin <[email protected]>'])
677+
{'algo': '',
678+
'date': '1739485886',
679+
'dummy': '951261047308BCA0B45FD738AD8630B336B88ECF',
680+
'expires': '',
681+
'keyid': '',
682+
'length': '',
683+
'ownertrust': '',
684+
'sig': '',
685+
'trust': 'u',
686+
'type': 'uid',
687+
'uid': 'Barb Bruin <[email protected]>'}
688+
>>> pprint(keys.uid_map['Babs Broon <[email protected]>'])
689+
{'algo': '',
690+
'date': '',
691+
'dummy': '2BDB74660AC54DF33DE523429386E2D460904E74',
692+
'expires': '',
693+
'keyid': '',
694+
'length': '',
695+
'ownertrust': '',
696+
'sig': '',
697+
'trust': 'r',
698+
'type': 'uid',
699+
'uid': 'Babs Broon <[email protected]>'}
700+
>>>
701+
702+
The first two of these dictionaries show normal uids (trust is 'u', for ultimate), whereas the third
703+
shows a revoked uid (trust is 'r', for revoked).
704+
644705
.. versionadded:: 0.3.8
645706
You can also list a subset of keys by specifying a ``keys=`` keyword
646707
argument to :meth:`~gnupg.GPG.list_keys` whose value is either a single

gnupg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ def __init__(self, gpg):
569569
self.curkey = None
570570
self.fingerprints = []
571571
self.uids = []
572+
self.uid_map = {}
572573

573574
def get_fields(self, args):
574575
"""
@@ -597,6 +598,10 @@ def uid(self, args):
597598
uid = uid.replace(k, v)
598599
self.curkey['uids'].append(uid)
599600
self.uids.append(uid)
601+
uid_data = {}
602+
self.uid_map[uid] = uid_data
603+
for fn, fv in zip(self.FIELDS, args):
604+
uid_data[fn] = fv
600605

601606
def handle_status(self, key, value): # pragma: no cover
602607
pass

0 commit comments

Comments
 (0)