commit: aa55f5b73d5a551494c51a1ca6f9beed13d9c17d Author: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> AuthorDate: Mon Jan 20 17:50:25 2025 +0000 Commit: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> CommitDate: Mon Jan 20 17:57:38 2025 +0000 URL: https://gitweb.gentoo.org/proj/pkgcore/pkgcore.git/commit/?id=aa55f5b7
pquery: add support for --format custom formatting Resolves: https://github.com/pkgcore/pkgcore/issues/168 Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org> src/pkgcore/scripts/pquery.py | 38 ++++++++++++++++++++++++++++++++++++-- tests/scripts/test_pquery.py | 6 ++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/pkgcore/scripts/pquery.py b/src/pkgcore/scripts/pquery.py index 46311121..ff04a95c 100644 --- a/src/pkgcore/scripts/pquery.py +++ b/src/pkgcore/scripts/pquery.py @@ -67,7 +67,7 @@ class DataSourceRestriction(values.base): dep_attrs = ["bdepend", "depend", "rdepend", "pdepend", "idepend"] metadata_attrs = dep_attrs -dep_attrs += list(f"raw_{x}" for x in dep_attrs) +dep_attrs += [f"raw_{x}" for x in dep_attrs] dep_formatted_attrs = dep_attrs + ["restrict"] dep_formatted_attrs = frozenset(dep_attrs + ["restrict"]) dep_attrs = tuple(sorted(dep_attrs)) @@ -118,7 +118,7 @@ printable_attrs += ( printable_attrs = tuple(sorted(set(printable_attrs))) -def stringify_attr(config, pkg, attr): +def stringify_attr(config, pkg, attr: str): """Grab a package attr and convert it to a string.""" # config is currently unused but may affect display in the future. if attr in ("files", "uris"): @@ -170,6 +170,18 @@ def stringify_attr(config, pkg, attr): return str(value) +class PrintAttrDict(dict): + def __init__(self, pkg, config): + super().__init__() + self.__pkg = pkg + self.__config = config + + def __missing__(self, key): + if key not in printable_attrs: + argparser.error(f"unknown attribute: {key}") + return stringify_attr(self.__config, self.__pkg, key) + + def _default_formatter(out, node): out.write(node, autoline=False) return False @@ -367,6 +379,17 @@ def print_package(options, out, err, pkg): out.write() out.later_prefix = [] out.wrap = False + elif options.format: + if options.atom: + out.write("=", autoline=False) + if options.atom or options.cpv: + out.write(pkg.cpvstr, autoline=False) + if options.display_slot: + out.write(":", pkg.slot, autoline=False) + if options.display_repo: + out.write("::", pkg.repo.repo_id, autoline=False) + out.write("|", autoline=False) + out.write(options.format.format_map(PrintAttrDict(pkg, options))) elif options.one_attr: if options.atom: out.write("=", autoline=False) @@ -1105,6 +1128,17 @@ output.add_argument( help="print what condition(s) trigger a dep", ) +output.add_argument( + "-F", + "--format", + metavar="format", + help="print this format filled with package attributes, suppresses other output", + docs=f""" + Use the given format string to print package attributes. The format + uses standard Python format string syntax (see :ref:`formatspec` for + details), with the all attributes of ``--attr``. + """, +) output.add_argument( "--attr", action="append", diff --git a/tests/scripts/test_pquery.py b/tests/scripts/test_pquery.py index c36e9f7c..edf19b9d 100644 --- a/tests/scripts/test_pquery.py +++ b/tests/scripts/test_pquery.py @@ -91,6 +91,12 @@ class TestCommandline(ArgParseMixin): "--all", test_domain=simple_repo_config, ) + self.assertOut( + ["abc/def-2 (MISSING)"], + "--format", + "{category}/{package}-{version} ({repo})", + test_domain=simple_repo_config, + ) def test_atom(self): config = self.parse("--print-revdep", "a/spork", "--all", domain=domain_config)
