From 16dba7828864d683219c15df5028312601aea8b1 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Tue, 10 Sep 2024 09:02:37 +0200 Subject: [PATCH] spec.py: dedent format logic (#46279) --- lib/spack/spack/spec.py | 69 ++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index e420eaada96..3d5a1ce3203 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -3911,46 +3911,43 @@ def format_attribute(match_object: Match) -> str: for idx, part in enumerate(parts): if not part: raise SpecFormatStringError("Format string attributes must be non-empty") - if part.startswith("_"): + elif part.startswith("_"): raise SpecFormatStringError("Attempted to format private attribute") + elif isinstance(current, VariantMap): + # subscript instead of getattr for variant names + try: + current = current[part] + except KeyError: + raise SpecFormatStringError(f"Variant '{part}' does not exist") else: - if isinstance(current, VariantMap): - # subscript instead of getattr for variant names - try: - current = current[part] - except KeyError: - raise SpecFormatStringError(f"Variant '{part}' does not exist") - else: - # aliases - if part == "arch": - part = "architecture" - elif part == "version": - # version (singular) requires a concrete versions list. Avoid - # pedantic errors by using versions (plural) when not concrete. - # These two are not entirely equivalent for pkg@=1.2.3: - # - version prints '1.2.3' - # - versions prints '=1.2.3' - if not current.versions.concrete: - part = "versions" - try: - current = getattr(current, part) - except AttributeError: - parent = ".".join(parts[:idx]) - m = "Attempted to format attribute %s." % attribute - m += "Spec %s has no attribute %s" % (parent, part) - raise SpecFormatStringError(m) - if isinstance(current, vn.VersionList): - if current == vn.any_version: - # don't print empty version lists - return "" - - if callable(current): - raise SpecFormatStringError("Attempted to format callable object") - - if current is None: - # not printing anything + # aliases + if part == "arch": + part = "architecture" + elif part == "version" and not current.versions.concrete: + # version (singular) requires a concrete versions list. Avoid + # pedantic errors by using versions (plural) when not concrete. + # These two are not entirely equivalent for pkg@=1.2.3: + # - version prints '1.2.3' + # - versions prints '=1.2.3' + part = "versions" + try: + current = getattr(current, part) + except AttributeError: + raise SpecFormatStringError( + f"Attempted to format attribute {attribute}. " + f"Spec {'.'.join(parts[:idx])} has no attribute {part}" + ) + if isinstance(current, vn.VersionList) and current == vn.any_version: + # don't print empty version lists return "" + if callable(current): + raise SpecFormatStringError("Attempted to format callable object") + + if current is None: + # not printing anything + return "" + # Set color codes for various attributes color = None if "architecture" in parts: