From 5f09278180eb148466def8c3f5d2131be1e2f686 Mon Sep 17 00:00:00 2001 From: Justus Kuhlmann Date: Thu, 15 Aug 2024 16:06:06 +0000 Subject: [PATCH] add toml interface --- backlogger/toml.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 backlogger/toml.py diff --git a/backlogger/toml.py b/backlogger/toml.py new file mode 100644 index 0000000..b5d94dd --- /dev/null +++ b/backlogger/toml.py @@ -0,0 +1,72 @@ +""" +TOML interface +-------------- + +Remember, that keys with dots have to be quoted. +Apart from improting projects yourdelf with python scripts, this package also allows for +the import of projects via TOML. +""" + + +import tomllib as toml +import shutil +from .input import sfcf +from .main import import_project +from .meas_io import write_measurement +import datalad.api as dl +import os + + +def check_project_data(d): + print(d.keys()) + if 'project' not in d.keys() or 'measurements' not in d.keys() or len(list(d.keys())) > 2: + raise ValueError('There should only be two key on the top level, "project" and "measurements"!') + project_data = d['project'] + if 'url' not in project_data.keys(): + raise ValueError('project.url is missing!') + if 'code' not in project_data.keys(): + raise ValueError('project.code is missing!') + if 'measurements' not in d.keys(): + raise ValueError('No measurements to import!') + return + + +def import_toml(path, file): + """ + Import a project decribed by a .toml file. + + Parameters + ---------- + path: str + Path to the backlog directory. + file: str + Path to the description file. + """ + print("Import project as decribed in " + file) + with open(file, 'rb') as fp: + toml_dict = toml.load(fp) + check_project_data(toml_dict) + project = toml_dict['project'] + measurements = toml_dict['measurements'] + uuid = import_project(path, project['url']) + print(measurements.items()) + for mname, md in measurements.items(): + print("Import measurement: " + mname) + ensemble = md['ensemble'] + param = sfcf.read_param(path, uuid, md['param_file']) + if 'names' in md.keys(): + measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param, + version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/', names=md['names']) + else: + measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param, + version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/') + write_measurement(path, ensemble, measurement, uuid, + project['code'], md['param_file']) + print(mname + " imported.") + if not os.path.exists(path + "/toml_imports/" + uuid): + os.makedirs(path + "/toml_imports/" + uuid) + import_file = "toml_imports/" + uuid + "/import.toml" + shutil.copy(file, ) + dl.save(path + import_file, message="Import using " + import_file, dataset=path) + print("Imported project, file copied to " + import_file) + return