From c305e3e308ec1673776b2882af43d1de65e83436 Mon Sep 17 00:00:00 2001 From: Mahfuza Humayra Mohona Date: Wed, 21 Aug 2024 07:20:22 +0600 Subject: [PATCH 1/2] add version --- src/scribe_data/cli/main.py | 7 +++- src/scribe_data/cli/version.py | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/scribe_data/cli/version.py diff --git a/src/scribe_data/cli/main.py b/src/scribe_data/cli/main.py index 05310f96f..f9bd5b66a 100644 --- a/src/scribe_data/cli/main.py +++ b/src/scribe_data/cli/main.py @@ -28,6 +28,7 @@ from scribe_data.cli.interactive import start_interactive_mode from scribe_data.cli.list import list_wrapper from scribe_data.cli.total import get_total_lexemes +from scribe_data.cli.version import get_version_message LIST_DESCRIPTION = "List languages, data types and combinations of each that Scribe-Data can be used for." GET_DESCRIPTION = ( @@ -50,7 +51,11 @@ def main() -> None: parser._actions[0].help = "Show this help message and exit." parser.add_argument( - "-v", "--version", help="Show the version of the Scribe-Data CLI." + "-v", + "--version", + action="version", + version=f"{get_version_message()}", + help="Show the local and latest versions of the Scribe-Data CLI.", ) parser.add_argument("-u", "--upgrade", help="Upgrade the Scribe-Data CLI.") diff --git a/src/scribe_data/cli/version.py b/src/scribe_data/cli/version.py new file mode 100644 index 000000000..d315a4695 --- /dev/null +++ b/src/scribe_data/cli/version.py @@ -0,0 +1,62 @@ +""" +Functions for checking current version of the Scribe-Data CLI. + +.. raw:: html + +""" + +import pkg_resources +import requests + + +def get_local_version(): + try: + return pkg_resources.get_distribution("scribe-data").version + except pkg_resources.DistributionNotFound: + return "Unknown (Not installed via pip)" + + +def get_latest_version(): + try: + response = requests.get( + "https://api.github.com/repos/scribe-org/Scribe-Data/releases/latest" + ) + version = response.json()["name"] + return version + except Exception: + return "Unknown (Unable to fetch version)" + + +def get_version_message(): + local_version = "Scribe-Data v" + get_local_version() + latest_version = get_latest_version() + + if ( + local_version == "Unknown (Not installed via pip)" + or latest_version == "Unknown (Unable to fetch version)" + ): + return f"{local_version}" + + if local_version == latest_version: + return f"{local_version}" + else: + update_message = ( + f"Scribe-Data v{local_version} (Update available: v{latest_version})\n" + ) + update_message += "To update, run: pip scribe-data --upgrade" + return update_message From a480bbb58f8979f6969d2cb7bd53376aa3335ab6 Mon Sep 17 00:00:00 2001 From: Andrew Tavis McAllister Date: Wed, 21 Aug 2024 23:56:08 +0200 Subject: [PATCH 2/2] Changelog entry and minor code improvements --- CHANGELOG.md | 1 + src/scribe_data/cli/version.py | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3161bacd..d01eacf73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Emojis for the following are chosen based on [gitmoji](https://gitmoji.dev/). - Querying Wikidata lexicographical data can be done via the `--query` command ([#159](https://github.com/scribe-org/Scribe-Data/issues/159)). - The output type of queries can be in JSON, CSV, TSV and SQLite, with conversions output types also being possible ([#145](https://github.com/scribe-org/Scribe-Data/issues/145), [#146](https://github.com/scribe-org/Scribe-Data/issues/146)) - Output paths can be set for query results ([#144](https://github.com/scribe-org/Scribe-Data/issues/144)). + - The version of the CLI can be printed to the command line ([#186](https://github.com/scribe-org/Scribe-Data/issues/186)). - Total Wikidata lexemes for languages and data types can be derived with the `--total` command ([#147](https://github.com/scribe-org/Scribe-Data/issues/147)). - Commands can be used via an interactive mode with the `--interactive` command ([#158](https://github.com/scribe-org/Scribe-Data/issues/158)). - Articles are removed from machine translations so they're more directly useful in Scribe applications ([#96](https://github.com/scribe-org/Scribe-Data/issues/96)). diff --git a/src/scribe_data/cli/version.py b/src/scribe_data/cli/version.py index d315a4695..15fa90daa 100644 --- a/src/scribe_data/cli/version.py +++ b/src/scribe_data/cli/version.py @@ -27,6 +27,7 @@ def get_local_version(): try: return pkg_resources.get_distribution("scribe-data").version + except pkg_resources.DistributionNotFound: return "Unknown (Not installed via pip)" @@ -36,14 +37,14 @@ def get_latest_version(): response = requests.get( "https://api.github.com/repos/scribe-org/Scribe-Data/releases/latest" ) - version = response.json()["name"] - return version + return response.json()["name"] + except Exception: return "Unknown (Unable to fetch version)" def get_version_message(): - local_version = "Scribe-Data v" + get_local_version() + local_version = f"Scribe-Data v{get_local_version()}" latest_version = get_latest_version() if ( @@ -54,9 +55,10 @@ def get_version_message(): if local_version == latest_version: return f"{local_version}" - else: - update_message = ( - f"Scribe-Data v{local_version} (Update available: v{latest_version})\n" - ) - update_message += "To update, run: pip scribe-data --upgrade" - return update_message + + update_message = ( + f"Scribe-Data v{local_version} (Update available: v{latest_version})\n" + ) + update_message += "To update: pip scribe-data --upgrade" + + return update_message