Skip to content

Commit

Permalink
Merge pull request #186 from mhmohona/version
Browse files Browse the repository at this point in the history
Add `-version` and `upgrade` for CLI
  • Loading branch information
andrewtavis committed Aug 21, 2024
2 parents b82bb67 + a480bbb commit e9abdee
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
7 changes: 6 additions & 1 deletion src/scribe_data/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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.")

Expand Down
64 changes: 64 additions & 0 deletions src/scribe_data/cli/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Functions for checking current version of the Scribe-Data CLI.
.. raw:: html
<!--
* Copyright (C) 2024 Scribe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
"""

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"
)
return response.json()["name"]

except Exception:
return "Unknown (Unable to fetch version)"


def get_version_message():
local_version = f"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}"

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

0 comments on commit e9abdee

Please sign in to comment.