use pathlib.Path for directories and files
Some checks failed
Ruff / ruff (push) Waiting to run
Pytest / pytest (3.12) (push) Successful in 1m15s
Pytest / pytest (3.13) (push) Has been cancelled
Mypy / mypy (push) Successful in 1m13s
Pytest / pytest (3.14) (push) Has been cancelled

This commit is contained in:
Justus Kuhlmann 2026-03-23 16:15:55 +01:00
commit 8162758cec
Signed by: jkuhl
GPG key ID: 00ED992DD79B85A6
13 changed files with 137 additions and 125 deletions

View file

@ -19,6 +19,7 @@ from .meas_io import write_measurement
import os
from .input.implementations import codes as known_codes
from typing import Any
from pathlib import Path
def replace_string(string: str, name: str, val: str) -> str:
@ -126,7 +127,7 @@ def check_measurement_data(measurements: dict[str, dict[str, str]], code: str) -
return
def import_tomls(path: str, files: list[str], copy_files: bool=True) -> None:
def import_tomls(path: Path, files: list[str], copy_files: bool=True) -> None:
"""
Import multiple toml files.
@ -144,7 +145,7 @@ def import_tomls(path: str, files: list[str], copy_files: bool=True) -> None:
return
def import_toml(path: str, file: str, copy_file: bool=True) -> None:
def import_toml(path: Path, file: str, copy_file: bool=True) -> None:
"""
Import a project decribed by a .toml file.
@ -171,7 +172,7 @@ def import_toml(path: str, file: str, copy_file: bool=True) -> None:
aliases = project.get('aliases', [])
uuid = project.get('uuid', None)
if uuid is not None:
if not os.path.exists(path + "/projects/" + uuid):
if not os.path.exists(path / "projects" / uuid):
uuid = import_project(path, project['url'], aliases=aliases)
else:
update_aliases(path, uuid, aliases)
@ -213,18 +214,18 @@ def import_toml(path: str, file: str, copy_file: bool=True) -> None:
write_measurement(path, ensemble, measurement, uuid, project['code'], (md['param_file'] if 'param_file' in md else None))
print(mname + " imported.")
if not os.path.exists(os.path.join(path, "toml_imports", uuid)):
os.makedirs(os.path.join(path, "toml_imports", uuid))
if not os.path.exists(path / "toml_imports" / uuid):
os.makedirs(path / "toml_imports" / uuid)
if copy_file:
import_file = os.path.join(path, "toml_imports", uuid, file.split("/")[-1])
import_file = path / "toml_imports" / uuid / file.split("/")[-1]
shutil.copy(file, import_file)
save(path, files=[import_file], message="Import using " + import_file)
print("File copied to " + import_file)
save(path, files=[import_file], message=f"Import using {import_file}")
print(f"File copied to {import_file}")
print("Imported project.")
return
def reimport_project(path: str, uuid: str) -> None:
def reimport_project(path: Path, uuid: str) -> None:
"""
Reimport an existing project using the files that are already available for this project.
@ -235,14 +236,14 @@ def reimport_project(path: str, uuid: str) -> None:
uuid: str
uuid of the project that is to be reimported.
"""
config_path = "/".join([path, "import_scripts", uuid])
config_path = path / "import_scripts" / uuid
for p, filenames, dirnames in os.walk(config_path):
for fname in filenames:
import_toml(path, os.path.join(config_path, fname), copy_file=False)
return
def update_project(path: str, uuid: str) -> None:
def update_project(path: Path, uuid: str) -> None:
"""
Update all entries associated with a given project.