Skip to content

Commit

Permalink
Add get_version_pins method to utils, remove final VERSION_PINS variable
Browse files Browse the repository at this point in the history
  • Loading branch information
nanglo123 committed Sep 27, 2024
1 parent 2af67dd commit 005c00b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 49 deletions.
45 changes: 43 additions & 2 deletions src/pyobo/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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:
Expand All @@ -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
44 changes: 0 additions & 44 deletions src/pyobo/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

"""Constants for PyOBO."""

import json
import logging
import os
import re

import pystow
Expand All @@ -13,8 +11,6 @@
"RAW_DIRECTORY",
"DATABASE_DIRECTORY",
"SPECIES_REMAPPING",
"VERSION_PINS",
"get_version_pins",
]

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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()
5 changes: 2 additions & 3 deletions tests/test_version_pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit 005c00b

Please sign in to comment.