From f7890eb51c0cc25de7cb10f01dbc9947e820b6e4 Mon Sep 17 00:00:00 2001 From: david-i-berry Date: Wed, 14 Feb 2024 16:13:00 +0100 Subject: [PATCH] Basename (#130) * Bump version number * Back to dev. * Addition of basename to output from list_templates. * Flake 8 * Flake 8 * change of basename to name in returned object (from list_templates) Use of name in matching templates. * Removal of cwd from default paths if CSV2BUFR_TEMPLATES set. * use .stem. --- csv2bufr/__init__.py | 2 +- csv2bufr/templates/__init__.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/csv2bufr/__init__.py b/csv2bufr/__init__.py index 6fdeb3a..10fbfe1 100644 --- a/csv2bufr/__init__.py +++ b/csv2bufr/__init__.py @@ -19,7 +19,7 @@ # ############################################################################### -__version__ = '0.8.0' +__version__ = '0.8.1' import csv from datetime import timezone, datetime diff --git a/csv2bufr/templates/__init__.py b/csv2bufr/templates/__init__.py index 3a9697e..1265e6b 100644 --- a/csv2bufr/templates/__init__.py +++ b/csv2bufr/templates/__init__.py @@ -29,7 +29,7 @@ THISDIR = os.path.dirname(os.path.realpath(__file__)) LOGGER = logging.getLogger(__name__) SCHEMA = f"{THISDIR}{os.sep}resources{os.sep}schema" -TEMPLATE_DIRS = [Path("./")] +TEMPLATE_DIRS = [] # [Path("./")] _SUCCESS_ = True @@ -47,10 +47,20 @@ LOGGER.error(msg) raise RuntimeError(msg) +_template_flag = False # Set user defined location first if 'CSV2BUFR_TEMPLATES' in os.environ: TEMPLATE_DIRS.append(Path(os.environ['CSV2BUFR_TEMPLATES'])) else: + TEMPLATE_DIRS.append(Path("./")) + _template_flag = True + +# Check if /opt/csv2bur/templates exists and add to search path +if Path("/opt/csv2bufr/templates").exists() and \ + "/opt/csv2bufr/templates" not in TEMPLATE_DIRS: + TEMPLATE_DIRS.append(Path("/opt/csv2bufr/templates")) + +if _template_flag: LOGGER.warning(f"""CSV2BUFR_TEMPLATES is not set, default search path(s) will be used ({TEMPLATE_DIRS}).""") @@ -81,7 +91,7 @@ def load_template(template_name: str) -> Union[dict, None]: msg = f"Requested template {template_name} not found, " +\ "searching by file name" for _template in TEMPLATES.values(): - if template_name in _template.get('path'): + if template_name == _template.get('name'): fname = _template.get('path') break if fname is None: @@ -182,7 +192,8 @@ def index_templates() -> bool: "author": tmpl['metadata'].get("author", ""), "dateCreated": tmpl['metadata'].get("dateCreated", ""), # noqa "id": tmpl['metadata'].get("id", ""), - "path": fname + "path": fname, + "name": Path(fname).stem } except Exception as e: