1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-22 05:02:23 +01:00

[core] os.path -> Path (#10654)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
This commit is contained in:
Jesse Hills
2025-09-20 00:59:48 +12:00
committed by GitHub
parent de617c85c7
commit 9ea3643b74
57 changed files with 808 additions and 938 deletions

View File

@@ -3,7 +3,6 @@ from __future__ import annotations
from abc import ABC, abstractmethod
from enum import IntEnum
import os
from pathlib import Path
import re
from subprocess import call
@@ -2704,8 +2703,8 @@ static const char *const TAG = "api.service";
import clang_format
def exec_clang_format(path: Path) -> None:
clang_format_path = os.path.join(
os.path.dirname(clang_format.__file__), "data", "bin", "clang-format"
clang_format_path = (
Path(clang_format.__file__).parent / "data" / "bin" / "clang-format"
)
call([clang_format_path, "-i", path])

View File

@@ -39,7 +39,7 @@ esphome/core/* @esphome/core
parts = [BASE]
# Fake some directory so that get_component works
CORE.config_path = str(root)
CORE.config_path = root
CORE.data[KEY_CORE] = {KEY_TARGET_FRAMEWORK: None, KEY_TARGET_PLATFORM: None}
codeowners = defaultdict(list)

View File

@@ -1,9 +1,9 @@
#!/usr/bin/env python3
import argparse
import glob
import inspect
import json
import os
from pathlib import Path
import re
import voluptuous as vol
@@ -70,14 +70,14 @@ def get_component_names():
component_names = ["esphome", "sensor", "esp32", "esp8266"]
skip_components = []
for d in os.listdir(CORE_COMPONENTS_PATH):
for d in CORE_COMPONENTS_PATH.iterdir():
if (
not d.startswith("__")
and os.path.isdir(os.path.join(CORE_COMPONENTS_PATH, d))
and d not in component_names
and d not in skip_components
not d.name.startswith("__")
and d.is_dir()
and d.name not in component_names
and d.name not in skip_components
):
component_names.append(d)
component_names.append(d.name)
return sorted(component_names)
@@ -121,7 +121,7 @@ from esphome.util import Registry # noqa: E402
def write_file(name, obj):
full_path = os.path.join(args.output_path, name + ".json")
full_path = Path(args.output_path) / f"{name}.json"
if JSON_DUMP_PRETTY:
json_str = json.dumps(obj, indent=2)
else:
@@ -131,9 +131,10 @@ def write_file(name, obj):
def delete_extra_files(keep_names):
for d in os.listdir(args.output_path):
if d.endswith(".json") and d[:-5] not in keep_names:
os.remove(os.path.join(args.output_path, d))
output_path = Path(args.output_path)
for d in output_path.iterdir():
if d.suffix == ".json" and d.stem not in keep_names:
d.unlink()
print(f"Deleted {d}")
@@ -367,13 +368,11 @@ def get_logger_tags():
"scheduler",
"api.service",
]
for x in os.walk(CORE_COMPONENTS_PATH):
for y in glob.glob(os.path.join(x[0], "*.cpp")):
with open(y, encoding="utf-8") as file:
data = file.read()
match = pattern.search(data)
if match:
tags.append(match.group(1))
for file in CORE_COMPONENTS_PATH.rglob("*.cpp"):
data = file.read_text()
match = pattern.search(data)
if match:
tags.append(match.group(1))
return tags

View File

@@ -6,6 +6,7 @@ import collections
import fnmatch
import functools
import os.path
from pathlib import Path
import re
import sys
import time
@@ -75,12 +76,12 @@ ignore_types = (
LINT_FILE_CHECKS = []
LINT_CONTENT_CHECKS = []
LINT_POST_CHECKS = []
EXECUTABLE_BIT = {}
EXECUTABLE_BIT: dict[str, int] = {}
errors = collections.defaultdict(list)
errors: collections.defaultdict[Path, list] = collections.defaultdict(list)
def add_errors(fname, errs):
def add_errors(fname: Path, errs: list[tuple[int, int, str] | None]) -> None:
if not isinstance(errs, list):
errs = [errs]
for err in errs:
@@ -246,8 +247,8 @@ def lint_ext_check(fname):
".github/copilot-instructions.md",
]
)
def lint_executable_bit(fname):
ex = EXECUTABLE_BIT[fname]
def lint_executable_bit(fname: Path) -> str | None:
ex = EXECUTABLE_BIT[str(fname)]
if ex != 100644:
return (
f"File has invalid executable bit {ex}. If running from a windows machine please "
@@ -506,8 +507,8 @@ def lint_constants_usage():
return errs
def relative_cpp_search_text(fname, content):
parts = fname.split("/")
def relative_cpp_search_text(fname: Path, content) -> str:
parts = fname.parts
integration = parts[2]
return f'#include "esphome/components/{integration}'
@@ -524,8 +525,8 @@ def lint_relative_cpp_import(fname, line, col, content):
)
def relative_py_search_text(fname, content):
parts = fname.split("/")
def relative_py_search_text(fname: Path, content: str) -> str:
parts = fname.parts
integration = parts[2]
return f"esphome.components.{integration}"
@@ -591,10 +592,8 @@ def lint_relative_py_import(fname, line, col, content):
"esphome/components/http_request/httplib.h",
],
)
def lint_namespace(fname, content):
expected_name = re.match(
r"^esphome/components/([^/]+)/.*", fname.replace(os.path.sep, "/")
).group(1)
def lint_namespace(fname: Path, content: str) -> str | None:
expected_name = fname.parts[2]
# Check for both old style and C++17 nested namespace syntax
search_old = f"namespace {expected_name}"
search_new = f"namespace esphome::{expected_name}"
@@ -733,9 +732,9 @@ def main():
files.sort()
for fname in files:
_, ext = os.path.splitext(fname)
fname = Path(fname)
run_checks(LINT_FILE_CHECKS, fname, fname)
if ext in ignore_types:
if fname.suffix in ignore_types:
continue
try:
with codecs.open(fname, "r", encoding="utf-8") as f_handle:

View File

@@ -52,10 +52,10 @@ def styled(color: str | tuple[str, ...], msg: str, reset: bool = True) -> str:
return prefix + msg + suffix
def print_error_for_file(file: str, body: str | None) -> None:
def print_error_for_file(file: str | Path, body: str | None) -> None:
print(
styled(colorama.Fore.GREEN, "### File ")
+ styled((colorama.Fore.GREEN, colorama.Style.BRIGHT), file)
+ styled((colorama.Fore.GREEN, colorama.Style.BRIGHT), str(file))
)
print()
if body is not None:
@@ -513,7 +513,7 @@ def get_all_dependencies(component_names: set[str]) -> set[str]:
# Set up fake config path for component loading
root = Path(__file__).parent.parent
CORE.config_path = str(root)
CORE.config_path = root
CORE.data[KEY_CORE] = {}
# Keep finding dependencies until no new ones are found
@@ -553,7 +553,7 @@ def get_components_from_integration_fixtures() -> set[str]:
fixtures_dir = Path(__file__).parent.parent / "tests" / "integration" / "fixtures"
for yaml_file in fixtures_dir.glob("*.yaml"):
config: dict[str, any] | None = yaml_util.load_yaml(str(yaml_file))
config: dict[str, any] | None = yaml_util.load_yaml(yaml_file)
if not config:
continue

View File

@@ -50,7 +50,7 @@ def create_components_graph():
root = Path(__file__).parent.parent
components_dir = root / "esphome" / "components"
# Fake some directory so that get_component works
CORE.config_path = str(root)
CORE.config_path = root
# Various configuration to capture different outcomes used by `AUTO_LOAD` function.
TARGET_CONFIGURATIONS = [
{KEY_TARGET_FRAMEWORK: None, KEY_TARGET_PLATFORM: None},