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:
		@@ -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
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
 | 
					               $@   Version
 | 
				
			||||||
 | 
					               $%   Compiler
 | 
				
			||||||
 | 
					               $%@  Compiler & compiler version
 | 
				
			||||||
 | 
					               $+   Options
 | 
				
			||||||
 | 
					               $=   Architecture
 | 
				
			||||||
 | 
					               $#   Dependencies' 6-char sha1 prefix
 | 
				
			||||||
 | 
					               $$   $
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					           Anything else is copied verbatim into the output stream.
 | 
				
			||||||
 | 
					           Example:  "$_$@$+" translates to the name, version, and options
 | 
				
			||||||
 | 
					                     of the package, but no dependencies, arch, or compiler.
 | 
				
			||||||
 | 
					           """
 | 
				
			||||||
 | 
					        color = kwargs.get('color', False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        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([':']):
 | 
					                    if self.versions and self.versions != VersionList([':']):
 | 
				
			||||||
            out += "@%s" % self.versions
 | 
					                        out.write(c + str(self.versions))
 | 
				
			||||||
 | 
					                elif c == '%':
 | 
				
			||||||
                    if self.compiler:
 | 
					                    if self.compiler:
 | 
				
			||||||
            out += "%%%s" % self.compiler
 | 
					                        out.write(c + str(self.compiler.name))
 | 
				
			||||||
 | 
					                    compiler = True
 | 
				
			||||||
        out += str(self.variants)
 | 
					                elif c == '+':
 | 
				
			||||||
 | 
					                    if self.variants:
 | 
				
			||||||
 | 
					                        out.write(str(self.variants))
 | 
				
			||||||
 | 
					                elif c == '=':
 | 
				
			||||||
                    if self.architecture:
 | 
					                    if self.architecture:
 | 
				
			||||||
            out += "=%s" % 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 kwargs.get('color', False):
 | 
					            elif compiler:
 | 
				
			||||||
            return colorize_spec(out)
 | 
					                if c == '@':
 | 
				
			||||||
 | 
					                    if self.compiler and self.compiler.versions:
 | 
				
			||||||
 | 
					                        out.write(c + str(self.compiler.versions))
 | 
				
			||||||
 | 
					                elif c == '$':
 | 
				
			||||||
 | 
					                    escape = True
 | 
				
			||||||
                else:
 | 
					                else:
 | 
				
			||||||
            return out
 | 
					                    out.write(c)
 | 
				
			||||||
 | 
					                compiler = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            elif c == '$':
 | 
				
			||||||
 | 
					                escape = True
 | 
				
			||||||
 | 
					                if i == length - 1:
 | 
				
			||||||
 | 
					                    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.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user