Skip to content

Commit bc332cc

Browse files
author
Jochem Berends
committed
Refactor gpg.py for better type annotations and code readability
__Import__ and __output__ type annotations have been added for better function clarity and maintainability. The importation of 'gnupg.GPG' was refined for consistency throughout the module. Some multi-line string statements were also revised for readability and coherence purposes.
1 parent 176ab46 commit bc332cc

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

kecpkg/gpg.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import hashlib
24
import logging
35
import os
@@ -6,8 +8,10 @@
68
import sys
79
from datetime import datetime
810

9-
import gnupg
1011
import six
12+
from typing import Any, List, Optional
13+
14+
from gnupg import GPG
1115

1216
from kecpkg.settings import GNUPG_KECPKG_HOME
1317
from kecpkg.utils import (
@@ -32,7 +36,7 @@ def hash_of_file(path, algorithm="sha256"):
3236
return hash.hexdigest()
3337

3438

35-
__gpg = None # type: gnupg.GPG or None
39+
__gpg: Optional[GPG] = None
3640

3741

3842
def has_gpg():
@@ -49,19 +53,17 @@ def has_gpg():
4953
return True
5054

5155

52-
def get_gpg():
53-
# type: () -> gnupg.GPG
54-
"""Return the GPG objects instantiated with custom KECPKG keyring in custom KECPKG GNUPG home."""
56+
def get_gpg() -> GPG:
57+
"""Return the GPG objects with custom KECPKG keyring in custom KECPKG GNUPG home."""
5558
global __gpg
5659
if not __gpg:
5760
if six.PY2:
5861
echo_failure(
59-
"Package signing capability is not available in python 2.7. Please use python 3 or greater."
62+
"Package signing capability is not available in python 2.7. "
63+
"Please use python 3 or greater."
6064
)
6165
sys.exit(1)
6266

63-
import gnupg
64-
6567
logging.basicConfig(level=LOGLEVEL)
6668
logging.getLogger("gnupg")
6769
gpg_bin = "gpg"
@@ -100,12 +102,12 @@ def get_gpg():
100102
# create the GNUPG_KECPKG_HOME when not exist, otherwise the GPG will fail
101103
ensure_dir_exists(GNUPG_KECPKG_HOME)
102104

103-
__gpg = gnupg.GPG(gpgbinary=gpg_bin, gnupghome=GNUPG_KECPKG_HOME)
105+
__gpg = GPG(gpgbinary=gpg_bin, gnupghome=GNUPG_KECPKG_HOME)
104106

105107
return __gpg
106108

107109

108-
def list_keys(gpg):
110+
def list_keys(gpg) -> list[list[str | None | Any]]:
109111
"""
110112
List all keys from the KECPKG keystore and return it as a list of list.
111113
@@ -127,7 +129,7 @@ def list_keys(gpg):
127129
return key_list
128130

129131

130-
def tabulate_keys(gpg, explain=False):
132+
def tabulate_keys(gpg: GPG, explain: Optional[bool] = False) -> None:
131133
"""
132134
List all keys in a table for printing on the CLI.
133135
@@ -157,7 +159,7 @@ def tabulate_keys(gpg, explain=False):
157159
sys.exit(1)
158160

159161

160-
def parse_key_uids(uids):
162+
def parse_key_uids(uids: str) -> dict[str, str]:
161163
"""
162164
Parse GPG key uids into a dictionary with Name, Comment and email.
163165

0 commit comments

Comments
 (0)