commit:     3e35553df4673ed05615adc088fdbbc3f212c876
Author:     Arno Bauernöppel <https://github.com/gronkern>
AuthorDate: Sat Jul  9 03:36:17 2022 +0000
Commit:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
CommitDate: Sat Jul  9 03:39:50 2022 +0000
URL:        https://gitweb.gentoo.org/proj/gentoolkit.git/commit/?id=3e35553d

equery: Add -F TMPL option to depends module

Like in other modules it is now possible to format the
output of the equery module 'depends' with the commandline
switch '-F' and TMPL.

The man page was adujusted.

A new static method 'print_formated' was created.
In method 'format_depend' is checked if the new option is present.
Depending on the check the PackageFormatter is used to display
the dependencies.

manually apply github PR, some minor editing.
Signed-off-by: Brian Dolbec <dolsen <AT> gentoo.org>

 man/equery.1                     |  4 +++
 pym/gentoolkit/equery/depends.py | 76 ++++++++++++++++++++++++++++------------
 2 files changed, 58 insertions(+), 22 deletions(-)

diff --git a/man/equery.1 b/man/equery.1
index ee7ad7b..9cb046f 100644
--- a/man/equery.1
+++ b/man/equery.1
@@ -129,6 +129,10 @@ Include dependencies that are not installed. This can take 
a while.
 .br
 Search for both direct and indirect dependencies.
 .HP
+.B \-F, \-\-format=\fITMPL\fP
+.br
+Customize the output format of the matched packages using the template string 
\fITMPL\fP. See the \fB\-\-format\fP option for \fBlist\fP below for a 
description of the \fITMPL\fP argument.
+.HP
 .BI "\-\-depth=" "NUM"
 .br
 Limit the indirect dependency tree to a depth of \fINUM\fP. \fB\-\-depth=0\fP 
is equivalent to not using \fB\-\-indirect\fP.

diff --git a/pym/gentoolkit/equery/depends.py b/pym/gentoolkit/equery/depends.py
index 581e2b6..93f0ec1 100644
--- a/pym/gentoolkit/equery/depends.py
+++ b/pym/gentoolkit/equery/depends.py
@@ -18,7 +18,7 @@ from gentoolkit.dependencies import Dependencies
 from gentoolkit.equery import format_options, mod_usage, CONFIG
 from gentoolkit.helpers import get_cpvs, get_installed_cpvs
 from gentoolkit.cpv import CPV
-
+from gentoolkit.package import PackageFormatter, FORMAT_TMPL_VARS
 # =======
 # Globals
 # =======
@@ -27,6 +27,7 @@ QUERY_OPTS = {
     "include_masked": False,
     "only_direct": True,
     "max_depth": -1,
+    "package_format": None,
 }
 
 # =======
@@ -61,6 +62,26 @@ class DependPrinter:
 
         pp.uprint(indent + cpv)
 
+    @staticmethod
+    def print_formated(pkg):
+        """Print pkg as formatted output depending on CONFIG."""
+
+        if pkg is None:
+            return
+
+        if CONFIG['verbose']:
+            print (PackageFormatter(
+                pkg,
+                do_format=True,
+                custom_format=QUERY_OPTS["package_format"]
+            ))
+        else:
+            print (PackageFormatter(
+                pkg,
+                do_format=False,
+                custom_format=QUERY_OPTS["package_format"]
+            ))
+
     def format_depend(self, dep, dep_is_displayed):
         """Format a dependency for printing.
 
@@ -76,27 +97,34 @@ class DependPrinter:
         indent = " " * depth
         mdep = dep.matching_dep
         use_conditional = ""
-        if mdep.use_conditional:
-            use_conditional = " & ".join(
-                pp.useflag(u) for u in mdep.use_conditional.split()
-            )
-        if mdep.operator == "=*":
-            formatted_dep = "=%s*" % str(mdep.cpv)
-        else:
-            formatted_dep = mdep.operator + str(mdep.cpv)
-        if mdep.slot:
-            formatted_dep += pp.emph(":") + pp.slot(mdep.slot)
-            if mdep.sub_slot:
-                formatted_dep += pp.slot("/") + pp.slot(mdep.sub_slot)
-        if mdep.use:
-            useflags = pp.useflag(",".join(mdep.use.tokens))
-            formatted_dep += pp.emph("[") + useflags + pp.emph("]")
-
-        if dep_is_displayed:
-            indent = indent + " " * len(str(dep.cpv))
-            self.print_fn(indent, "", use_conditional, formatted_dep)
+
+        if QUERY_OPTS["package_format"] != None:
+            pkg = Package(str(dep.cpv))
+            self.print_formated(pkg)
         else:
             self.print_fn(indent, str(dep.cpv), use_conditional, formatted_dep)
+            if mdep.use_conditional:
+                use_conditional = " & ".join(
+                    pp.useflag(u) for u in mdep.use_conditional.split()
+                )
+            if mdep.operator == '=*':
+                formatted_dep = '=%s*' % str(mdep.cpv)
+            else:
+                formatted_dep = mdep.operator + str(mdep.cpv)
+            if mdep.slot:
+                formatted_dep += pp.emph(':') + pp.slot(mdep.slot)
+                if mdep.sub_slot:
+                    formatted_dep += pp.slot('/') + pp.slot(mdep.sub_slot)
+            if mdep.use:
+                useflags = pp.useflag(','.join(mdep.use.tokens))
+                formatted_dep += (pp.emph('[') + useflags + pp.emph(']'))
+
+            if dep_is_displayed:
+                indent = indent + " " * len(str(dep.cpv))
+                self.print_fn(indent, '', use_conditional, formatted_dep)
+            else:
+                self.print_fn(indent, \
+                    str(dep.cpv), use_conditional, formatted_dep)
 
 
 # =========
@@ -126,6 +154,7 @@ def print_help(with_description=True):
                     "include dependencies that are not installed (slow)",
                 ),
                 (" -D, --indirect", "search both direct and indirect 
dependencies"),
+                (" -F, --format=TMPL", "specify a custom output format"),
                 ("     --depth=N", "limit indirect dependency tree to 
specified depth"),
             )
         )
@@ -145,6 +174,8 @@ def parse_module_options(module_opts):
             QUERY_OPTS["include_masked"] = True
         elif opt in ("-D", "--indirect"):
             QUERY_OPTS["only_direct"] = False
+        elif opt in ('-F', '--format'):
+            QUERY_OPTS["package_format"] = posarg
         elif opt in ("--depth"):
             if posarg.isdigit():
                 depth = int(posarg)
@@ -159,8 +190,9 @@ def parse_module_options(module_opts):
 
 def main(input_args):
     """Parse input and run the program"""
-    short_opts = "hadD"  # -d, --direct was old option for default action
-    long_opts = ("help", "all-packages", "direct", "indirect", "depth=")
+    short_opts = "hadDF:" # -d, --direct was old option for default action
+    long_opts = ('help', 'all-packages', 'direct', 'indirect', \
+                    'format', 'depth=')
 
     try:
         module_opts, queries = gnu_getopt(input_args, short_opts, long_opts)

Reply via email to