summaryrefslogtreecommitdiffstats
path: root/pym/portage/output.py
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2009-08-18 06:03:08 +0000
committerZac Medico <zmedico@gentoo.org>2009-08-18 06:03:08 +0000
commit5a762a62e2db198bca50215a2d1c4b67b74a63e0 (patch)
tree4f20558e9a213d4355cc8e0ba3cce6eea87daed8 /pym/portage/output.py
parent620ea2ba391be96b74e3d457a18585dac786e2e7 (diff)
downloadportage-5a762a62e2db198bca50215a2d1c4b67b74a63e0.tar.gz
portage-5a762a62e2db198bca50215a2d1c4b67b74a63e0.tar.bz2
portage-5a762a62e2db198bca50215a2d1c4b67b74a63e0.zip
Implement lazy loading of color.map when either the codes or _styles
attribute is accessed. This provides and _init(config_root='/') function that the prefix branch can use to substitute an appropriate config_root value at runtime. svn path=/main/trunk/; revision=14083
Diffstat (limited to 'pym/portage/output.py')
-rw-r--r--pym/portage/output.py57
1 files changed, 45 insertions, 12 deletions
diff --git a/pym/portage/output.py b/pym/portage/output.py
index fbf6c01e4..373b9686f 100644
--- a/pym/portage/output.py
+++ b/pym/portage/output.py
@@ -141,7 +141,7 @@ _styles["PKG_NOMERGE_WORLD"] = ( "blue", )
_styles["PROMPT_CHOICE_DEFAULT"] = ( "green", )
_styles["PROMPT_CHOICE_OTHER"] = ( "red", )
-def _parse_color_map(onerror=None):
+def _parse_color_map(config_root='/', onerror=None):
"""
Parse /etc/portage/color.map and return a dict of error codes.
@@ -157,7 +157,7 @@ def _parse_color_map(onerror=None):
# that can be called in order adjust the location that color.map
# is read from.
global codes, _styles
- myfile = os.path.join('/', COLOR_MAP_FILE)
+ myfile = os.path.join(config_root, COLOR_MAP_FILE)
ansi_code_pattern = re.compile("^[0-9;]*m$")
quotes = '\'"'
def strip_quotes(token):
@@ -723,13 +723,46 @@ class TermProgressBar(ProgressBar):
">" + ((max_bar_width - bar_width) * " ") + "]"
return image
-try:
- _parse_color_map(onerror=lambda e: writemsg("%s\n" % str(e), noiselevel=-1))
-except FileNotFound:
- pass
-except PermissionDenied, e:
- writemsg(_("Permission denied: '%s'\n") % str(e), noiselevel=-1)
- del e
-except PortageException, e:
- writemsg("%s\n" % str(e), noiselevel=-1)
- del e
+_color_map_loaded = False
+
+def _init(config_root='/'):
+ """
+ Load color.map from the given config_root. This is called automatically
+ on first access of the codes or _styles attributes (unless it has already
+ been called for some other reason).
+ """
+
+ global _color_map_loaded, codes, _styles
+ if _color_map_loaded:
+ return
+
+ _color_map_loaded = True
+ codes = object.__getattribute__(codes, '_attr')
+ _styles = object.__getattribute__(_styles, '_attr')
+
+ try:
+ _parse_color_map(config_root=config_root,
+ onerror=lambda e: writemsg("%s\n" % str(e), noiselevel=-1))
+ except FileNotFound:
+ pass
+ except PermissionDenied, e:
+ writemsg(_("Permission denied: '%s'\n") % str(e), noiselevel=-1)
+ del e
+ except PortageException, e:
+ writemsg("%s\n" % str(e), noiselevel=-1)
+ del e
+
+class _LazyInitColorMap(portage.proxy.objectproxy.ObjectProxy):
+
+ __slots__ = ('_attr',)
+
+ def __init__(self, attr):
+ portage.proxy.objectproxy.ObjectProxy.__init__(self)
+ object.__setattr__(self, '_attr', attr)
+
+ def _get_target(self):
+ _init()
+ return object.__getattribute__(self, '_attr')
+
+codes = _LazyInitColorMap(codes)
+_styles = _LazyInitColorMap(_styles)