Package _emerge :: Module UseFlagDisplay
[hide private]

Source Code for Module _emerge.UseFlagDisplay

  1  # Copyright 1999-2013 Gentoo Foundation 
  2  # Distributed under the terms of the GNU General Public License v2 
  3   
  4  from __future__ import unicode_literals 
  5   
  6  import collections 
  7  from itertools import chain 
  8  import sys 
  9   
 10  from portage import _encodings, _unicode_encode 
 11  from portage.output import red 
 12  from portage.util import cmp_sort_key 
 13  from portage.output import blue 
 14   
15 -class UseFlagDisplay(object):
16 17 __slots__ = ('name', 'enabled', 'forced') 18
19 - def __init__(self, name, enabled, forced):
20 self.name = name 21 self.enabled = enabled 22 self.forced = forced
23
24 - def __str__(self):
25 s = self.name 26 if self.enabled: 27 s = red(s) 28 else: 29 s = '-' + s 30 s = blue(s) 31 if self.forced: 32 s = '(%s)' % s 33 return s
34 35 if sys.hexversion < 0x3000000: 36 37 __unicode__ = __str__ 38
39 - def __str__(self):
40 return _unicode_encode(self.__unicode__(), 41 encoding=_encodings['content'])
42
43 - def _cmp_combined(a, b):
44 """ 45 Sort by name, combining enabled and disabled flags. 46 """ 47 return (a.name > b.name) - (a.name < b.name)
48 49 sort_combined = cmp_sort_key(_cmp_combined) 50 del _cmp_combined 51
52 - def _cmp_separated(a, b):
53 """ 54 Sort by name, separating enabled flags from disabled flags. 55 """ 56 enabled_diff = b.enabled - a.enabled 57 if enabled_diff: 58 return enabled_diff 59 return (a.name > b.name) - (a.name < b.name)
60 61 sort_separated = cmp_sort_key(_cmp_separated) 62 del _cmp_separated
63 64 65 _flag_info = collections.namedtuple('_flag_info', ('flag', 'display')) 66 67
68 -def pkg_use_display(pkg, opts, modified_use=None):
69 settings = pkg.root_config.settings 70 use_expand = pkg.use.expand 71 use_expand_hidden = pkg.use.expand_hidden 72 alphabetical_use = '--alphabetical' in opts 73 forced_flags = set(chain(pkg.use.force, 74 pkg.use.mask)) 75 if modified_use is None: 76 use = set(pkg.use.enabled) 77 else: 78 use = set(modified_use) 79 use.discard(settings.get('ARCH')) 80 use_expand_flags = set() 81 use_enabled = {} 82 use_disabled = {} 83 for varname in use_expand: 84 flag_prefix = varname.lower() + "_" 85 for f in use: 86 if f.startswith(flag_prefix): 87 use_expand_flags.add(f) 88 use_enabled.setdefault( 89 varname.upper(), []).append( 90 _flag_info(f, f[len(flag_prefix):])) 91 92 for f in pkg.iuse.all: 93 if f.startswith(flag_prefix): 94 use_expand_flags.add(f) 95 if f not in use: 96 use_disabled.setdefault( 97 varname.upper(), []).append( 98 _flag_info(f, f[len(flag_prefix):])) 99 100 var_order = set(use_enabled) 101 var_order.update(use_disabled) 102 var_order = sorted(var_order) 103 var_order.insert(0, 'USE') 104 use.difference_update(use_expand_flags) 105 use_enabled['USE'] = list(_flag_info(f, f) for f in use) 106 use_disabled['USE'] = [] 107 108 for f in pkg.iuse.all: 109 if f not in use and \ 110 f not in use_expand_flags: 111 use_disabled['USE'].append(_flag_info(f, f)) 112 113 flag_displays = [] 114 for varname in var_order: 115 if varname.lower() in use_expand_hidden: 116 continue 117 flags = [] 118 for f in use_enabled.get(varname, []): 119 flags.append(UseFlagDisplay(f.display, True, f.flag in forced_flags)) 120 for f in use_disabled.get(varname, []): 121 flags.append(UseFlagDisplay(f.display, False, f.flag in forced_flags)) 122 if alphabetical_use: 123 flags.sort(key=UseFlagDisplay.sort_combined) 124 else: 125 flags.sort(key=UseFlagDisplay.sort_separated) 126 # Use unicode_literals to force unicode format string so 127 # that UseFlagDisplay.__unicode__() is called in python2. 128 flag_displays.append('%s="%s"' % (varname, 129 ' '.join("%s" % (f,) for f in flags))) 130 131 return ' '.join(flag_displays)
132