use pathlib.Path for directories and files
This commit is contained in:
parent
110ddaf3a1
commit
8162758cec
13 changed files with 137 additions and 125 deletions
|
|
@ -10,9 +10,13 @@ from .tools import get_db_file, cache_enabled
|
|||
from .tracker import get, save, unlock
|
||||
import shutil
|
||||
from typing import Any
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def write_measurement(path: str, ensemble: str, measurement: dict[str, dict[str, dict[str, Any]]], uuid: str, code: str, parameter_file: Union[str, None]) -> None:
|
||||
CACHE_DIR = ".cache"
|
||||
|
||||
|
||||
def write_measurement(path: Path, ensemble: str, measurement: dict[str, dict[str, dict[str, Any]]], uuid: str, code: str, parameter_file: Union[str, None]) -> None:
|
||||
"""
|
||||
Write a measurement to the backlog.
|
||||
If the file for the measurement already exists, update the measurement.
|
||||
|
|
@ -33,7 +37,7 @@ def write_measurement(path: str, ensemble: str, measurement: dict[str, dict[str,
|
|||
The parameter file used for the measurement.
|
||||
"""
|
||||
db_file = get_db_file(path)
|
||||
db = os.path.join(path, db_file)
|
||||
db = path / db_file
|
||||
|
||||
files_to_save = []
|
||||
|
||||
|
|
@ -44,11 +48,11 @@ def write_measurement(path: str, ensemble: str, measurement: dict[str, dict[str,
|
|||
conn = sqlite3.connect(db)
|
||||
c = conn.cursor()
|
||||
for corr in measurement.keys():
|
||||
file_in_archive = os.path.join('.', 'archive', ensemble, corr, uuid + '.json.gz')
|
||||
file = os.path.join(path, file_in_archive)
|
||||
file_in_archive = Path('.') / 'archive' / ensemble / corr / str(uuid + '.json.gz')
|
||||
file = path / file_in_archive
|
||||
known_meas = {}
|
||||
if not os.path.exists(os.path.join(path, '.', 'archive', ensemble, corr)):
|
||||
os.makedirs(os.path.join(path, '.', 'archive', ensemble, corr))
|
||||
if not os.path.exists(path / 'archive' / ensemble / corr):
|
||||
os.makedirs(path / 'archive' / ensemble / corr)
|
||||
files_to_save.append(file_in_archive)
|
||||
else:
|
||||
if os.path.exists(file):
|
||||
|
|
@ -99,7 +103,7 @@ def write_measurement(path: str, ensemble: str, measurement: dict[str, dict[str,
|
|||
pars[subkey] = json.dumps(parameters)
|
||||
for subkey in subkeys:
|
||||
parHash = sha256(str(pars[subkey]).encode('UTF-8')).hexdigest()
|
||||
meas_path = file_in_archive + "::" + parHash
|
||||
meas_path = str(file_in_archive) + "::" + parHash
|
||||
|
||||
known_meas[parHash] = measurement[corr][subkey]
|
||||
|
||||
|
|
@ -115,7 +119,7 @@ def write_measurement(path: str, ensemble: str, measurement: dict[str, dict[str,
|
|||
return
|
||||
|
||||
|
||||
def load_record(path: str, meas_path: str) -> Union[Corr, Obs]:
|
||||
def load_record(path: Path, meas_path: str) -> Union[Corr, Obs]:
|
||||
"""
|
||||
Load a list of records by their paths.
|
||||
|
||||
|
|
@ -134,7 +138,7 @@ def load_record(path: str, meas_path: str) -> Union[Corr, Obs]:
|
|||
return load_records(path, [meas_path])[0]
|
||||
|
||||
|
||||
def load_records(path: str, meas_paths: list[str], preloaded: dict[str, Any] = {}) -> list[Union[Corr, Obs]]:
|
||||
def load_records(path: Path, meas_paths: list[str], preloaded: dict[str, Any] = {}) -> list[Union[Corr, Obs]]:
|
||||
"""
|
||||
Load a list of records by their paths.
|
||||
|
||||
|
|
@ -162,11 +166,11 @@ def load_records(path: str, meas_paths: list[str], preloaded: dict[str, Any] = {
|
|||
returned_data: list[Any] = []
|
||||
for file in needed_data.keys():
|
||||
for key in list(needed_data[file]):
|
||||
if os.path.exists(cache_path(path, file, key) + ".p"):
|
||||
returned_data.append(load_object(cache_path(path, file, key) + ".p"))
|
||||
if os.path.exists(str(cache_path(path, file, key)) + ".p"):
|
||||
returned_data.append(load_object(str(cache_path(path, file, key)) + ".p"))
|
||||
else:
|
||||
if file not in preloaded:
|
||||
preloaded[file] = preload(path, file)
|
||||
preloaded[file] = preload(path, Path(file))
|
||||
returned_data.append(preloaded[file][key])
|
||||
if cache_enabled(path):
|
||||
if not os.path.exists(cache_dir(path, file)):
|
||||
|
|
@ -175,7 +179,7 @@ def load_records(path: str, meas_paths: list[str], preloaded: dict[str, Any] = {
|
|||
return returned_data
|
||||
|
||||
|
||||
def cache_dir(path: str, file: str) -> str:
|
||||
def cache_dir(path: Path, file: str) -> Path:
|
||||
"""
|
||||
Returns the directory corresponding to the cache for the given file.
|
||||
|
||||
|
|
@ -190,14 +194,14 @@ def cache_dir(path: str, file: str) -> str:
|
|||
cache_path: str
|
||||
The path holding the cached data for the given file.
|
||||
"""
|
||||
cache_path_list = [path]
|
||||
cache_path_list.append(".cache")
|
||||
cache_path_list.extend(file.split("/")[1:])
|
||||
cache_path = "/".join(cache_path_list)
|
||||
cache_path_list = file.split("/")[1:]
|
||||
cache_path = path / CACHE_DIR
|
||||
for directory in cache_path_list:
|
||||
cache_path /= directory
|
||||
return cache_path
|
||||
|
||||
|
||||
def cache_path(path: str, file: str, key: str) -> str:
|
||||
def cache_path(path: Path, file: str, key: str) -> Path:
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
|
|
@ -213,11 +217,11 @@ def cache_path(path: str, file: str, key: str) -> str:
|
|||
cache_path: str
|
||||
The path at which the measurement of the given file and key is cached.
|
||||
"""
|
||||
cache_path = os.path.join(cache_dir(path, file), key)
|
||||
cache_path = cache_dir(path, file) / key
|
||||
return cache_path
|
||||
|
||||
|
||||
def preload(path: str, file: str) -> dict[str, Any]:
|
||||
def preload(path: Path, file: Path) -> dict[str, Any]:
|
||||
"""
|
||||
Read the contents of a file into a json dictionary with the pyerrors.json.load_json_dict method.
|
||||
|
||||
|
|
@ -234,12 +238,12 @@ def preload(path: str, file: str) -> dict[str, Any]:
|
|||
The data read from the file.
|
||||
"""
|
||||
get(path, file)
|
||||
filedict: dict[str, Any] = pj.load_json_dict(os.path.join(path, file))
|
||||
filedict: dict[str, Any] = pj.load_json_dict(path / file)
|
||||
print("> read file")
|
||||
return filedict
|
||||
|
||||
|
||||
def drop_record(path: str, meas_path: str) -> None:
|
||||
def drop_record(path: Path, meas_path: str) -> None:
|
||||
"""
|
||||
Drop a record by it's path.
|
||||
|
||||
|
|
@ -251,9 +255,9 @@ def drop_record(path: str, meas_path: str) -> None:
|
|||
The measurement path as noted in the database.
|
||||
"""
|
||||
file_in_archive = meas_path.split("::")[0]
|
||||
file = os.path.join(path, file_in_archive)
|
||||
file = path / file_in_archive
|
||||
db_file = get_db_file(path)
|
||||
db = os.path.join(path, db_file)
|
||||
db = path / db_file
|
||||
get(path, db_file)
|
||||
sub_key = meas_path.split("::")[1]
|
||||
unlock(path, db_file)
|
||||
|
|
@ -268,7 +272,7 @@ def drop_record(path: str, meas_path: str) -> None:
|
|||
known_meas = pj.load_json_dict(file)
|
||||
if sub_key in known_meas:
|
||||
del known_meas[sub_key]
|
||||
unlock(path, file_in_archive)
|
||||
unlock(path, Path(file_in_archive))
|
||||
pj.dump_dict_to_json(known_meas, file)
|
||||
save(path, message="Drop measurements to database", files=[db, file])
|
||||
return
|
||||
|
|
@ -276,7 +280,7 @@ def drop_record(path: str, meas_path: str) -> None:
|
|||
raise ValueError("This measurement does not exist as a file!")
|
||||
|
||||
|
||||
def drop_cache(path: str) -> None:
|
||||
def drop_cache(path: Path) -> None:
|
||||
"""
|
||||
Drop the cache directory of the library.
|
||||
|
||||
|
|
@ -285,7 +289,7 @@ def drop_cache(path: str) -> None:
|
|||
path: str
|
||||
The path of the library.
|
||||
"""
|
||||
cache_dir = os.path.join(path, ".cache")
|
||||
cache_dir = path / ".cache"
|
||||
for f in os.listdir(cache_dir):
|
||||
shutil.rmtree(os.path.join(cache_dir, f))
|
||||
shutil.rmtree(cache_dir / f)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue