From bc332cc737acfe50572933254a9c646a02fb2291 Mon Sep 17 00:00:00 2001 From: Jochem Berends Date: Fri, 21 Jun 2024 15:31:41 +0200 Subject: [PATCH] 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. --- kecpkg/gpg.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/kecpkg/gpg.py b/kecpkg/gpg.py index 99723ac..d25e977 100644 --- a/kecpkg/gpg.py +++ b/kecpkg/gpg.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import hashlib import logging import os @@ -6,8 +8,10 @@ import sys from datetime import datetime -import gnupg import six +from typing import Any, List, Optional + +from gnupg import GPG from kecpkg.settings import GNUPG_KECPKG_HOME from kecpkg.utils import ( @@ -32,7 +36,7 @@ def hash_of_file(path, algorithm="sha256"): return hash.hexdigest() -__gpg = None # type: gnupg.GPG or None +__gpg: Optional[GPG] = None def has_gpg(): @@ -49,19 +53,17 @@ def has_gpg(): return True -def get_gpg(): - # type: () -> gnupg.GPG - """Return the GPG objects instantiated with custom KECPKG keyring in custom KECPKG GNUPG home.""" +def get_gpg() -> GPG: + """Return the GPG objects with custom KECPKG keyring in custom KECPKG GNUPG home.""" global __gpg if not __gpg: if six.PY2: echo_failure( - "Package signing capability is not available in python 2.7. Please use python 3 or greater." + "Package signing capability is not available in python 2.7. " + "Please use python 3 or greater." ) sys.exit(1) - import gnupg - logging.basicConfig(level=LOGLEVEL) logging.getLogger("gnupg") gpg_bin = "gpg" @@ -100,12 +102,12 @@ def get_gpg(): # create the GNUPG_KECPKG_HOME when not exist, otherwise the GPG will fail ensure_dir_exists(GNUPG_KECPKG_HOME) - __gpg = gnupg.GPG(gpgbinary=gpg_bin, gnupghome=GNUPG_KECPKG_HOME) + __gpg = GPG(gpgbinary=gpg_bin, gnupghome=GNUPG_KECPKG_HOME) return __gpg -def list_keys(gpg): +def list_keys(gpg) -> list[list[str | None | Any]]: """ List all keys from the KECPKG keystore and return it as a list of list. @@ -127,7 +129,7 @@ def list_keys(gpg): return key_list -def tabulate_keys(gpg, explain=False): +def tabulate_keys(gpg: GPG, explain: Optional[bool] = False) -> None: """ List all keys in a table for printing on the CLI. @@ -157,7 +159,7 @@ def tabulate_keys(gpg, explain=False): sys.exit(1) -def parse_key_uids(uids): +def parse_key_uids(uids: str) -> dict[str, str]: """ Parse GPG key uids into a dictionary with Name, Comment and email.