corrlib/corrlib/cli.py

171 lines
3.7 KiB
Python

from typing import Optional
import typer
from corrlib import __app_name__, __version__
from .initialization import create
from .toml import import_tomls, update_project, reimport_project
from .find import find_record, list_projects
from .tools import str2list
from .main import update_aliases
import os
app = typer.Typer()
def _version_callback(value: bool) -> None:
if value:
typer.echo(f"{__app_name__} v{__version__}")
raise typer.Exit()
@app.command()
def update(
path: str = typer.Option(
str('./corrlib'),
"--dataset",
"-d",
),
uuid: str = typer.Argument(),
) -> None:
"""
Update a project by it's UUID.
"""
update_project(path, uuid)
return
@app.command()
def list(
path: str = typer.Option(
str('./corrlib'),
"--dataset",
"-d",
),
entities: str = typer.Argument('ensembles'),
) -> None:
"""
List entities. (ensembles, projects)
"""
if entities in ['ensembles', 'Ensembles','ENSEMBLES']:
print("Ensembles:")
for item in os.listdir(path + "/archive"):
if os.path.isdir(os.path.join(path + "/archive", item)):
print(item)
elif entities == 'projects':
results = list_projects(path)
print("Projects:")
header = "UUID".ljust(37) + "| Aliases"
print(header)
for project in 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: str = typer.Option(
str('./corrlib'),
"--dataset",
"-d",
),
uuid: str = typer.Argument(),
alias: str = typer.Argument(),
) -> None:
"""
Add an alias to a project UUID.
"""
alias_list = alias.pülit(",")
update_aliases(path, uuid, alias_list)
return
@app.command()
def find(
path: str = typer.Option(
str('./corrlib'),
"--dataset",
"-d",
),
ensemble: str = typer.Argument(),
corr: str = typer.Argument(),
) -> None:
"""
Find a record in the backlog at hand. Through specifying it's ensemble and the measured correlator.
"""
results = find_record(path, ensemble, corr)
print(results)
@app.command()
def importer(
path: str = typer.Option(
str('./corrlib'),
"--dataset",
"-d",
),
files: str = typer.Argument(
),
copy_file: bool = typer.Option(
bool(True),
"--save",
"-s",
),
) -> None:
"""
Import a project from a .toml-file via CLI.
"""
file_list = files.split(",")
import_tomls(path, file_list, copy_file)
return
@app.command()
def reimporter(
path: str = typer.Option(
str('./corrlib'),
"--dataset",
"-d",
),
ident: str = typer.Argument()
) -> None:
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)
return
@app.command()
def init(
path: str = typer.Option(
str('./corrlib'),
"--dataset",
"-d",
),
) -> None:
"""
Initialize a new backlog-database.
"""
create(path)
return
@app.callback()
def main(
version: Optional[bool] = typer.Option(
None,
"--version",
"-v",
help="Show the application's version and exit.",
callback=_version_callback,
is_eager=True,
)
) -> None:
return