Made formatting specs easier with format() syntax.

- Replaced existing str_no_deps() function with more flexible format() function.
- Spec.tree() can take a format argument now, as well.
This commit is contained in:
Todd Gamblin 2013-12-20 16:09:26 -08:00
parent e645bb065a
commit 88017ec49e
2 changed files with 75 additions and 30 deletions

View File

@ -63,12 +63,7 @@ def hasher():
specs = index[architecture][compiler] specs = index[architecture][compiler]
specs.sort() specs.sort()
abbreviated = [] abbreviated = [s.format('$_$@$+$#') for s in specs]
for s in specs:
abbrv = "%s@%s%s" % (s.name, s.version, s.variants)
if s.dependencies:
abbrv += '-' + s.dependencies.sha1()[:6]
abbreviated.append(abbrv)
if args.paths: if args.paths:
# Print one spec per line along with prefix path # Print one spec per line along with prefix path
@ -81,7 +76,7 @@ def hasher():
elif args.full_specs: elif args.full_specs:
for spec in specs: for spec in specs:
print spec.tree(indent=4), print spec.tree(indent=4, format='$_$@$+'),
else: else:
for abbrv in abbreviated: for abbrv in abbreviated:
print " %s" % abbrv print " %s" % abbrv

View File

@ -805,25 +805,82 @@ def colorized(self):
return colorize_spec(self) return colorize_spec(self)
def str_no_deps(self, **kwargs): def format(self, format_string='$_$@$%@$+$=', **kwargs):
out = self.name """Prints out particular pieces of a spec, depending on what is
in the format string. The format strings you can provide are::
# If the version range is entirely open, omit it $_ Package name
if self.versions and self.versions != VersionList([':']): $@ Version
out += "@%s" % self.versions $% Compiler
$%@ Compiler & compiler version
$+ Options
$= Architecture
$# Dependencies' 6-char sha1 prefix
$$ $
if self.compiler: Anything else is copied verbatim into the output stream.
out += "%%%s" % self.compiler Example: "$_$@$+" translates to the name, version, and options
of the package, but no dependencies, arch, or compiler.
"""
color = kwargs.get('color', False)
out += str(self.variants) length = len(format_string)
out = StringIO()
escape = compiler = False
for i, c in enumerate(format_string):
if escape:
if c == '_':
out.write(self.name)
elif c == '@':
if self.versions and self.versions != VersionList([':']):
out.write(c + str(self.versions))
elif c == '%':
if self.compiler:
out.write(c + str(self.compiler.name))
compiler = True
elif c == '+':
if self.variants:
out.write(str(self.variants))
elif c == '=':
if self.architecture:
out.write(c + str(self.architecture))
elif c == '#':
if self.dependencies:
out.write('-' + self.dependencies.sha1()[:6])
elif c == '$':
out.write('$')
escape = False
if self.architecture: elif compiler:
out += "=%s" % self.architecture if c == '@':
if self.compiler and self.compiler.versions:
out.write(c + str(self.compiler.versions))
elif c == '$':
escape = True
else:
out.write(c)
compiler = False
if kwargs.get('color', False): elif c == '$':
return colorize_spec(out) escape = True
else: if i == length - 1:
return out raise ValueError("Error: unterminated $ in format: '%s'"
% format_string)
else:
out.write(c)
result = out.getvalue()
if color:
result = colorize_spec(result)
return result
def __str__(self):
by_name = lambda d: d.name
deps = self.preorder_traversal(key=by_name, root=False)
sorted_deps = sorted(deps, key=by_name)
dep_string = ''.join("^" + dep.format() for dep in sorted_deps)
return self.format() + dep_string
def tree(self, **kwargs): def tree(self, **kwargs):
@ -834,6 +891,7 @@ def tree(self, **kwargs):
showid = kwargs.get('ids', False) showid = kwargs.get('ids', False)
cover = kwargs.get('cover', 'nodes') cover = kwargs.get('cover', 'nodes')
indent = kwargs.get('indent', 0) indent = kwargs.get('indent', 0)
format = kwargs.get('format', '$_$@$%@$+$=')
out = "" out = ""
cur_id = 0 cur_id = 0
@ -850,7 +908,7 @@ def tree(self, **kwargs):
out += (" " * d) out += (" " * d)
if d > 0: if d > 0:
out += "^" out += "^"
out += node.str_no_deps(color=color) + "\n" out += node.format(format, color=color) + "\n"
return out return out
@ -864,14 +922,6 @@ def __repr__(self):
return str(self) return str(self)
def __str__(self):
byname = lambda d: d.name
deps = self.preorder_traversal(key=byname, root=False)
sorted_deps = sorted(deps, key=byname)
dep_string = ''.join("^" + dep.str_no_deps() for dep in sorted_deps)
return self.str_no_deps() + dep_string
# #
# These are possible token types in the spec grammar. # These are possible token types in the spec grammar.
# #