corrlib/corrlib/cli.py

250 lines
5.3 KiB
Python

import os
from importlib.metadata import version
from pathlib import Path
import typer
from corrlib import __app_name__
from .find import find_record, get_stat, list_ensembles, list_projects
from .initialization import create
from .integrity import full_integrity_check
from .main import update_aliases
from .meas_io import drop_cache as mio_drop_cache
from .toml import import_tomls, reimport_project, update_project
from .tools import str2list
app = typer.Typer()
def _version_callback(value: bool) -> None:
if value:
print(__app_name__, version(__app_name__))
raise typer.Exit()
@app.command()
def update(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
uuid: str = typer.Argument(),
) -> None:
"""
Update a project by it's UUID.
"""
update_project(path, uuid)
return
@app.command()
def lister(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
entities: str = typer.Argument('ensembles'),
) -> None:
"""
List entities. (ensembles, projects)
"""
if entities in ['ensembles', 'Ensembles','ENSEMBLES']:
print("Ensembles:")
ensemble_results = list_ensembles(path)
for e in ensemble_results:
print(e)
elif entities == 'projects':
project_results = list_projects(path)
print("Projects:")
header = "UUID".ljust(37) + "| Aliases"
print(header)
for project in project_results:
if project[1] is not None:
aliases = " | ".join(str2list(project[1]))
else:
aliases = "---"
print(project[0], "|", aliases)
return
@app.command()
def alias_add(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
uuid: str = typer.Argument(),
alias: str = typer.Argument(),
) -> None:
"""
Add an alias to a project UUID.
"""
alias_list = alias.split(",")
update_aliases(path, uuid, alias_list)
return
@app.command()
def find(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
ensemble: str = typer.Argument(),
corr: str = typer.Argument(),
code: str = typer.Argument(),
arg: str = typer.Option(
'all',
"--argument",
"-a",
),
) -> None:
"""
Find a record in the given backlog.
"""
results = find_record(path, ensemble, corr, code)
if results.empty:
return
if arg == 'all':
print(results)
else:
if arg == 'stat':
for r in results['path'].values:
stat = get_stat(path, r)
print(stat)
return
for r in results[arg].values:
print(r)
@app.command()
def stat(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
record_id: str = typer.Argument(),
) -> None:
"""
Show the statistics of a given record.
"""
statistics = get_stat(path, record_id)
print(statistics)
return
@app.command()
def check(path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
) -> None:
"""
Check the integrity of the repository.
"""
full_integrity_check(path)
@app.command()
def importer(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
files: str = typer.Argument(
),
copy_file: bool = typer.Option(
True,
"--save",
"-s",
),
) -> None:
"""
Import a project from a .toml-file via CLI.
"""
file_list = files.split(",")
import_tomls(path, file_list, copy_file)
mio_drop_cache(path)
return
@app.command()
def reimporter(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
ident: str = typer.Argument()
) -> None:
"""
Reimport the toml file identfied by the ident string.
"""
uuid = ident.split("::")[0]
if len(ident.split("::")) > 1:
toml_file = os.path.join(path, "toml_imports", ident.split("::")[1])
if os.path.exists(toml_file):
import_tomls(path, [toml_file], copy_files=False)
else:
raise Exception("This file is not known for this project.")
else:
reimport_project(path, uuid)
mio_drop_cache(path)
return
@app.command()
def init(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
tracker: str = typer.Option(
'datalad',
"--tracker",
"-t",
),
) -> None:
"""
Initialize a new backlog-database.
"""
create(path, tracker)
return
@app.command()
def drop_cache(
path: Path = typer.Option( # noqa: B008
Path('.'),
"--dataset",
"-d",
),
) -> None:
"""
Drop the currect cache directory of the dataset.
"""
mio_drop_cache(path)
return
@app.callback()
def main(
version: bool | None = typer.Option(
None,
"--version",
"-v",
help="Show the application's version and exit.",
callback=_version_callback,
is_eager=True,
)
) -> None:
return