Refactor merge list attribute display.
authorZac Medico <zmedico@gentoo.org>
Fri, 14 Sep 2012 01:03:54 +0000 (18:03 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 14 Sep 2012 01:03:54 +0000 (18:03 -0700)
This should not change the behavior at all, but it makes the display
much easier to modify or extend.

pym/_emerge/resolver/output.py
pym/_emerge/resolver/output_helpers.py

index 61cfe9e980dca900f286ddb07cf490eddac701a6..11e668bc9a6ded0127f3a4e0634c52deeb9a05e3 100644 (file)
@@ -17,7 +17,7 @@ from portage.dep import cpvequal, _repo_separator
 from portage.exception import InvalidDependString, SignatureException
 from portage.package.ebuild._spawn_nofetch import spawn_nofetch
 from portage.output import ( blue, colorize, create_color_func,
-       darkblue, darkgreen, green, nc_len, red, teal, turquoise, yellow )
+       darkblue, darkgreen, green, nc_len, teal)
 bad = create_color_func("BAD")
 from portage.util import writemsg_stdout
 from portage.versions import best, catpkgsplit
@@ -392,26 +392,9 @@ class Display(object):
                        myoldbest_str = blue("["+", ".join(versions)+"]")
                return myoldbest_str
 
-
-       def set_interactive(self, pkg, ordered, addl):
-               """Increments counters.interactive if the pkg is to
-               be merged and it's metadata has interactive set True
-
-               @param pkg: _emerge.Package.Package instance
-               @param ordered: boolean
-               @param addl: already defined string to add to
-               """
-               if 'interactive' in pkg.metadata.properties and \
-                       pkg.operation == 'merge':
-                       addl = colorize("WARN", "I") + addl[1:]
-                       if ordered:
-                               self.counters.interactive += 1
-               return addl
-
-       def _set_non_root_columns(self, addl, pkg_info, pkg):
+       def _set_non_root_columns(self, pkg_info, pkg):
                """sets the indent level and formats the output
 
-               @param addl: already defined string to add to
                @param pkg_info: dictionary
                @param pkg: _emerge.Package.Package instance
                @rtype string
@@ -421,7 +404,7 @@ class Display(object):
                        any(x.repo != self.portdb.repositories.mainRepo().name for x in pkg_info.oldbest_list + [pkg])):
                        ver_str += _repo_separator + pkg.repo
                if self.conf.quiet:
-                       myprint = addl + " " + self.indent + \
+                       myprint = str(pkg_info.attr_display) + " " + self.indent + \
                                self.pkgprint(pkg_info.cp, pkg_info)
                        myprint = myprint+darkblue(" "+ver_str)+" "
                        myprint = myprint+pkg_info.oldbest
@@ -434,7 +417,8 @@ class Display(object):
                                        self.indent, self.pkgprint(pkg.cp, pkg_info))
                        else:
                                myprint = "[%s %s] %s%s" % \
-                                       (self.pkgprint(pkg.type_name, pkg_info), addl,
+                                       (self.pkgprint(pkg.type_name, pkg_info),
+                                       pkg_info.attr_display,
                                        self.indent, self.pkgprint(pkg.cp, pkg_info))
                        if (self.newlp-nc_len(myprint)) > 0:
                                myprint = myprint+(" "*(self.newlp-nc_len(myprint)))
@@ -446,10 +430,9 @@ class Display(object):
                return myprint
 
 
-       def _set_root_columns(self, addl, pkg_info, pkg):
+       def _set_root_columns(self, pkg_info, pkg):
                """sets the indent level and formats the output
 
-               @param addl: already defined string to add to
                @param pkg_info: dictionary
                @param pkg: _emerge.Package.Package instance
                @rtype string
@@ -460,7 +443,7 @@ class Display(object):
                        any(x.repo != self.portdb.repositories.mainRepo().name for x in pkg_info.oldbest_list + [pkg])):
                        ver_str += _repo_separator + pkg.repo
                if self.conf.quiet:
