2023-07-21 14:54:09 +01:00
|
|
|
# © Crown-owned copyright 2023, Defence Science and Technology Laboratory UK
|
2023-06-07 22:40:16 +01:00
|
|
|
"""Provides a CLI using Typer as an entry point."""
|
2023-06-09 22:23:45 +01:00
|
|
|
import logging
|
2023-06-07 22:40:16 +01:00
|
|
|
import os
|
2023-06-09 22:23:45 +01:00
|
|
|
from enum import Enum
|
|
|
|
|
from typing import Optional
|
2023-06-07 22:40:16 +01:00
|
|
|
|
|
|
|
|
import typer
|
2023-06-09 22:23:45 +01:00
|
|
|
import yaml
|
2023-06-09 16:44:49 +01:00
|
|
|
from typing_extensions import Annotated
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-07-21 14:00:50 +01:00
|
|
|
from primaite import PRIMAITE_PATHS
|
2023-06-30 09:08:13 +01:00
|
|
|
|
2023-06-07 22:40:16 +01:00
|
|
|
app = typer.Typer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-07-14 12:01:38 +01:00
|
|
|
def build_dirs() -> None:
|
2023-06-07 22:40:16 +01:00
|
|
|
"""Build the PrimAITE app directories."""
|
2023-07-28 13:49:26 +01:00
|
|
|
from primaite import PRIMAITE_PATHS
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-07-28 13:49:26 +01:00
|
|
|
PRIMAITE_PATHS.mkdirs()
|
2023-06-07 22:40:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-07-14 12:01:38 +01:00
|
|
|
def reset_notebooks(overwrite: bool = True) -> None:
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
|
|
|
|
Force a reset of the demo notebooks in the users notebooks directory.
|
|
|
|
|
|
|
|
|
|
:param overwrite: If True, will overwrite existing demo notebooks.
|
|
|
|
|
"""
|
|
|
|
|
from primaite.setup import reset_demo_notebooks
|
|
|
|
|
|
|
|
|
|
reset_demo_notebooks.run(overwrite)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-07-14 12:01:38 +01:00
|
|
|
def logs(last_n: Annotated[int, typer.Option("-n")]) -> None:
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
|
|
|
|
Print the PrimAITE log file.
|
|
|
|
|
|
|
|
|
|
:param last_n: The number of lines to print. Default value is 10.
|
|
|
|
|
"""
|
|
|
|
|
import re
|
2023-06-27 13:06:10 +01:00
|
|
|
|
2023-07-21 14:00:50 +01:00
|
|
|
from primaite import PRIMAITE_PATHS
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-07-21 14:00:50 +01:00
|
|
|
if os.path.isfile(PRIMAITE_PATHS.app_log_file_path):
|
|
|
|
|
with open(PRIMAITE_PATHS.app_log_file_path) as file:
|
2023-06-07 22:40:16 +01:00
|
|
|
lines = file.readlines()
|
|
|
|
|
for line in lines[-last_n:]:
|
|
|
|
|
print(re.sub(r"\n*", "", line))
|
|
|
|
|
|
|
|
|
|
|
2023-06-30 16:52:57 +01:00
|
|
|
_LogLevel = Enum("LogLevel", {k: k for k in logging._levelToName.values()}) # noqa
|
2023-06-09 22:23:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-07-14 12:01:38 +01:00
|
|
|
def log_level(level: Annotated[Optional[_LogLevel], typer.Argument()] = None) -> None:
|
2023-06-09 22:23:45 +01:00
|
|
|
"""
|
|
|
|
|
View or set the PrimAITE Log Level.
|
|
|
|
|
|
|
|
|
|
To View, simply call: primaite log-level
|
|
|
|
|
|
|
|
|
|
To set, call: primaite log-level <desired log level>
|
|
|
|
|
|
|
|
|
|
For example, to set the to debug, call: primaite log-level DEBUG
|
|
|
|
|
"""
|
2023-07-21 14:00:50 +01:00
|
|
|
if PRIMAITE_PATHS.app_config_file_path.exists():
|
|
|
|
|
with open(PRIMAITE_PATHS.app_config_file_path, "r") as file:
|
2023-06-09 22:23:45 +01:00
|
|
|
primaite_config = yaml.safe_load(file)
|
|
|
|
|
|
|
|
|
|
if level:
|
2023-06-30 09:08:13 +01:00
|
|
|
primaite_config["logging"]["log_level"] = level.value
|
2023-07-21 14:00:50 +01:00
|
|
|
with open(PRIMAITE_PATHS.app_config_file_path, "w") as file:
|
2023-06-09 22:23:45 +01:00
|
|
|
yaml.dump(primaite_config, file)
|
2023-06-30 09:08:13 +01:00
|
|
|
print(f"PrimAITE Log Level: {level}")
|
2023-06-09 22:23:45 +01:00
|
|
|
else:
|
2023-06-30 09:08:13 +01:00
|
|
|
level = primaite_config["logging"]["log_level"]
|
2023-06-09 22:23:45 +01:00
|
|
|
print(f"PrimAITE Log Level: {level}")
|
|
|
|
|
|
|
|
|
|
|
2023-06-07 22:40:16 +01:00
|
|
|
@app.command()
|
2023-07-14 12:01:38 +01:00
|
|
|
def version() -> None:
|
2023-06-07 22:40:16 +01:00
|
|
|
"""Get the installed PrimAITE version number."""
|
|
|
|
|
import primaite
|
|
|
|
|
|
|
|
|
|
print(primaite.__version__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-07-14 12:01:38 +01:00
|
|
|
def setup(overwrite_existing: bool = True) -> None:
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
|
|
|
|
Perform the PrimAITE first-time setup.
|
|
|
|
|
|
|
|
|
|
WARNING: All user-data will be lost.
|
|
|
|
|
"""
|
|
|
|
|
from primaite import getLogger
|
2023-10-27 12:20:23 +01:00
|
|
|
from primaite.setup import reset_demo_notebooks, reset_example_configs
|
2023-06-07 22:40:16 +01:00
|
|
|
|
|
|
|
|
_LOGGER = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
_LOGGER.info("Performing the PrimAITE first-time setup...")
|
|
|
|
|
|
2023-06-30 11:40:26 +01:00
|
|
|
_LOGGER.info("Building primaite_config.yaml...")
|
2023-06-09 15:49:48 +01:00
|
|
|
|
2023-06-07 22:40:16 +01:00
|
|
|
_LOGGER.info("Building the PrimAITE app directories...")
|
2023-07-21 14:00:50 +01:00
|
|
|
PRIMAITE_PATHS.mkdirs()
|
2023-06-07 22:40:16 +01:00
|
|
|
|
|
|
|
|
_LOGGER.info("Rebuilding the demo notebooks...")
|
|
|
|
|
reset_demo_notebooks.run(overwrite_existing=True)
|
|
|
|
|
|
|
|
|
|
_LOGGER.info("Rebuilding the example notebooks...")
|
|
|
|
|
reset_example_configs.run(overwrite_existing=True)
|
|
|
|
|
|
|
|
|
|
_LOGGER.info("PrimAITE setup complete!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-07-28 13:49:26 +01:00
|
|
|
def session(
|
2023-10-27 12:20:23 +01:00
|
|
|
config: Optional[str] = None,
|
2023-07-28 13:49:26 +01:00
|
|
|
) -> None:
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
|
|
|
|
Run a PrimAITE session.
|
|
|
|
|
|
2023-10-27 14:26:52 +01:00
|
|
|
:param config: The path to the config file. Optional, if None, the example config will be used.
|
|
|
|
|
:type config: Optional[str]
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
2023-10-27 14:26:52 +01:00
|
|
|
from primaite.config.load import example_config_path
|
|
|
|
|
from primaite.main import run
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-10-27 14:26:52 +01:00
|
|
|
if not config:
|
|
|
|
|
config = example_config_path()
|
|
|
|
|
print(config)
|
|
|
|
|
run(config_path=config)
|