Skip to content

Commit

Permalink
Merge pull request #77 from ielis:improve-release-tag-handling
Browse files Browse the repository at this point in the history
Improve ontology release tag handling
  • Loading branch information
ielis authored Dec 12, 2024
2 parents 7d8bca6 + 10317b6 commit f1b8895
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/hpotk/store/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class OntologyReleaseService(metaclass=abc.ABCMeta):

@abc.abstractmethod
def fetch_tags(
self,
ontology_type: OntologyType,
self,
ontology_type: OntologyType,
) -> typing.Iterable[str]:
"""
Fetch sequence of tags for an ontology.
Expand All @@ -100,10 +100,10 @@ class OntologyStore:
"""

def __init__(
self,
store_dir: str,
ontology_release_service: OntologyReleaseService,
remote_ontology_service: RemoteOntologyService,
self,
store_dir: str,
ontology_release_service: OntologyReleaseService,
remote_ontology_service: RemoteOntologyService,
):
self._logger = logging.getLogger(__name__)
self._store_dir = store_dir
Expand Down
8 changes: 4 additions & 4 deletions src/hpotk/store/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@


def configure_ontology_store(
store_dir: typing.Optional[str] = None,
ontology_release_service: OntologyReleaseService = GitHubOntologyReleaseService(),
remote_ontology_service: RemoteOntologyService = GitHubRemoteOntologyService(),
store_dir: typing.Optional[str] = None,
ontology_release_service: OntologyReleaseService = GitHubOntologyReleaseService(),
remote_ontology_service: RemoteOntologyService = GitHubRemoteOntologyService(),
) -> OntologyStore:
"""
Configure and create the default ontology store.
Expand All @@ -28,7 +28,7 @@ def configure_ontology_store(
store_dir = get_default_ontology_store_dir()
else:
if not os.path.isdir(store_dir):
raise ValueError(f'`store_dir` must point to an existing directory')
raise ValueError('`store_dir` must point to an existing directory')
return OntologyStore(
store_dir=store_dir,
ontology_release_service=ontology_release_service,
Expand Down
38 changes: 27 additions & 11 deletions src/hpotk/store/_github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import json
import logging
import re
import ssl
import typing
from urllib.request import urlopen
Expand All @@ -10,22 +11,31 @@
from ._api import OntologyType, OntologyReleaseService, RemoteOntologyService


production_tag_pt = r'^v(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$'
"""
A tag pattern to ensure we only include the "production" tags (e.g. not `v2024-12-12X`).
"""


ONTOLOGY_CREDENTIALS = {
OntologyType.HPO: {
'owner': 'obophenotype',
'repo': 'human-phenotype-ontology',
'tag_pt': production_tag_pt,
},
OntologyType.MAxO: {
'owner': 'monarch-initiative',
'repo': 'MAxO',
'tag_pt': production_tag_pt,
},
OntologyType.MONDO: {
'owner': 'monarch-initiative',
'repo': 'mondo',
'tag_pt': production_tag_pt,
},
}
"""
The default ontology credentials that only include HPO at the time.
The default ontology credentials that only include HPO, MAxO, and MONDO at this time.
"""


Expand All @@ -35,9 +45,9 @@ class GitHubOntologyReleaseService(OntologyReleaseService):
"""

def __init__(
self,
timeout: int = 10,
ontology_credentials: typing.Mapping[OntologyType, typing.Mapping[str, str]] = ONTOLOGY_CREDENTIALS,
self,
timeout: int = 10,
ontology_credentials: typing.Mapping[OntologyType, typing.Mapping[str, str]] = ONTOLOGY_CREDENTIALS,
):
self._logger = logging.getLogger(__name__)
self._timeout = timeout
Expand All @@ -56,20 +66,22 @@ def fetch_tags(self, ontology_type: OntologyType) -> typing.Iterable[str]:
return self._get_tag_names(
owner=credentials['owner'],
repo=credentials['repo'],
tag_pt=credentials['tag_pt'],
)

def _get_tag_names(
self,
owner: str,
repo: str,
self,
owner: str,
repo: str,
tag_pt: str,
) -> typing.Iterable[str]:
tag_url = self._tag_api_url.format(owner=owner, repo=repo)
self._logger.debug('Pulling tag from %s', tag_url)

with urlopen(
tag_url,
timeout=self._timeout,
context=self._ctx,
tag_url,
timeout=self._timeout,
context=self._ctx,
) as fh:
tags = json.load(fh)

Expand All @@ -78,7 +90,11 @@ def _get_tag_names(
else:
self._logger.debug('Fetched %d tags', len(tags))

return (tag['name'] for tag in tags)
pattern = re.compile(tag_pt)
return filter(
lambda tag: pattern.match(tag),
(tag['name'] for tag in tags),
)


class GitHubRemoteOntologyService(RemoteOntologyService):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ def initialize_store_dir(store_dir: Path):
mondo_path.joinpath("x.txt").touch()
mondo_path.joinpath("y.txt").touch()

@pytest.mark.skip("Just for manual debugging")
def test_resolve_store_path__latest(
self,
ontology_store: hpotk.OntologyStore,
):
latest = ontology_store.resolve_store_path(hpotk.store.OntologyType.HPO)
print(latest)


@pytest.mark.online
class TestGitHubOntologyStoreOnline:
"""
Expand Down

0 comments on commit f1b8895

Please sign in to comment.