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