-                       myprint = addl + " " + self.indent + \
+                       myprint = str(pkg_info.attr_display) + " " + self.indent + \
                                self.pkgprint(pkg_info.cp, pkg_info)
                        myprint = myprint+" "+green(ver_str)+" "
                        myprint = myprint+pkg_info.oldbest
@@ -473,7 +456,8 @@ class Display(object):
                                        addl, self.indent, self.pkgprint(pkg.cp, pkg_info))
                        else:
                                myprint = "[%s %s] %s%s" % \
-                                       (self.pkgprint(pkg.type_name, pkg_info), addl,
+                                       (self.pkgprint(pkg.type_name, pkg_info),
+                                       pkg_info.attr_display,
                                        self.indent, self.pkgprint(pkg.cp, pkg_info))
                        if (self.newlp-nc_len(myprint)) > 0:
                                myprint = myprint+(" "*(self.newlp-nc_len(myprint)))
@@ -484,12 +468,11 @@ class Display(object):
                return myprint
 
 
-       def _set_no_columns(self, pkg, pkg_info, addl):
+       def _set_no_columns(self, pkg, pkg_info):
                """prints pkg info without column indentation.
 
                @param pkg: _emerge.Package.Package instance
                @param pkg_info: dictionary
-               @param addl: the current text to add for the next line to output
                @rtype the updated addl
                """
                pkg_str = pkg.cpv
@@ -506,46 +489,10 @@ class Display(object):
                else:
                        myprint = "[%s %s] %s%s %s" % \
                                (self.pkgprint(pkg.type_name, pkg_info),
-                               addl, self.indent,
+                               pkg_info.attr_display, self.indent,
                                self.pkgprint(pkg_str, pkg_info), pkg_info.oldbest)
                return myprint
 
-
-       def _insert_slot(self, pkg, pkg_info, myinslotlist):
-               """Adds slot info to the message
-
-               @return addl: formatted slot info
-               @return myoldbest: installed version list
-               Modifies self.counters.downgrades, self.counters.upgrades
-               """
-               addl = "   " + pkg_info.fetch_symbol
-               if not cpvequal(pkg.cpv,
-                       best([pkg.cpv] + [x.cpv for x in myinslotlist])):
-                       # Downgrade in slot
-                       addl += turquoise("U")+blue("D")
-                       if pkg_info.ordered:
-                               self.counters.downgrades += 1
-               else:
-                       # Update in slot
-                       addl += turquoise("U") + " "
-                       if pkg_info.ordered:
-                               self.counters.upgrades += 1
-               return addl
-
-
-       def _new_slot(self, pkg, pkg_info):
-               """New slot, mark it new.
-
-               @return addl: formatted slot info
-               @return myoldbest: installed version list
-               Modifies self.counters.newslot
-               """
-               addl = " " + green("NS") + pkg_info.fetch_symbol + "  "
-               if pkg_info.ordered:
-                       self.counters.newslot += 1
-               return addl
-
-
        def print_messages(self, show_repos):
                """Performs the actual output printing of the pre-formatted
                messages
@@ -636,7 +583,6 @@ class Display(object):
                """
                pkg_info = PkgInfo()
                pkg_info.ordered = ordered
-               pkg_info.fetch_symbol = " "
                pkg_info.operation = pkg.operation
                pkg_info.merge = ordered and pkg_info.operation == "merge"
                if not pkg_info.merge and pkg_info.operation == "merge":
@@ -666,13 +612,13 @@ class Display(object):
                        'fetch' in pkg.metadata.restrict:
                        if pkg_info.ordered:
                                self.counters.restrict_fetch += 1
+                       pkg_info.attr_display.fetch_restrict = True
                        if not self.portdb.getfetchsizes(pkg.cpv,
                                useflags=pkg_info.use, myrepo=pkg.repo):
-                               pkg_info.fetch_symbol = green("f")
+                               pkg_info.attr_display.fetch_restrict_satisfied = True
                                if pkg_info.ordered:
                                        self.counters.restrict_fetch_satisfied += 1
                        else:
-                               pkg_info.fetch_symbol = red("F")
                                if pkg_info.ebuild_path is not None:
                                        self.restrict_fetch_list[pkg] = pkg_info
                return pkg_info
@@ -757,7 +703,7 @@ class Display(object):
                myinslotlist = None
                installed_versions = self.vardb.match_pkgs(pkg.cp)
                if self.vardb.cpv_exists(pkg.cpv):
-                       addl = "  "+yellow("R")+pkg_info.fetch_symbol+"  "
+                       pkg_info.attr_display.replace = True
                        installed_version = self.vardb.match_pkgs(pkg.cpv)[0]
                        if not self.quiet_repo_display and installed_version.repo != pkg.repo:
                                myoldbest = [installed_version]
@@ -775,17 +721,31 @@ class Display(object):
                                myinslotlist = None
                        if myinslotlist:
                                myoldbest = myinslotlist[:]
-                               addl = self._insert_slot(pkg, pkg_info, myinslotlist)
+                               if not cpvequal(pkg.cpv,
+                                       best([pkg.cpv] + [x.cpv for x in myinslotlist])):
+                                       # Downgrade in slot
+                                       pkg_info.attr_display.new_version = True
+                                       pkg_info.attr_display.downgrade = True
+                                       if pkg_info.ordered:
+                                               self.counters.downgrades += 1
+                               else:
+                                       # Update in slot
+                                       pkg_info.attr_display.new_version = True
+                                       if pkg_info.ordered:
+                                               self.counters.upgrades += 1
                        else:
                                myoldbest = installed_versions
-                               addl = self._new_slot(pkg, pkg_info)
+                               pkg_info.attr_display.new = True
+                               pkg_info.attr_display.new_slot = True
+                               if pkg_info.ordered:
+                                       self.counters.newslot += 1
                        if self.conf.changelog:
                                self.do_changelog(pkg, pkg_info)
                else:
-                       addl = " " + green("N") + " " + pkg_info.fetch_symbol + "  "
+                       pkg_info.attr_display.new = True
                        if pkg_info.ordered:
                                self.counters.new += 1
-               return addl, myoldbest, myinslotlist
+               return myoldbest, myinslotlist
 
 
        def __call__(self, depgraph, mylist, favorites=None, verbosity=None):
@@ -828,7 +788,7 @@ class Display(object):
                                        continue
                        else:
                                pkg_info = self.set_pkg_info(pkg, ordered)
-                               addl, pkg_info.oldbest_list, myinslotlist = \
+                               pkg_info.oldbest_list, myinslotlist = \
                                        self._get_installed_best(pkg, pkg_info)
                                self.verboseadd = ""
                                if self.quiet_repo_display:
@@ -849,17 +809,20 @@ class Display(object):
                                pkg_info.oldbest = self.convert_myoldbest(pkg, pkg_info.oldbest_list)
                                pkg_info.system, pkg_info.world = \
                                        self.check_system_world(pkg)
-                               addl = self.set_interactive(pkg, pkg_info.ordered, addl)
+                               if 'interactive' in pkg.metadata.properties and \
+                                       pkg.operation == 'merge':
+                                       pkg_info.attr_display.interactive = True
+                                       if ordered:
+                                               self.counters.interactive += 1
 
                                if self.include_mask_str():
-                                       addl += self.gen_mask_str(pkg)
+                                       pkg_info.attr_display.mask = self.gen_mask_str(pkg)
 
                                if pkg.root_config.settings["ROOT"] != "/":
                                        if pkg_info.oldbest:
                                                pkg_info.oldbest += " "
                                        if self.conf.columns:
-                                               myprint = self._set_non_root_columns(
-                                                       addl, pkg_info, pkg)
+                                               myprint = self._set_non_root_columns(pkg_info, pkg)
                                        else:
                                                pkg_str = pkg.cpv
                                                if self.conf.verbosity == 3 and not self.quiet_repo_display and (self.verbose_main_repo_display or
@@ -873,17 +836,16 @@ class Display(object):
                                                                )
                                                else:
                                                        myprint = "[%s %s] " % (
-                                                               self.pkgprint(pkg.type_name, pkg_info), addl)
+                                                               self.pkgprint(pkg.type_name, pkg_info),
+                                                               pkg_info.attr_display)
                                                myprint += self.indent + \
                                                        self.pkgprint(pkg_str, pkg_info) + " " + \
                                                        pkg_info.oldbest + darkgreen("to " + pkg.root)
                                else:
                                        if self.conf.columns:
