From 005c00bd4971ca722ec6cbda125cf0b965b0fbec Mon Sep 17 00:00:00 2001 From: nanglo123 Date: Fri, 27 Sep 2024 15:05:14 -0400 Subject: [PATCH] Add get_version_pins method to utils, remove final VERSION_PINS variable --- src/pyobo/api/utils.py | 45 ++++++++++++++++++++++++++++++++++++-- src/pyobo/constants.py | 44 ------------------------------------- tests/test_version_pins.py | 5 ++--- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/pyobo/api/utils.py b/src/pyobo/api/utils.py index 2d683006..11c9d263 100644 --- a/src/pyobo/api/utils.py +++ b/src/pyobo/api/utils.py @@ -3,18 +3,22 @@ """Utilities for high-level API.""" import json +import logging +import os from typing import Optional import bioversions -from ..constants import VERSION_PINS from ..utils.path import prefix_directory_join __all__ = [ "get_version", + "get_version_pins", "VersionError", ] +logger = logging.getLogger(__name__) + class VersionError(ValueError): """A catch-all for version getting failure.""" @@ -27,7 +31,7 @@ def get_version(prefix: str) -> Optional[str]: :return: The version if available else None """ # Prioritize loaded environmental variable VERSION_PINS dictionary - version = VERSION_PINS.get(prefix) + version = get_version_pins().get(prefix) if version: return version try: @@ -46,3 +50,40 @@ def get_version(prefix: str) -> Optional[str]: return data["version"] return None + + +def get_version_pins(): + """Retrieve the resource version pins.""" + try: + version_pins_str = os.getenv("VERSION_PINS") + if not version_pins_str: + version_pins = {} + else: + version_pins = json.loads(version_pins_str) + invalid_prefixes = [] + for prefix, version in version_pins.items(): + if not isinstance(prefix, str) or not isinstance(version, str): + logger.error( + f"The prefix:{prefix} and version:{version} name must both be strings" + ) + invalid_prefixes.append(prefix) + for prefix in invalid_prefixes: + version_pins.pop(prefix) + except ValueError as e: + logger.error( + "The value for the environment variable VERSION_PINS must be a valid JSON string: %s" + % e + ) + version_pins = {} + + if version_pins: + logger.debug( + f"These are the resource versions that are pinned.\n" + f"{version_pins}. " + f"\nPyobo will download the latest version of a resource if it's " + f"not pinned.\nIf you want to use a specific version of a " + f"resource, edit your VERSION_PINS environmental " + f"variable which is a JSON string to include a prefix and version " + f"name." + ) + return version_pins diff --git a/src/pyobo/constants.py b/src/pyobo/constants.py index 3c7fe1cb..16e6d4e1 100644 --- a/src/pyobo/constants.py +++ b/src/pyobo/constants.py @@ -2,9 +2,7 @@ """Constants for PyOBO.""" -import json import logging -import os import re import pystow @@ -13,8 +11,6 @@ "RAW_DIRECTORY", "DATABASE_DIRECTORY", "SPECIES_REMAPPING", - "VERSION_PINS", - "get_version_pins", ] logger = logging.getLogger(__name__) @@ -102,43 +98,3 @@ "isbn", "issn", } - - -def get_version_pins(): - """Retrieve the resource version pins.""" - try: - version_pins_str = os.getenv("VERSION_PINS") - if not version_pins_str: - version_pins = {} - else: - version_pins = json.loads(version_pins_str) - invalid_prefixes = [] - for prefix, version in version_pins.items(): - if not isinstance(prefix, str) or not isinstance(version, str): - logger.error( - f"The prefix:{prefix} and version:{version} name must both be strings" - ) - invalid_prefixes.append(prefix) - for prefix in invalid_prefixes: - version_pins.pop(prefix) - except ValueError as e: - logger.error( - "The value for the environment variable VERSION_PINS must be a valid JSON string: %s" - % e - ) - version_pins = {} - - if version_pins: - logger.debug( - f"These are the resource versions that are pinned.\n" - f"{version_pins}. " - f"\nPyobo will download the latest version of a resource if it's " - f"not pinned.\nIf you want to use a specific version of a " - f"resource, edit your VERSION_PINS environmental " - f"variable which is a JSON string to include a prefix and version " - f"name." - ) - return version_pins - - -VERSION_PINS = get_version_pins() diff --git a/tests/test_version_pins.py b/tests/test_version_pins.py index d9708c08..82d26f49 100644 --- a/tests/test_version_pins.py +++ b/tests/test_version_pins.py @@ -5,10 +5,9 @@ import unittest from unittest import mock -from pyobo.api.utils import get_version -from pyobo.constants import get_version_pins +from pyobo.api.utils import get_version, get_version_pins -MOCK_VERSION_PINS = '{"ncbitaxon": "2024-05-08", "vo":"2024-04-09", ' '"chebi":"235", "bfo":5}' +MOCK_VERSION_PINS = '{"ncbitaxon": "2024-07-03", "vo":"2024-04-09", ' '"chebi":"235", "bfo":5}' @mock.patch.dict(os.environ, {"VERSION_PINS": MOCK_VERSION_PINS})