spec.py: dedent format logic (#46279)

This commit is contained in:
Harmen Stoppels 2024-09-10 09:02:37 +02:00 committed by GitHub
parent b220938d42
commit 16dba78288
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3911,46 +3911,43 @@ def format_attribute(match_object: Match) -> str:
for idx, part in enumerate(parts): for idx, part in enumerate(parts):
if not part: if not part:
raise SpecFormatStringError("Format string attributes must be non-empty") raise SpecFormatStringError("Format string attributes must be non-empty")
if part.startswith("_"): elif part.startswith("_"):
raise SpecFormatStringError("Attempted to format private attribute") 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: else:
if isinstance(current, VariantMap): # aliases
# subscript instead of getattr for variant names if part == "arch":
try: part = "architecture"
current = current[part] elif part == "version" and not current.versions.concrete:
except KeyError: # version (singular) requires a concrete versions list. Avoid
raise SpecFormatStringError(f"Variant '{part}' does not exist") # pedantic errors by using versions (plural) when not concrete.
else: # These two are not entirely equivalent for pkg@=1.2.3:
# aliases # - version prints '1.2.3'
if part == "arch": # - versions prints '=1.2.3'
part = "architecture" part = "versions"
elif part == "version": try:
# version (singular) requires a concrete versions list. Avoid current = getattr(current, part)
# pedantic errors by using versions (plural) when not concrete. except AttributeError:
# These two are not entirely equivalent for pkg@=1.2.3: raise SpecFormatStringError(
# - version prints '1.2.3' f"Attempted to format attribute {attribute}. "
# - versions prints '=1.2.3' f"Spec {'.'.join(parts[:idx])} has no attribute {part}"
if not current.versions.concrete: )
part = "versions" if isinstance(current, vn.VersionList) and current == vn.any_version:
try: # don't print empty version lists
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
return "" 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 # Set color codes for various attributes
color = None color = None
if "architecture" in parts: if "architecture" in parts: