about summary refs log tree commit diff
path: root/scripts/cli
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/cli')
-rw-r--r--scripts/cli/__init__.py3
-rw-r--r--scripts/cli/logging.py18
-rw-r--r--scripts/cli/options.py46
-rw-r--r--scripts/cli/validators.py10
4 files changed, 77 insertions, 0 deletions
diff --git a/scripts/cli/__init__.py b/scripts/cli/__init__.py
new file mode 100644
index 0000000..45bbda9
--- /dev/null
+++ b/scripts/cli/__init__.py
@@ -0,0 +1,3 @@
+"""Package to hold CLI-specific utilities."""
+
+from . import options
diff --git a/scripts/cli/logging.py b/scripts/cli/logging.py
new file mode 100644
index 0000000..30ecf17
--- /dev/null
+++ b/scripts/cli/logging.py
@@ -0,0 +1,18 @@
+"""Logging for scripts."""
+import logging
+
+def setup_logging(
+        script_logger: logging.Logger,
+        loglevel: str,
+        modules: tuple[str, ...] = tuple()
+):
+    """Setup module-level loggers to the same log-level as the application."""
+    logging.basicConfig(
+        encoding="utf-8",
+        format=("%(asctime)s — %(filename)s:%(lineno)s — %(levelname)s: "
+                "%(message)s"),
+        level=logging.INFO)
+    script_logger.setLevel(getattr(logging, loglevel.upper()))
+    effective_loglevel = logging.getLevelName(script_logger.getEffectiveLevel())
+    for module in modules:
+        logging.getLogger(module).setLevel(effective_loglevel)
diff --git a/scripts/cli/options.py b/scripts/cli/options.py
new file mode 100644
index 0000000..67f35dc
--- /dev/null
+++ b/scripts/cli/options.py
@@ -0,0 +1,46 @@
+"""General options to be added to CLI scripts."""
+from argparse import ArgumentParser
+
+
+def add_logging(parser: ArgumentParser) -> ArgumentParser:
+    """Add optional log-level option"""
+    loglevels = ("debug", "info", "warning", "error", "critical")
+    parser.add_argument(
+        "--log_level",
+        "--log-level",
+        "--loglevel",
+        metavar="LOG-LEVEL",
+        type=str,
+        default="INFO",
+        choices=loglevels,
+        help=(f"Controls the severity of events to log. Valid values are: " +
+              ", ".join(f"'{level}'" for level in loglevels)))
+    return parser
+
+
+def add_mariadb_uri(parser: ArgumentParser) -> ArgumentParser:
+    """Add the MySQL/MariaDB URI argument."""
+    parser.add_argument("db_uri",
+                        metavar="DB-URI",
+                        type=str,
+                        help="MariaDB/MySQL connection URL")
+    return parser
+
+
+def add_species_id(parser: ArgumentParser) -> ArgumentParser:
+    """Add species-id as a mandatory argument."""
+    parser.add_argument("species_id",
+                        metavar="SPECIES-ID",
+                        type=int,
+                        help="The species to operate on.")
+    return parser
+
+
+def add_population_id(parser: ArgumentParser) -> ArgumentParser:
+    """Add population-id as a mandatory argument."""
+    parser = add_species_id(parser)
+    parser.add_argument("population_id",
+                        metavar="POPULATION-ID",
+                        type=int,
+                        help="The ID for the population to operate on.")
+    return parser
diff --git a/scripts/cli/validators.py b/scripts/cli/validators.py
new file mode 100644
index 0000000..6d16e4c
--- /dev/null
+++ b/scripts/cli/validators.py
@@ -0,0 +1,10 @@
+"""CLI options validators."""
+from pathlib import Path
+
+
+def directory_exists(val: str) -> Path:
+    """Check that directory path specified actually exists."""
+    _dir = Path(val).absolute()
+    if _dir.is_dir() and _dir.exists():
+        return _dir
+    raise FileNotFoundError(f"The path '{_dir}' MUST exist and be a directory.")