3 # Copyright(c) 2010, Gentoo Foundation
4 # Distributed under the terms of the GNU General Public License v2
7 """Provides various output classes and functions for
8 both screen and file output
11 from __future__ import print_function
16 from gentoolkit import pprinter as pp
17 from gentoolkit.formatters import CpvValueWrapper
18 from gentoolkit.cpv import split_cpv
21 """small utility function to print blank lines
24 @param lines: optional number of blank lines to print
29 class AnalysisPrinter(CpvValueWrapper):
30 """Printing functions"""
31 def __init__(self, target, verbose=True, references=None, key_width=1, width=None):
32 """@param references: list of accepted keywords or
35 self.references = references
36 self.key_width = key_width
38 CpvValueWrapper.__init__(self, cpv_width=key_width, width=width)
39 self.set_target(target, verbose)
41 def set_target(self, target, verbose=True):
44 self.print_fn = self.print_use_verbose
46 self.print_fn = self.print_use_quiet
47 self._format_key = self._format_use_keyword
48 elif target in ["keywords"]:
50 self.print_fn = self.print_keyword_verbose
52 self.print_fn = self.print_keyword_quiet
53 self._format_key = self._format_use_keyword
54 elif target in ["packages"]:
56 self.print_fn = self.print_pkg_verbose
58 self.print_fn = self.print_pkg_quiet
59 self._format_key = self._format_pkg
61 def __call__(self, key, active, data):
62 self._format_key(key, active, data)
64 def _format_use_keyword(self, key, active, pkgs):
65 """Determines the stats for key, formats it and
66 calls the pre-determined print function
68 occurred = str(len(pkgs))
69 if active in ["-", "~"]:
73 if _key in self.references:
77 count = ' '*(5-len(occurred)) + occurred
79 self.print_fn(key, active, default, count, pkgs)
82 def print_use_verbose(key, active, default, count, pkgs):
83 """Verbosely prints a set of use flag info. including the pkgs
87 if active in ["+", "-"]:
88 _key = pp.useflag((active+key), active=="+")
92 print(_key,'.'*(35-len(key)), default, pp.number(count), pp.cpv(cpv))
95 print(' '*52 + pp.cpv(cpv))
97 # W0613: *Unused argument %r*
98 # pylint: disable-msg=W0613
100 def print_use_quiet(key, active, default, count, pkgs):
101 """Quietly prints a subset set of USE flag info..
103 if active in ["+", "-"]:
104 _key = pp.useflag((active+key), active=="+")
107 print(_key,'.'*(35-len(key)), default, pp.number(count))
110 def print_keyword_verbose(key, stability, default, count, pkgs):
111 """Verbosely prints a set of keywords info. including the pkgs
115 _key = (pp.keyword((stability+key),stable=(stability==" "),
116 hard_masked=stability=="-"))
118 print(_key,'.'*(20-len(key)), default, pp.number(count), pp.cpv(cpv))
121 print(' '*37 + pp.cpv(cpv))
123 # W0613: *Unused argument %r*
124 # pylint: disable-msg=W0613
126 def print_keyword_quiet(key, stability, default, count, pkgs):
127 """Quietly prints a subset set of USE flag info..
129 _key = (pp.keyword((stability+key), stable=(stability==" "),
130 hard_masked=stability=="-"))
131 print(_key,'.'*(20-len(key)), default, pp.number(count))
133 # W0613: *Unused argument %r*
134 # pylint: disable-msg=W0613
135 def _format_pkg(self, key, active, flags):
136 """Determines the stats for key, formats it and
137 calls the pre-determined print function
139 (plus, minus, cleaned) = flags
154 _cleaned.append(_flag)
155 #print("cpv=", key, "_plus=", _plus, "_minus=", _minus)
156 self.print_fn(key, (plus, minus, cleaned))
158 def print_pkg_verbose(self, cpv, flags):
159 """Verbosely prints the pkg's use flag info.
161 (plus, minus, unset) = flags
164 _flags.append(pp.useflag((flag), True))
166 _flags.append(pp.useflag(('-' + flag), False))
168 _flags.append(pp.globaloption('-' + flag))
170 print(self._format_values(cpv, ", ".join(_flags)))
173 def print_pkg_quiet(self, cpv, flags):
174 """Verbosely prints the pkg's use flag info.
176 (plus, minus, unset) = flags
179 _flags.append(pp.useflag((flag), True))
181 _flags.append(pp.useflag(('-'+flag), False))
183 _flags.append(pp.globaloption('-' + flag))
185 print(self._format_values(cpv, ", ".join(_flags)))
188 class RebuildPrinter(CpvValueWrapper):
189 """Output functions"""
190 def __init__(self, target, pretend=True, exact=False,
191 slot=False, key_width=1, width=None):
192 """@param references: list of accepted keywords or
196 self.set_target(target)
197 self.pretend = pretend
198 CpvValueWrapper.__init__(self, cpv_width=key_width, width=width)
201 self.init_indent = len(self.spacer)
209 def set_target(self, target):
210 if target in ["use"]:
211 self.print_fn = self.print_use
212 elif target in ["keywords"]:
213 self.print_fn = self.print_keyword
214 elif target in ["unmask"]:
215 self.print_fn = self.print_mask
216 self.lines = [self.header()]
219 def __call__(self, key, values, cp_count):
220 if self.target in ["keywords", "use"]:
221 self._format_atoms(key, values, cp_count)
223 self._format_key(key, values)
226 def _format_key(self, key, values):
227 """Determines the stats for key, formats it and
228 calls the pre-determined print function
233 parts = split_cpv(key)
234 _key = '/'.join(parts[:2])
236 self.data[_key] = values
237 self.print_fn( _key, values)
239 def print_use(self, key, atom=None, values=None):
240 """Prints a USE flag string.
242 if atom and not values:
247 flags.append(pp.useflag(flag, (flag[0] != '-')))
248 print(self._format_values(self.spacer+key, ' '.join(flags)))
250 line = ' '.join([key, ' '.join(values)])
251 self.lines.append(line)
253 def _format_atoms(self, key, atoms, count):
254 """Determines if there are more than one atom in the values and
255 calls the predetermined print function for each atom.
257 #print("_format_atoms(),", key, atoms)
260 self.print_fn(str(atom), atom=atom)
262 #print("_format_atoms(), count =", count)
263 if self.slot or count > 1:
265 _key = str(atom.cp) + ":" + atom.slot
266 self.print_fn(_key, atom=atom)
270 self.print_fn(_key, atom=atom)
273 def print_keyword(self, key, atom=None, keyword=None):
274 """prints a pkg key and a keyword"""
275 #print("print_keyword(),", key, keyword)
276 if atom and not keyword:
277 keyword = atom.keyword
279 print(self._format_values(key, keyword))
281 line = ' '.join([key, keyword])
282 self.lines.append(line)
285 def print_unmask(self):
289 """Generates a file header
292 h=("# This package.%s file was generated by "
294 "gentoolkit's 'enalyze rebuild' module\n"
295 "# Date: " + time.asctime() + "\n"