use pathlib

This commit is contained in:
Jörg Thalheim
2024-09-02 17:32:29 +02:00
parent e9536c2be0
commit af0a1dd3f2
9 changed files with 37 additions and 39 deletions

View File

@@ -64,7 +64,7 @@ def _init_proc(
os.setsid()
# Open stdout and stderr
with open(out_file, "w") as out_fd:
with out_file.open("w") as out_fd:
os.dup2(out_fd.fileno(), sys.stdout.fileno())
os.dup2(out_fd.fileno(), sys.stderr.fileno())

View File

@@ -1,3 +1,5 @@
from pathlib import Path
from clan_cli.inventory import (
AdminConfig,
ServiceAdmin,
@@ -25,7 +27,7 @@ def get_admin_service(base_url: str) -> ServiceAdmin | None:
@API.register
def set_admin_service(
base_url: str,
allowed_keys: dict[str, str],
allowed_keys: dict[str, Path],
instance_name: str = "admin",
extra_machines: list[str] | None = None,
) -> None:
@@ -43,10 +45,10 @@ def set_admin_service(
keys = {}
for name, keyfile in allowed_keys.items():
if not keyfile.startswith("/"):
if not keyfile.is_absolute():
msg = f"Keyfile '{keyfile}' must be an absolute path"
raise ValueError(msg)
with open(keyfile) as f:
with keyfile.open() as f:
pubkey = f.read()
keys[name] = pubkey

View File

@@ -69,8 +69,8 @@ def extract_frontmatter(readme_content: str, err_scope: str) -> tuple[Frontmatte
)
def get_roles(module_path: str) -> None | list[str]:
roles_dir = Path(module_path) / "roles"
def get_roles(module_path: Path) -> None | list[str]:
roles_dir = module_path / "roles"
if not roles_dir.exists() or not roles_dir.is_dir():
return None
@@ -117,14 +117,14 @@ def list_modules(base_path: str) -> dict[str, ModuleInfo]:
"""
modules = get_modules(base_path)
return {
module_name: get_module_info(module_name, module_path)
module_name: get_module_info(module_name, Path(module_path))
for module_name, module_path in modules.items()
}
def get_module_info(
module_name: str,
module_path: str,
module_path: Path,
) -> ModuleInfo:
"""
Retrieves information about a module
@@ -136,7 +136,7 @@ def get_module_info(
location=f"show_module_info {module_name}",
description="Module does not exist",
)
module_readme = Path(module_path) / "README.md"
module_readme = module_path / "README.md"
if not module_readme.exists():
msg = "Module not found"
raise ClanError(
@@ -144,7 +144,7 @@ def get_module_info(
location=f"show_module_info {module_name}",
description="Module does not exist or doesn't have any README.md file",
)
with open(module_readme) as f:
with module_readme.open() as f:
readme = f.read()
frontmatter, readme_content = extract_frontmatter(
readme, f"{module_path}/README.md"

View File

@@ -114,7 +114,7 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
)
parser.add_argument(
"path", type=Path, help="Path to the clan directory", default=Path(".")
"path", type=Path, help="Path to the clan directory", default=Path()
)
def create_flake_command(args: argparse.Namespace) -> None:

View File

@@ -172,7 +172,7 @@ def get_or_set_option(args: argparse.Namespace) -> None:
args.flake, machine_name=args.machine, show_trace=args.show_trace
)
else:
with open(args.options_file) as f:
with args.options_file.open() as f:
options = json.load(f)
# compute settings json file location
if args.settings_file is None:

View File

@@ -21,7 +21,7 @@ def list_state_folders(machine: str, service: None | str = None) -> None:
if (clan_dir_result := get_clan_flake_toplevel_or_env()) is not None:
flake = clan_dir_result
else:
flake = Path(".")
flake = Path()
cmd = nix_eval(
[
f"{flake}#nixosConfigurations.{machine}.config.clanCore.state",

View File

@@ -20,8 +20,7 @@ def parse_args() -> argparse.Namespace:
def get_current_shell() -> str | None:
if selected_shell_file.exists():
with open(selected_shell_file) as f:
return f.read().strip()
return selected_shell_file.read_text().strip()
return None
@@ -57,7 +56,7 @@ def select_shell(shell: str) -> None:
print(f"{shell} devshell already selected. No changes made.")
sys.exit(0)
else:
with open(selected_shell_file, "w") as f:
with selected_shell_file.open("w") as f:
f.write(shell)
print(f"{shell} devshell selected")