From 5c344e56e5ae77be155389dcfc5e98c34a536739 Mon Sep 17 00:00:00 2001 From: Carlos Rueda Date: Tue, 19 Mar 2024 13:15:21 -0700 Subject: [PATCH] add `--version` option to the programs --- ChangeLog.md | 2 + pbp/__init__.py | 61 ++++++----------------------- pbp/json_generator/corrector.py | 2 +- pbp/json_generator/gen_iclisten.py | 2 +- pbp/json_generator/gen_nrs.py | 2 +- pbp/json_generator/gen_soundtrap.py | 2 +- pbp/main_args.py | 9 +++++ pbp/main_json_generator_args.py | 8 ++++ pbp/plot.py | 7 ++++ pbp/process_helper.py | 30 +++++++++++++- 10 files changed, 71 insertions(+), 54 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 601b148..eacad93 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,6 +2,8 @@ 2024-03 +- added `--version` option to the programs +- minor code reorg - enabled poetry - now packaging and publishing to PyPI - TODO ongoing: README/justfile adjustments, etc. diff --git a/pbp/__init__.py b/pbp/__init__.py index a351632..d4f6d73 100644 --- a/pbp/__init__.py +++ b/pbp/__init__.py @@ -1,49 +1,14 @@ -import os - -import xarray as xr - -from pbp.logging_helper import PbpLogger - - -def save_dataset_to_netcdf(logger: PbpLogger, ds: xr.Dataset, filename: str) -> bool: - logger.info(f" - saving dataset to: {filename}") - try: - ds.to_netcdf( - filename, - engine="h5netcdf", - encoding={ - "effort": {"_FillValue": None}, - "frequency": {"_FillValue": None}, - "sensitivity": {"_FillValue": None}, - }, - ) - return True - except Exception as e: # pylint: disable=broad-exception-caught - error = f"Unable to save {filename}: {e}" - logger.error(error) - print(error) - return False - - -def save_dataset_to_csv(logger: PbpLogger, ds: xr.Dataset, filename: str): - logger.info(f" - saving dataset to: {filename}") +def get_pbp_version() -> str: try: - ds.to_pandas().to_csv(filename, float_format="%.1f") - except Exception as e: # pylint: disable=broad-exception-caught - logger.error(f"Unable to save {filename}: {e}") - - -def get_cpus_to_use(num_cpus: int) -> int: - cpu_count: int = os.cpu_count() or 1 - if num_cpus <= 0 or num_cpus > cpu_count: - num_cpus = cpu_count - return num_cpus - - -class PBPException(Exception): - """ - Placeholder for a more specific exception. - """ - - def __init__(self, msg: str): - super().__init__(f"PBPException({msg})") + import importlib.metadata + + return importlib.metadata.version("mbari-pbp") + except Exception: # pylint: disable=broad-exception-caught + try: + from poetry.core.factory import Factory + + factory = Factory() + poetry = factory.create_poetry() + return poetry.package.version.text + except Exception: # pylint: disable=broad-exception-caught + return "version not found" diff --git a/pbp/json_generator/corrector.py b/pbp/json_generator/corrector.py index ec9b908..5716ee6 100644 --- a/pbp/json_generator/corrector.py +++ b/pbp/json_generator/corrector.py @@ -12,7 +12,7 @@ import tempfile import json -from pbp import PbpLogger +from pbp.logging_helper import PbpLogger class MetadataCorrector: diff --git a/pbp/json_generator/gen_iclisten.py b/pbp/json_generator/gen_iclisten.py index 2e45ec7..d358171 100644 --- a/pbp/json_generator/gen_iclisten.py +++ b/pbp/json_generator/gen_iclisten.py @@ -13,7 +13,7 @@ import pbp.json_generator.utils as utils from pbp.json_generator.corrector import MetadataCorrector from pbp.json_generator.metadata_extractor import IcListenWavFile -from pbp import PbpLogger +from pbp.logging_helper import PbpLogger from pbp.json_generator.gen_abstract import MetadataGeneratorAbstract diff --git a/pbp/json_generator/gen_nrs.py b/pbp/json_generator/gen_nrs.py index aa21dad..2c61b83 100644 --- a/pbp/json_generator/gen_nrs.py +++ b/pbp/json_generator/gen_nrs.py @@ -12,7 +12,7 @@ from progressbar import progressbar from pbp.json_generator.corrector import MetadataCorrector from pbp.json_generator.metadata_extractor import FlacFile -from pbp import PbpLogger +from pbp.logging_helper import PbpLogger from pbp.json_generator.gen_abstract import MetadataGeneratorAbstract from pbp.json_generator.utils import parse_s3_or_gcp_url diff --git a/pbp/json_generator/gen_soundtrap.py b/pbp/json_generator/gen_soundtrap.py index 0ce57c3..6180fbf 100644 --- a/pbp/json_generator/gen_soundtrap.py +++ b/pbp/json_generator/gen_soundtrap.py @@ -13,7 +13,7 @@ from pathlib import Path from progressbar import progressbar -from pbp import PbpLogger +from pbp.logging_helper import PbpLogger from pbp.json_generator.gen_abstract import MetadataGeneratorAbstract from pbp.json_generator.metadata_extractor import SoundTrapWavFile from pbp.json_generator.corrector import MetadataCorrector diff --git a/pbp/main_args.py b/pbp/main_args.py index ab975a3..533750a 100644 --- a/pbp/main_args.py +++ b/pbp/main_args.py @@ -1,6 +1,9 @@ from argparse import ArgumentParser, RawTextHelpFormatter +from pbp import get_pbp_version + + def parse_arguments(): description = "PyPAM based processing of Pacific Sound data." example = """ @@ -15,6 +18,12 @@ def parse_arguments(): description=description, epilog=example, formatter_class=RawTextHelpFormatter ) + parser.add_argument( + "--version", + action="version", + version=get_pbp_version(), + ) + parser.add_argument( "--json-base-dir", type=str, diff --git a/pbp/main_json_generator_args.py b/pbp/main_json_generator_args.py index 9ce10d3..408196b 100644 --- a/pbp/main_json_generator_args.py +++ b/pbp/main_json_generator_args.py @@ -1,5 +1,7 @@ from argparse import ArgumentParser, RawTextHelpFormatter +from pbp import get_pbp_version + def parse_arguments(): description = ( @@ -22,6 +24,12 @@ def parse_arguments(): description=description, epilog=example, formatter_class=RawTextHelpFormatter ) + parser.add_argument( + "--version", + action="version", + version=get_pbp_version(), + ) + class InstrumentType: NRS = "NRS" ICLISTEN = "ICLISTEN" diff --git a/pbp/plot.py b/pbp/plot.py index d6f563d..aae1313 100644 --- a/pbp/plot.py +++ b/pbp/plot.py @@ -7,6 +7,7 @@ DEFAULT_YLIM, DEFAULT_CMLIM, ) +from pbp import get_pbp_version def parse_arguments(): @@ -14,6 +15,12 @@ def parse_arguments(): parser = ArgumentParser(description=description, formatter_class=RawTextHelpFormatter) + parser.add_argument( + "--version", + action="version", + version=get_pbp_version(), + ) + parser.add_argument( "--latlon", type=float, diff --git a/pbp/process_helper.py b/pbp/process_helper.py index 5c78b5f..1963a7c 100644 --- a/pbp/process_helper.py +++ b/pbp/process_helper.py @@ -7,8 +7,6 @@ import numpy as np import xarray as xr -from pbp import save_dataset_to_csv, save_dataset_to_netcdf - from pbp.file_helper import FileHelper from pbp.logging_helper import PbpLogger from pbp.metadata import MetadataHelper, parse_attributes, replace_snippets @@ -312,3 +310,31 @@ def _get_global_attributes(self, year: int, month: int, day: int): for k, v in global_attrs.items(): snippets["{{" + k + "}}"] = v return replace_snippets(global_attrs, snippets) + + +def save_dataset_to_netcdf(logger: PbpLogger, ds: xr.Dataset, filename: str) -> bool: + logger.info(f" - saving dataset to: {filename}") + try: + ds.to_netcdf( + filename, + engine="h5netcdf", + encoding={ + "effort": {"_FillValue": None}, + "frequency": {"_FillValue": None}, + "sensitivity": {"_FillValue": None}, + }, + ) + return True + except Exception as e: # pylint: disable=broad-exception-caught + error = f"Unable to save {filename}: {e}" + logger.error(error) + print(error) + return False + + +def save_dataset_to_csv(logger: PbpLogger, ds: xr.Dataset, filename: str): + logger.info(f" - saving dataset to: {filename}") + try: + ds.to_pandas().to_csv(filename, float_format="%.1f") + except Exception as e: # pylint: disable=broad-exception-caught + logger.error(f"Unable to save {filename}: {e}")