Skip to content

Commit

Permalink
feat: added support for identifiers.org namespace patterns with unico…
Browse files Browse the repository at this point in the history
…de categories
  • Loading branch information
jonrkarr committed Jan 10, 2022
1 parent d07ce12 commit 9ed221f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion biosimulators_utils/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.156'
__version__ = '0.1.157'
21 changes: 18 additions & 3 deletions biosimulators_utils/utils/identifiers_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"""

from ..config import get_app_dirs
from ..warnings import warn, BioSimulatorsWarning
import dataclasses
import datetime
import dateutil.parser
import functools
import os.path
import re
import regex as re
import requests_cache
import typing

Expand Down Expand Up @@ -95,12 +96,13 @@ def get_identifiers_org_namespaces():
a tuple of its provider code and its prefix to its attributes
"""
filename = os.path.join(get_app_dirs().user_cache_dir, 'identifiers-org')
session = requests_cache.CachedSession(filename, expire_after=7*24*60*60)
session = requests_cache.CachedSession(filename, expire_after=7 * 24 * 60 * 60)

response = session.get(NAMESPACES_ENDPOINT)
response.raise_for_status()

namespaces = {}
skipped_namespaces = []
for namespace in response.json()['payload']['namespaces']:
resources = []
for resource in namespace['resources']:
Expand Down Expand Up @@ -133,13 +135,21 @@ def get_identifiers_org_namespaces():
deprecated_date=dateutil.parser.parse(resource['deprecationDate']) if resource['deprecationDate'] else None,
))

try:
pattern = re.compile(namespace['pattern'])
except re.error as exception:
msg = "'{}' (prefix '{}'): '{}' is not valid: {}.".format(
namespace['name'], namespace['prefix'], namespace['pattern'], str(exception))
skipped_namespaces.append(msg)
continue

namespace_obj = IdentifiersOrgNamespace(
id=namespace['id'],
mir_id=namespace['mirId'],
prefix=namespace['prefix'],
name=namespace['name'],
description=namespace['description'],
pattern=re.compile(namespace['pattern']),
pattern=pattern,
embedded_in_lui=namespace['namespaceEmbeddedInLui'],
sample_id=namespace['sampleId'],
resources=resources,
Expand All @@ -152,6 +162,11 @@ def get_identifiers_org_namespaces():
for resource in namespace['resources']:
namespaces[resource['providerCode'].lower() + '/' + namespace['prefix'].lower()] = namespace_obj

if skipped_namespaces:
msg = '{} namespaces will not be validated because their regular expression patterns are not valid:\n - {}'.format(
len(skipped_namespaces), '\n - '.join(sorted(skipped_namespaces)))
warn(msg, BioSimulatorsWarning)

return namespaces


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ python_libcombine >= 0.2.11
python_libsedml >= 2.0.16
pyyaml
rdflib
regex
requests
requests_cache
setuptools
Expand Down

0 comments on commit 9ed221f

Please sign in to comment.