From 3b7dfd2462710debab1cb5294b0eff26c9a80531 Mon Sep 17 00:00:00 2001 From: Deepak Mallubhotla Date: Sat, 20 Apr 2024 16:39:33 -0500 Subject: [PATCH] feat: adds click cli app --- poetry.lock | 13 +++++------ pyproject.toml | 4 ++++ tantri/cli/__init__.py | 34 +++++++++++++++++++++++++++++ tantri/cli/file_importer.py | 15 +++++++++++++ tantri/cli/input_files/json_dots.py | 1 + tantri/dipoles/__init__.py | 4 ++-- tests/cli/test_read_dots_json.py | 0 7 files changed, 61 insertions(+), 10 deletions(-) create mode 100755 tantri/cli/__init__.py create mode 100755 tantri/cli/file_importer.py create mode 100755 tantri/cli/input_files/json_dots.py create mode 100755 tests/cli/test_read_dots_json.py diff --git a/poetry.lock b/poetry.lock index 2e4d623..2d92d40 100644 --- a/poetry.lock +++ b/poetry.lock @@ -55,7 +55,7 @@ uvloop = ["uvloop (>=0.15.2)"] name = "click" version = "8.1.7" description = "Composable command line interface toolkit" -category = "dev" +category = "main" optional = false python-versions = ">=3.7" @@ -66,13 +66,13 @@ colorama = {version = "*", markers = "platform_system == \"Windows\""} name = "colorama" version = "0.4.6" description = "Cross-platform colored terminal text." -category = "dev" +category = "main" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" [[package]] name = "coverage" -version = "7.3.2" +version = "7.4.4" description = "Code coverage measurement for Python" category = "dev" optional = false @@ -492,7 +492,7 @@ python-versions = "*" [metadata] lock-version = "1.1" python-versions = ">=3.8.1,<3.10" -content-hash = "b5275c33449e8f85acbf9c0f6dfe1ec4e7296adfa16360d782b33534e1223638" +content-hash = "66c63e7a7a61bc34880f47e2568e2407f330c2691dabc0dace9aa15f03a97531" [metadata.files] appnope = [] @@ -510,10 +510,7 @@ iniconfig = [] ipython = [] jedi = [] matplotlib-inline = [] -mccabe = [ - {file = "mccabe-0.6.1-py2.py3-none-any.whl", hash = "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42"}, - {file = "mccabe-0.6.1.tar.gz", hash = "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f"}, -] +mccabe = [] mypy = [] mypy-extensions = [] numpy = [] diff --git a/pyproject.toml b/pyproject.toml index fcf224b..1f1d0ff 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ readme = "README.md" python = ">=3.8.1,<3.10" numpy = "^1.22.3" scipy = "~1.10" +click = "^8.1.7" [tool.poetry.dev-dependencies] pytest = ">=6" @@ -40,3 +41,6 @@ module = [ "scipy.optimize" ] ignore_missing_imports = true + +[tool.poetry.scripts] +tantri = "tantri.cli:cli" diff --git a/tantri/cli/__init__.py b/tantri/cli/__init__.py new file mode 100755 index 0000000..74bc613 --- /dev/null +++ b/tantri/cli/__init__.py @@ -0,0 +1,34 @@ +import click +import logging +import tantri + +_logger = logging.getLogger(__name__) + +LOG_PATTERN = "%(asctime)s | %(levelname)-7s | %(name)s | %(message)s" + + +def _set_up_logging(filename): + logging.basicConfig( + level=logging.DEBUG, + format=LOG_PATTERN, + handlers=[logging.StreamHandler(), logging.FileHandler(filename)], + ) + logging.getLogger("pdme").setLevel(logging.INFO) + logging.captureWarnings(True) + + +@click.group() +@click.version_option(tantri.get_version()) +@click.option("--log-file", help="A filename to use for logging") +def cli(log_file): + print("hi") + if log_file is not None: + # log file has been provided, let's log + _set_up_logging(log_file) + _logger.info("cli") + + +@cli.command() +def hello(): + _logger.info("in hello") + print("in hello") diff --git a/tantri/cli/file_importer.py b/tantri/cli/file_importer.py new file mode 100755 index 0000000..1fca1e3 --- /dev/null +++ b/tantri/cli/file_importer.py @@ -0,0 +1,15 @@ +import json +import logging + +_logger = logging.getLogger(__name__) + +# note that json is insecure by default right? +# but don't worry for now +# TODO: if this ever matters, can improve file handling. + + +def read_json_file(filename): + try: + return json.load(filename) + except Exception: + _logger.exception(f"failed on reading filename {filename}") diff --git a/tantri/cli/input_files/json_dots.py b/tantri/cli/input_files/json_dots.py new file mode 100755 index 0000000..2ee43a1 --- /dev/null +++ b/tantri/cli/input_files/json_dots.py @@ -0,0 +1 @@ +import tantri \ No newline at end of file diff --git a/tantri/dipoles/__init__.py b/tantri/dipoles/__init__.py index 8e864f2..78b458f 100755 --- a/tantri/dipoles/__init__.py +++ b/tantri/dipoles/__init__.py @@ -32,8 +32,8 @@ class Dipole: # For caching purposes tell each dipole where the dots are # TODO: This can be done better by only passing into the time series the non-repeated p s and w, - # and then creating a new wrapper type to include all the cached stuff. - # Realistically, the dot positions and measurement type data should live in the time series. + # TODO: and then creating a new wrapper type to include all the cached stuff. + # TODO: Realistically, the dot positions and measurement type data should live in the time series. dot_positions: typing.Sequence[DotPosition] measurement_type: DipoleMeasurementType diff --git a/tests/cli/test_read_dots_json.py b/tests/cli/test_read_dots_json.py new file mode 100755 index 0000000..e69de29