add toml interface

This commit is contained in:
Justus Kuhlmann 2024-08-15 16:06:06 +00:00
parent 5407de0aff
commit 5f09278180

72
backlogger/toml.py Normal file
View file

@ -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