From e863317fe61d5538d67d2836071c6a4a61f97f4c Mon Sep 17 00:00:00 2001 From: Mike Gouline <1960272+gouline@users.noreply.github.com> Date: Sat, 24 Jul 2021 16:46:54 +1000 Subject: [PATCH] Optional version import --- dbtmetabase/__init__.py | 8 +++++++- dbtmetabase/utils.py | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 dbtmetabase/utils.py diff --git a/dbtmetabase/__init__.py b/dbtmetabase/__init__.py index 30ea7b72..eae480ae 100644 --- a/dbtmetabase/__init__.py +++ b/dbtmetabase/__init__.py @@ -6,10 +6,11 @@ from .metabase import MetabaseClient from .parsers.dbt_folder import DbtFolderReader from .parsers.dbt_manifest import DbtManifestReader +from .utils import get_version from typing import Iterable, List, Union -from ._version import version as __version__ +__version__ = get_version() def export( @@ -122,6 +123,11 @@ def main(args: List = None): parser = argparse.ArgumentParser( description="Model synchronization from dbt to Metabase." ) + parser.add_argument( + "--version", + action="version", + version=f"%(prog)s {__version__}", + ) parser.add_argument("command", choices=["export"], help="command to execute") # dbt arguments diff --git a/dbtmetabase/utils.py b/dbtmetabase/utils.py new file mode 100644 index 00000000..86fb4133 --- /dev/null +++ b/dbtmetabase/utils.py @@ -0,0 +1,24 @@ +import logging +import importlib.metadata + + +def get_version() -> str: + """Checks _version.py or build metadata for package version. + + Returns: + str: Version string. + """ + + try: + from ._version import version + + return version + except ModuleNotFoundError: + logging.debug("No _version.py found") + + try: + return importlib.metadata.version("dbt-metabase") + except importlib.metadata.PackageNotFoundError: + logging.warning("No version found in metadata") + + return "0.0.0-UNKONWN"