[yum-git] cli.py output.py yum/__init__.py

James Antill james at linux.duke.edu
Sat Mar 15 21:55:14 UTC 2008


 cli.py          |   24 ++++++++++++++++++------
 output.py       |   20 ++++++++++++++++----
 yum/__init__.py |   45 ++++++++++++++++++++++++++++++---------------
 3 files changed, 64 insertions(+), 25 deletions(-)

New commits:
commit 5edc7f2491a8ac1c056fbfac406261714ab9ba0e
Author: James Antill <james at and.org>
Date:   Sat Mar 15 17:53:10 2008 -0400

     Add section headers to "yum search" which show which arguments matched.
     Show which arguments didn't match anything.
     Add fmtSection() and fmtKeyValFill() to output.py API.

diff --git a/cli.py b/cli.py
index 431a008..35735bc 100644
--- a/cli.py
+++ b/cli.py
@@ -681,14 +681,26 @@ class YumBaseCli(yum.YumBase, output.YumOutput):
         # display the list of matches
             
         searchlist = ['name', 'summary', 'description', 'url']
-        matching = self.searchGenerator(searchlist, args, showdups=self.conf.showdupesfromrepos)
+        dups = self.conf.showdupesfromrepos
+        matching = self.searchGenerator(searchlist, args,
+                                        showdups=dups, keys=True)
         
-        total = 0
-        for (po, matched_value) in matching:
+        okeys = set()
+        akeys = set()
+        for (po, keys, matched_value) in matching:
+            if keys != okeys:
+                if akeys:
+                    print ""
+                print self.fmtSection("Matched: " + ", ".join(sorted(keys)))
+                okeys = keys
+                akeys.update(keys)
             self.matchcallback(po, matched_value, args)
-            total += 1
-            
-        if total == 0:
+
+        for arg in args:
+            if arg not in akeys:
+                self.logger.warning(_('Warning: No matches found for: %s'), arg)
+
+        if not akeys:
             return 0, [_('No Matches found')]
         return 0, matching
 
diff --git a/output.py b/output.py
index a9ad0f4..1f0d59e 100644
--- a/output.py
+++ b/output.py
@@ -251,7 +251,7 @@ class YumOutput:
         print "%-40.40s %-22.22s %-16.16s" % (na, ver, pkg.repoid)
 
 
-    def _outKeyValFill(self, key, val):
+    def fmtKeyValFill(self, key, val):
         """ Return a key value pair in the common two column output format. """
         keylen = len(key)
         cols = self.term.columns
@@ -264,6 +264,18 @@ class YumOutput:
                        initial_indent=key, subsequent_indent='     ...: ')
         return ret
     
+    def fmtSection(self, name, fill='='):
+        name = str(name)
+        cols = self.term.columns - 2
+        name_len = len(name)
+        if name_len >= (cols - 4):
+            beg = end = fill * 2
+        else:
+            beg = fill * ((cols - name_len) / 2)
+            end = fill * (cols - name_len - len(beg))
+
+        return "%s %s %s" % (beg, name, end)
+
     def infoOutput(self, pkg):
         def enc(s):
             """Get the translated version from specspo and ensure that
@@ -288,11 +300,11 @@ class YumOutput:
         print _("Repo       : %s") % pkg.repoid
         if self.verbose_logger.isEnabledFor(logginglevels.DEBUG_3):
             print _("Committer  : %s") % pkg.committer
-        print self._outKeyValFill(_("Summary    : "), enc(pkg.summary))
+        print self.fmtKeyValFill(_("Summary    : "), enc(pkg.summary))
         if pkg.url:
             print _("URL        : %s") % pkg.url
         print _("License    : %s") % pkg.license
-        print self._outKeyValFill(_("Description: "), enc(pkg.description))
+        print self.fmtKeyValFill(_("Description: "), enc(pkg.description))
         print ""
     
     def updatesObsoletesList(self, uotup, changetype):
@@ -441,7 +453,7 @@ class YumOutput:
             msg = '%s : ' % po
         else:
             msg = '%s.%s : ' % (po.name, po.arch)
-        msg = self._outKeyValFill(msg, po.summary)
+        msg = self.fmtKeyValFill(msg, po.summary)
         if matchfor:
             msg = self.term.sub_bold(msg, matchfor)
         
diff --git a/yum/__init__.py b/yum/__init__.py
index ddb4d59..dd5fa44 100644
--- a/yum/__init__.py
+++ b/yum/__init__.py
@@ -1440,9 +1440,11 @@ class YumBase(depsolve.Depsolve):
         return results
     
     # pre 3.2.10 API used to always showdups, so that's the default atm.
-    def searchGenerator(self, fields, criteria, showdups=True):
+    def searchGenerator(self, fields, criteria, showdups=True, keys=False):
         """Generator method to lighten memory load for some searches.
