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
|
|
|
|
2024-04-25 13:32:57 +01:00
|
|
|
app = typer.Typer(no_args_is_help=True)
|
2023-06-07 22:40:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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!")
|
2024-04-25 13:32:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
|
|
|
|
def mode(
|
|
|
|
|
dev: Annotated[bool, typer.Option("--dev", help="Activates PrimAITE developer mode")] = None,
|
|
|
|
|
prod: Annotated[bool, typer.Option("--prod", help="Activates PrimAITE production mode")] = None,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Switch PrimAITE between developer mode and production mode.
|
|
|
|
|
|
|
|
|
|
By default, PrimAITE will be in production mode.
|
|
|
|
|
|
|
|
|
|
To view the current mode, use: primaite mode
|
|
|
|
|
|
|
|
|
|
To set to development mode, use: primaite mode --dev
|
|
|
|
|
|
|
|
|
|
To return to production mode, use: primaite mode --prod
|
|
|
|
|
"""
|
|
|
|
|
if PRIMAITE_PATHS.app_config_file_path.exists():
|
|
|
|
|
with open(PRIMAITE_PATHS.app_config_file_path, "r") as file:
|
|
|
|
|
primaite_config = yaml.safe_load(file)
|
|
|
|
|
if dev and prod:
|
|
|
|
|
print("Unable to activate developer and production modes concurrently.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if (dev is None) and (prod is None):
|
|
|
|
|
is_dev_mode = primaite_config["developer_mode"]
|
|
|
|
|
|
|
|
|
|
if is_dev_mode:
|
|
|
|
|
print("PrimAITE is running in developer mode.")
|
|
|
|
|
else:
|
|
|
|
|
print("PrimAITE is running in production mode.")
|
|
|
|
|
if dev:
|
|
|
|
|
# activate dev mode
|
|
|
|
|
primaite_config["developer_mode"] = True
|
|
|
|
|
with open(PRIMAITE_PATHS.app_config_file_path, "w") as file:
|
|
|
|
|
yaml.dump(primaite_config, file)
|
|
|
|
|
print("PrimAITE is running in developer mode.")
|
|
|
|
|
if prod:
|
|
|
|
|
# activate prod mode
|
|
|
|
|
primaite_config["developer_mode"] = False
|
|
|
|
|
with open(PRIMAITE_PATHS.app_config_file_path, "w") as file:
|
|
|
|
|
yaml.dump(primaite_config, file)
|
|
|
|
|
print("PrimAITE is running in production mode.")
|