1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Use literal string interpolation in core (f-strings) (#26166)

This commit is contained in:
Franck Nijhof
2019-08-23 18:53:33 +02:00
committed by Paulus Schoutsen
parent 1efa29d6ff
commit decf13b948
67 changed files with 180 additions and 246 deletions

View File

@@ -230,9 +230,7 @@ def gather_recursive_requirements(domain, seen=None):
seen = set()
seen.add(domain)
integration = Integration(
pathlib.Path("homeassistant/components/{}".format(domain))
)
integration = Integration(pathlib.Path(f"homeassistant/components/{domain}"))
integration.load_manifest()
reqs = set(integration.manifest["requirements"])
for dep_domain in integration.manifest["dependencies"]:
@@ -272,13 +270,13 @@ def gather_requirements_from_manifests(errors, reqs):
integration = integrations[domain]
if not integration.manifest:
errors.append("The manifest for integration {} is invalid.".format(domain))
errors.append(f"The manifest for integration {domain} is invalid.")
continue
process_requirements(
errors,
integration.manifest["requirements"],
"homeassistant.components.{}".format(domain),
f"homeassistant.components.{domain}",
reqs,
)
@@ -306,13 +304,9 @@ def process_requirements(errors, module_requirements, package, reqs):
if req in IGNORE_REQ:
continue
if "://" in req:
errors.append(
"{}[Only pypi dependencies are allowed: {}]".format(package, req)
)
errors.append(f"{package}[Only pypi dependencies are allowed: {req}]")
if req.partition("==")[1] == "" and req not in IGNORE_PIN:
errors.append(
"{}[Please pin requirement {}, see {}]".format(package, req, URL_PIN)
)
errors.append(f"{package}[Please pin requirement {req}, see {URL_PIN}]")
reqs.setdefault(req, []).append(package)
@@ -321,12 +315,12 @@ def generate_requirements_list(reqs):
output = []
for pkg, requirements in sorted(reqs.items(), key=lambda item: item[0]):
for req in sorted(requirements):
output.append("\n# {}".format(req))
output.append(f"\n# {req}")
if comment_requirement(pkg):
output.append("\n# {}\n".format(pkg))
output.append(f"\n# {pkg}\n")
else:
output.append("\n{}\n".format(pkg))
output.append(f"\n{pkg}\n")
return "".join(output)

View File

@@ -68,7 +68,7 @@ def main():
print()
for integration in sorted(invalid_itg, key=lambda itg: itg.domain):
print("Integration {}:".format(integration.domain))
print(f"Integration {integration.domain}:")
for error in integration.errors:
print("*", error)
print()

View File

@@ -67,5 +67,5 @@ def validate(integrations: Dict[str, Integration], config):
for dep in integration.manifest["dependencies"]:
if dep not in integrations:
integration.add_error(
"dependencies", "Dependency {} does not exist".format(dep)
"dependencies", f"Dependency {dep} does not exist"
)

View File

@@ -79,20 +79,20 @@ class Integration:
"""Load manifest."""
manifest_path = self.path / "manifest.json"
if not manifest_path.is_file():
self.add_error("model", "Manifest file {} not found".format(manifest_path))
self.add_error("model", f"Manifest file {manifest_path} not found")
return
try:
manifest = json.loads(manifest_path.read_text())
except ValueError as err:
self.add_error("model", "Manifest contains invalid JSON: {}".format(err))
self.add_error("model", f"Manifest contains invalid JSON: {err}")
return
self.manifest = manifest
def import_pkg(self, platform=None):
"""Import the Python file."""
pkg = "homeassistant.components.{}".format(self.domain)
pkg = f"homeassistant.components.{self.domain}"
if platform is not None:
pkg += ".{}".format(platform)
pkg += f".{platform}"
return importlib.import_module(pkg)

View File

@@ -34,7 +34,7 @@ def printc(the_color, *args):
print(escape_codes[the_color] + msg + escape_codes["reset"])
except KeyError:
print(msg)
raise ValueError("Invalid color {}".format(the_color))
raise ValueError(f"Invalid color {the_color}")
def validate_requirements_ok():
@@ -145,7 +145,7 @@ async def lint(files):
lint_ok = True
for err in res:
err_msg = "{} {}:{} {}".format(err.file, err.line, err.col, err.msg)
err_msg = f"{err.file} {err.line}:{err.col} {err.msg}"
# tests/* does not have to pass lint
if err.skip:

View File

@@ -40,18 +40,11 @@ def get_component_path(lang, component):
"""Get the component translation path."""
if os.path.isdir(os.path.join("homeassistant", "components", component)):
return os.path.join(
"homeassistant",
"components",
component,
".translations",
"{}.json".format(lang),
"homeassistant", "components", component, ".translations", f"{lang}.json"
)
else:
return os.path.join(
"homeassistant",
"components",
".translations",
"{}.{}.json".format(component, lang),
"homeassistant", "components", ".translations", f"{component}.{lang}.json"
)
@@ -64,7 +57,7 @@ def get_platform_path(lang, component, platform):
component,
platform,
".translations",
"{}.json".format(lang),
f"{lang}.json",
)
else:
return os.path.join(
@@ -72,7 +65,7 @@ def get_platform_path(lang, component, platform):
"components",
component,
".translations",
"{}.{}.json".format(platform, lang),
f"{platform}.{lang}.json",
)

View File

@@ -35,7 +35,7 @@ def save_json(filename: str, data: Union[List, Dict]):
def find_strings_files():
"""Return the paths of the strings source files."""
return itertools.chain(
glob.iglob("strings*.json"), glob.iglob("*{}strings*.json".format(os.sep))
glob.iglob("strings*.json"), glob.iglob(f"*{os.sep}strings*.json")
)

View File

@@ -81,7 +81,7 @@ def bump_version(version, bump_type):
to_change["pre"] = ("b", 0)
else:
assert False, "Unsupported type: {}".format(bump_type)
assert False, f"Unsupported type: {bump_type}"
temp = Version("0")
temp._version = version._version._replace(**to_change)
@@ -95,15 +95,9 @@ def write_version(version):
major, minor, patch = str(version).split(".", 2)
content = re.sub(
"MAJOR_VERSION = .*\n", "MAJOR_VERSION = {}\n".format(major), content
)
content = re.sub(
"MINOR_VERSION = .*\n", "MINOR_VERSION = {}\n".format(minor), content
)
content = re.sub(
"PATCH_VERSION = .*\n", "PATCH_VERSION = '{}'\n".format(patch), content
)
content = re.sub("MAJOR_VERSION = .*\n", f"MAJOR_VERSION = {major}\n", content)
content = re.sub("MINOR_VERSION = .*\n", f"MINOR_VERSION = {minor}\n", content)
content = re.sub("PATCH_VERSION = .*\n", f"PATCH_VERSION = '{patch}'\n", content)
with open("homeassistant/const.py", "wt") as fil:
content = fil.write(content)
@@ -129,7 +123,7 @@ def main():
if not arguments.commit:
return
subprocess.run(["git", "commit", "-am", "Bumped version to {}".format(bumped)])
subprocess.run(["git", "commit", "-am", f"Bumped version to {bumped}"])
def test_bump_version():