-           This is the preferred search function to use."""
+           This is the preferred search function to use. Setting keys to True
+           will use the search keys that matched in the sorting, and return
+           the search keys in the results. """
         sql_fields = []
         for f in fields:
             if RPM_TO_SQLITE.has_key(f):
@@ -1460,9 +1462,11 @@ class YumBase(depsolve.Depsolve):
             if s.find('%') == -1:
                 real_crit.append(s)
         real_crit_lower = [] # Take the s.lower()'s out of the loop
+        rcl2c = {}
         for s in criteria:
             if s.find('%') == -1:
                 real_crit_lower.append(s.lower())
+                rcl2c[s.lower()] = s
 
         for sack in self.pkgSack.sacks.values():
             tmpres.extend(sack.searchPrimaryFieldsMultipleStrings(sql_fields, real_crit))
@@ -1470,6 +1474,7 @@ class YumBase(depsolve.Depsolve):
         for (po, count) in tmpres:
             # check the pkg for sanity
             # pop it into the sorted lists
+            tmpkeys   = set()
             tmpvalues = []
             if count not in sorted_lists: sorted_lists[count] = []
             for s in real_crit_lower:
@@ -1477,13 +1482,13 @@ class YumBase(depsolve.Depsolve):
                     value = getattr(po, field)
                     if value and value.lower().find(s) != -1:
                         tmpvalues.append(value)
+                        tmpkeys.add(rcl2c[s])
 
             if len(tmpvalues) > 0:
-                sorted_lists[count].append((po, tmpvalues))
-
-            
+                sorted_lists[count].append((po, tmpkeys, tmpvalues))
         
         for po in self.rpmdb:
+            tmpkeys   = set()
             tmpvalues = []
             criteria_matched = 0
             for s in real_crit_lower:
@@ -1499,23 +1504,33 @@ class YumBase(depsolve.Depsolve):
                             matched_s = True
                         
                         tmpvalues.append(value)
-
+                        tmpkeys.add(rcl2c[s])
 
             if len(tmpvalues) > 0:
                 if criteria_matched not in sorted_lists: sorted_lists[criteria_matched] = []
-                sorted_lists[criteria_matched].append((po, tmpvalues))
-                
+                sorted_lists[criteria_matched].append((po, tmpkeys, tmpvalues))
 
-        # close our rpmdb connection so we can ctrl-c, kthxbai                    
+        # close our rpmdb connection so we can ctrl-c, kthxbai
         self.closeRpmDB()
-        
+
+        # By default just sort using package sorting
+        sort_func = operator.itemgetter(0)
+        if keys:
+            # Take into account the keys found, as well
+            sort_func = lambda x: "%s%s" % ("\0".join(sorted(x[1])), str(x[0]))
         yielded = {}
         for val in reversed(sorted(sorted_lists)):
-            for (po, matched) in sorted(sorted_lists[val], key=operator.itemgetter(0)):
-                if (po.name, po.arch) not in yielded:
-                    yield (po, matched)
-                    if not showdups:
-                        yielded[(po.name, po.arch)] = 1
+            for (po, ks, vs) in sorted(sorted_lists[val], key=sort_func):
+                if not showdups and (po.name, po.arch) in yielded:
+                    continue
+
+                if keys:
+                    yield (po, ks, vs)
+                else:
+                    yield (po, vs)
+
+                if not showdups:
+                    yielded[(po.name, po.arch)] = 1
 
 
     def searchPackages(self, fields, criteria, callback=None):



More information about the Yum-cvs-commits mailing list