-                                               myprint = self._set_root_columns(
-                                                       addl, pkg_info, pkg)
+                                               myprint = self._set_root_columns(pkg_info, pkg)
                                        else:
-                                               myprint = self._set_no_columns(
-                                                       pkg, pkg_info, addl)
+                                               myprint = self._set_no_columns(pkg, pkg_info)
 
                                if self.conf.columns and pkg.operation == "uninstall":
                                        continue
index dc622bd9d231fe674adbfb33597b192056ad0d35..41dc324a1d8d8b408deea98ba367f5fdd3f16e7f 100644 (file)
@@ -15,10 +15,11 @@ from portage import os
 from portage import _encodings, _unicode_encode
 from portage._sets.base import InternalPackageSet
 from portage.output import (blue, bold, colorize, create_color_func,
-       green, red, teal, yellow)
+       green, red, teal, turquoise, yellow)
 bad = create_color_func("BAD")
 from portage.package.ebuild.config import _feature_flags
 from portage.util import shlex_split, writemsg
+from portage.util.SlotObject import SlotObject
 from portage.versions import catpkgsplit
 
 from _emerge.Blocker import Blocker
@@ -613,7 +614,8 @@ class PkgInfo(object):
        information about the pkg being printed.
        """
        
-       __slots__ = ("built", "cp", "ebuild_path", "fetch_symbol", "merge",
+       __slots__ = ("attr_display", "built", "cp",
+               "ebuild_path", "fetch_symbol", "merge",
                "oldbest", "oldbest_list", "operation", "ordered",
                "repo_name", "repo_path_real", "system", "use", "ver", "world")
 
@@ -634,3 +636,61 @@ class PkgInfo(object):
                self.use = ''
                self.ver = ''
                self.world = False
+               self.attr_display = PkgAttrDisplay()
+
+class PkgAttrDisplay(SlotObject):
+
+       __slots__ = ("downgrade", "fetch_restrict", "fetch_restrict_satisfied",
+               "interactive", "mask", "new", "new_slot", "new_version", "replace")
+
+       def __str__(self):
+               output = []
+
+               if self.interactive:
+                       output.append(colorize("WARN", "I"))
+               else:
+                       output.append(" ")
+
+               if self.new:
+                       output.append(green("N"))
+               else:
+                       output.append(" ")
+
+               if self.new_slot or self.replace:
+                       if self.replace:
+                               output.append(yellow("R"))
+                       else:
+                               output.append(green("S"))
+               else:
+                       output.append(" ")
+
+               if self.fetch_restrict or self.fetch_restrict_satisfied:
+                       if self.fetch_restrict_satisfied:
+                               output.append(green("f"))
+                       else:
+                               output.append(red("F"))
+               else:
+                       output.append(" ")
+
+               if self.new_version:
+                       output.append(turquoise("U"))
+               else:
+                       output.append(" ")
+
+               if self.downgrade:
+                       output.append(blue("D"))
+               else:
+                       output.append(" ")
+
+               if self.mask is not None:
+                       output.append(self.mask)
+
+               return "".join(output)
+
+       if sys.hexversion < 0x3000000:
+
+               __unicode__ = __str__
+
+               def __str__(self):
+                       return _unicode_encode(self.__unicode__(),
+                               encoding=_encodings['content'])