diff options
Diffstat (limited to 'lib/spack/spack/spec.py')
-rw-r--r-- | lib/spack/spack/spec.py | 74 |
1 files changed, 57 insertions, 17 deletions
diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index 2e144fc80f..624c28285a 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -2826,9 +2826,10 @@ class Spec(object): return colorize_spec(self) def format(self, format_string='$_$@$%@+$+$=', **kwargs): - """ - Prints out particular pieces of a spec, depending on what is - in the format string. The format strings you can provide are:: + """Prints out particular pieces of a spec, depending on what is + in the format string. + + The format strings you can provide are:: $_ Package name $. Full package name (with namespace) @@ -2870,13 +2871,36 @@ class Spec(object): 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. + Args: + format_string (str): string containing the format to be expanded + + **kwargs (dict): the following list of keywords is supported + + - color (bool): True if returned string is colored + + - transform (dict): maps full-string formats to a callable \ + that accepts a string and returns another one + + Examples: + + The following line: + + .. code-block:: python + + s = spec.format('$_$@$+') + + translates to the name, version, and options of the package, but no + dependencies, arch, or compiler. TODO: allow, e.g., ``$6#`` to customize short hash length TODO: allow, e.g., ``$//`` for full hash. """ color = kwargs.get('color', False) + + # Dictionary of transformations for named tokens + token_transforms = {} + token_transforms.update(kwargs.get('transform', {})) + length = len(format_string) out = StringIO() named = escape = compiler = False @@ -2953,39 +2977,55 @@ class Spec(object): named_str += c continue named_str = named_str.upper() + + # Retrieve the token transformation from the dictionary. + # + # The default behavior is to leave the string unchanged + # (`lambda x: x` is the identity function) + token_transform = token_transforms.get(named_str, lambda x: x) + if named_str == 'PACKAGE': name = self.name if self.name else '' - write(fmt % self.name, '@') + write(fmt % token_transform(name), '@') if named_str == 'VERSION': if self.versions and self.versions != _any_version: - write(fmt % str(self.versions), '@') + write(fmt % token_transform(str(self.versions)), '@') elif named_str == 'COMPILER': if self.compiler: - write(fmt % self.compiler, '%') + write(fmt % token_transform(self.compiler), '%') elif named_str == 'COMPILERNAME': if self.compiler: - write(fmt % self.compiler.name, '%') + write(fmt % token_transform(self.compiler.name), '%') elif named_str in ['COMPILERVER', 'COMPILERVERSION']: if self.compiler: - write(fmt % self.compiler.versions, '%') + write( + fmt % token_transform(self.compiler.versions), + '%' + ) elif named_str == 'COMPILERFLAGS': if self.compiler: - write(fmt % str(self.compiler_flags), '%') + write( + fmt % token_transform(str(self.compiler_flags)), + '%' + ) elif named_str == 'OPTIONS': if self.variants: - write(fmt % str(self.variants), '+') + write(fmt % token_transform(str(self.variants)), '+') elif named_str == 'ARCHITECTURE': if self.architecture and str(self.architecture): - write(fmt % str(self.architecture), '=') + write( + fmt % token_transform(str(self.architecture)), + '=' + ) elif named_str == 'SHA1': if self.dependencies: - out.write(fmt % str(self.dag_hash(7))) + out.write(fmt % token_transform(str(self.dag_hash(7)))) elif named_str == 'SPACK_ROOT': - out.write(fmt % spack.prefix) + out.write(fmt % token_transform(spack.prefix)) elif named_str == 'SPACK_INSTALL': - out.write(fmt % spack.store.root) + out.write(fmt % token_transform(spack.store.root)) elif named_str == 'PREFIX': - out.write(fmt % self.prefix) + out.write(fmt % token_transform(self.prefix)) elif named_str.startswith('HASH'): if named_str.startswith('HASH:'): _, hashlen = named_str.split(':') |