commit:     272b9fabe7bfba705c6bfbbdfff901535f1d305b
Author:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
AuthorDate: Fri Dec 22 13:27:16 2023 +0000
Commit:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Fri Dec 22 13:27:16 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/pkgdev.git/commit/?id=272b9fab

bugs: support ~/.bugzrc fir api-key extraction

Resolves: https://github.com/pkgcore/pkgdev/issues/162
Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org>

 src/pkgdev/scripts/argparsers.py  | 39 +++++++++++++++++++++++++++++++++++++++
 src/pkgdev/scripts/pkgdev_bugs.py | 18 ++----------------
 src/pkgdev/scripts/pkgdev_tatt.py | 12 ++----------
 3 files changed, 43 insertions(+), 26 deletions(-)

diff --git a/src/pkgdev/scripts/argparsers.py b/src/pkgdev/scripts/argparsers.py
index ac1a758..76479ff 100644
--- a/src/pkgdev/scripts/argparsers.py
+++ b/src/pkgdev/scripts/argparsers.py
@@ -1,5 +1,7 @@
 import os
 import subprocess
+from configparser import ConfigParser
+from pathlib import Path
 
 from pkgcore.repository import errors as repo_errors
 from snakeoil.cli.arghparse import ArgumentParser
@@ -41,3 +43,40 @@ def _determine_git_repo(parser, namespace):
         pass
 
     namespace.git_repo = path
+
+
+class BugzillaApiKey:
+    @classmethod
+    def mangle_argparser(cls, parser):
+        parser.add_argument(
+            "--api-key",
+            metavar="KEY",
+            help="Bugzilla API key",
+            docs="""
+                The Bugzilla API key to use for authentication. WARNING: using 
this
+                option will expose your API key to other users of the same 
system.
+                Consider instead saving your API key in a file named 
``~/.bugzrc``
+                in an INI format like so::
+
+                        [default]
+                        key = <your API key>
+
+                ANother supported option is to save your API key in a file 
named
+                ``~/.bugz_token``.
+            """,
+        )
+
+        parser.bind_delayed_default(1000, "api_key")(cls._default_api_key)
+
+    @staticmethod
+    def _default_api_key(namespace, attr):
+        """Use all known arches by default."""
+        if (bugz_rc_file := Path.home() / ".bugzrc").is_file():
+            try:
+                config = ConfigParser(default_section="default")
+                config.read(bugz_rc_file)
+                setattr(namespace, attr, config.get("default", "key"))
+            except Exception as e:
+                raise ValueError(f"failed parsing {bugz_rc_file}: {e}")
+        elif (bugz_token_file := Path.home() / ".bugz_token").is_file():
+            setattr(namespace, attr, bugz_token_file.read_text().strip())

diff --git a/src/pkgdev/scripts/pkgdev_bugs.py 
b/src/pkgdev/scripts/pkgdev_bugs.py
index 5d3672c..7c11d31 100644
--- a/src/pkgdev/scripts/pkgdev_bugs.py
+++ b/src/pkgdev/scripts/pkgdev_bugs.py
@@ -30,7 +30,7 @@ from snakeoil.cli.input import userquery
 from snakeoil.formatters import Formatter
 
 from ..cli import ArgumentParser
-from .argparsers import _determine_cwd_repo, cwd_repo_argparser
+from .argparsers import _determine_cwd_repo, cwd_repo_argparser, BugzillaApiKey
 
 bugs = ArgumentParser(
     prog="pkgdev bugs",
@@ -39,16 +39,7 @@ bugs = ArgumentParser(
     quiet=False,
     parents=(cwd_repo_argparser,),
 )
-bugs.add_argument(
-    "--api-key",
-    metavar="KEY",
-    help="Bugzilla API key",
-    docs="""
-        The Bugzilla API key to use for authentication. WARNING: using this
-        option will expose your API key to other users of the same system.
-        Consider instead saving your API key in a file named ~/.bugz_token.
-    """,
-)
+BugzillaApiKey.mangle_argparser(bugs)
 bugs.add_argument(
     "targets",
     metavar="target",
@@ -572,11 +563,6 @@ def main(options, out: Formatter, err: Formatter):
     for node in d.nodes:
         node.cleanup_keywords(search_repo)
 
-    if options.api_key is None:
-        bugz_token_file = Path.home() / ".bugz_token"
-        if bugz_token_file.is_file:
-            options.api_key = bugz_token_file.read_text().strip()
-
     if not d.nodes:
         out.write(out.fg("red"), "Nothing to do, exiting", out.reset)
         return 1

diff --git a/src/pkgdev/scripts/pkgdev_tatt.py 
b/src/pkgdev/scripts/pkgdev_tatt.py
index 37454fc..79624cd 100644
--- a/src/pkgdev/scripts/pkgdev_tatt.py
+++ b/src/pkgdev/scripts/pkgdev_tatt.py
@@ -15,18 +15,10 @@ from pkgcore.util import packages as pkgutils
 from snakeoil.cli import arghparse
 
 from ..cli import ArgumentParser
+from .argparsers import BugzillaApiKey
 
 tatt = ArgumentParser(prog="pkgdev tatt", description=__doc__, verbose=False, 
quiet=False)
-tatt.add_argument(
-    "--api-key",
-    metavar="KEY",
-    help="Bugzilla API key",
-    docs="""
-        The Bugzilla API key to use for authentication. Used mainly to overcome
-        rate limiting done by bugzilla server. This tool doesn't perform any
-        bug editing, just fetching info for the bug.
-    """,
-)
+BugzillaApiKey.mangle_argparser(tatt)
 tatt.add_argument(
     "-j",
     "--job-name",

Reply via email to