Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use curies package for default implementation #23

Merged
merged 8 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 26 additions & 5 deletions prefixcommons/curie_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Union

import curies
import requests
from curies import Converter

PREFIX_MAP = Dict[str, Any]

Expand Down Expand Up @@ -100,9 +102,14 @@ def read_biocontext(name: str) -> PREFIX_MAP:
]


default_converter = curies.chain(
Converter.from_prefix_map(prefix_map) for prefix_map in default_curie_maps
)


def get_prefixes(cmaps: Optional[List[PREFIX_MAP]] = None) -> List[str]:
if cmaps is None:
cmaps = default_curie_maps
return list(default_converter.get_prefixes())
prefixes = []
for cmap in cmaps:
prefixes += cmap.keys()
Expand Down Expand Up @@ -136,7 +143,15 @@ def contract_uri(

"""
if cmaps is None:
cmaps = default_curie_maps
# TODO warn if not shortest?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e., this implementation is not compatible with the argument shortest=False since tries only return the longest

curie = default_converter.compress(uri)
if curie is not None:
return [curie]
elif strict:
raise NoPrefix(uri)
else:
return []

curies = set()
for cmap in cmaps:
for (k, v) in cmap.items():
Expand Down Expand Up @@ -164,17 +179,23 @@ def expand_uri(id: str, cmaps: Optional[List[PREFIX_MAP]] = None, strict: bool =
prefix, localid = id.split(":", 1)
except ValueError:
if strict:
raise InvalidSyntax(id)
raise InvalidSyntax(id) from None
else:
return id

if cmaps is None:
cmaps = default_curie_maps
uri = default_converter.expand(curie=id)
if uri is not None:
return uri
elif strict:
raise NoExpansion(prefix, localid)
else:
return id

for cmap in cmaps:
if prefix in cmap:
return cmap[prefix] + localid
if strict:
raise NoExpansion(prefix, id)
raise NoExpansion(prefix, localid)
else:
return id
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ requests = "^2.28.1"
click = "^8.1.3"
pytest-logging = "^2015.11.4"
PyYAML = "^6.0"
curies = "^0.1.5"

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
Expand Down