Skip to content

Commit

Permalink
Update records
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Apr 22, 2024
1 parent 2f74f16 commit 5fe2a21
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 39 deletions.
42 changes: 18 additions & 24 deletions docs/make_schema.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,46 @@
import pydantic
import pydantic.schema
from typing import List
"""Generate a JSON schema for extended prefix maps."""

from pydantic import BaseModel, ConfigDict

from curies import Record
from pathlib import Path
import json
from pathlib import Path

from curies import Records
from curies._pydantic_compat import PYDANTIC_V1

HERE = Path(__file__).parent.resolve()
PATH = HERE.joinpath("path.json")
PATH = HERE.joinpath("schema.json")
TITLE = "Extended Prefix Map"
DESCRIPTION = ""
URL = "https://w3id.org/biopragmatics/schema/epm.json"


class Records(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)

class Config:
arbitrary_types_allowed = True

__root__ = List[Record]


def main():
def main() -> None:
"""Generate a JSON schema for extended prefix maps."""
rv = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://bioregistry.io/schema.json",
"$id": URL,
}

try:
if PYDANTIC_V1:
import pydantic.schema

# see https://docs.pydantic.dev/latest/usage/json_schema/#general-notes-on-json-schema-generation
from pydantic.json_schema import models_json_schema
except ImportError:

schema_dict = pydantic.schema.schema(
[Records],
title=TITLE,
description=DESCRIPTION,
)
else:
from pydantic.json_schema import models_json_schema

_, schema_dict = models_json_schema(
[(Records, "validation")],
title=TITLE,
description=DESCRIPTION,
)
rv.update(schema_dict)

PATH.write_text(json.dumps(rv, indent=2))
rv.update(schema_dict)
PATH.write_text(json.dumps(rv, indent=2) + "\n")


if __name__ == "__main__":
Expand Down
63 changes: 63 additions & 0 deletions docs/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://w3id.org/biopragmatics/schema/epm.json",
"$defs": {
"Record": {
"description": "A record of some prefixes and their associated URI prefixes.\n\n.. seealso:: https://github.com/cthoyt/curies/issues/70",
"properties": {
"prefix": {
"description": "The canonical CURIE prefix, used in the reverse prefix map",
"title": "CURIE prefix",
"type": "string"
},
"uri_prefix": {
"description": "The canonical URI prefix, used in the forward prefix map",
"title": "URI prefix",
"type": "string"
},
"prefix_synonyms": {
"items": {
"type": "string"
},
"title": "CURIE prefix synonyms",
"type": "array"
},
"uri_prefix_synonyms": {
"items": {
"type": "string"
},
"title": "URI prefix synonyms",
"type": "array"
},
"pattern": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "The regular expression pattern for entries in this semantic space. Warning: this is an experimental feature.",
"title": "Pattern"
}
},
"required": [
"prefix",
"uri_prefix"
],
"title": "Record",
"type": "object"
},
"Records": {
"description": "A list of records.",
"items": {
"$ref": "#/$defs/Record"
},
"title": "Records",
"type": "array"
}
},
"title": "Extended Prefix Map"
}
2 changes: 2 additions & 0 deletions src/curies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
DuplicateURIPrefixes,
DuplicateValueError,
Record,
Records,
Reference,
ReferenceTuple,
chain,
Expand Down Expand Up @@ -35,6 +36,7 @@
__all__ = [
"Converter",
"Record",
"Records",
"ReferenceTuple",
"Reference",
"DuplicateValueError",
Expand Down
52 changes: 37 additions & 15 deletions src/curies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"Reference",
"ReferenceTuple",
"Record",
"Records",
"DuplicateValueError",
"DuplicatePrefixes",
"DuplicateURIPrefixes",
Expand Down Expand Up @@ -252,26 +253,21 @@ def from_curie(cls, curie: str, sep: str = ":") -> "Reference":
class Record(BaseModel): # type:ignore
"""A record of some prefixes and their associated URI prefixes.
A list of records can be annotated in a FastAPI setting with the following:
.. code-block:: python
from typing import List
from curies import Record
from pydantic import BaseModel
class Records(BaseModel):
__root__ = List[Record]
.. seealso:: https://github.com/cthoyt/curies/issues/70
"""

prefix: str = Field(..., description="The canonical prefix, used in the reverse prefix map")
prefix: str = Field(
...,
title="CURIE prefix",
description="The canonical CURIE prefix, used in the reverse prefix map",
)
uri_prefix: str = Field(
..., description="The canonical URI prefix, used in the forward prefix map"
...,
title="URI prefix",
description="The canonical URI prefix, used in the forward prefix map",
)
prefix_synonyms: List[str] = Field(default_factory=list)
uri_prefix_synonyms: List[str] = Field(default_factory=list)
prefix_synonyms: List[str] = Field(default_factory=list, title="CURIE prefix synonyms")
uri_prefix_synonyms: List[str] = Field(default_factory=list, title="URI prefix synonyms")
pattern: Optional[str] = Field(
default=None,
description="The regular expression pattern for entries in this semantic space. "
Expand Down Expand Up @@ -315,6 +311,32 @@ def _key(self) -> RecordKey:
)


if PYDANTIC_V1:
# An explanation of RootModels in Pydantic V1 can be found on
# https://docs.pydantic.dev/1.10/usage/models/#custom-root-types

from pydantic import BaseModel

class Records(BaseModel):
"""A list of records."""

class Config:
arbitrary_types_allowed = True

__root__: List[Record]

else:
# An explanation of RootModels in Pydantic V2 can be found on
# https://docs.pydantic.dev/latest/concepts/models/#rootmodel-and-custom-root-types

from pydantic import RootModel

class Records(RootModel):
"""A list of records."""

root: List[Record]


class DuplicateSummary(NamedTuple):
"""A triple representing two records that are duplicated, either based on a CURIE or URI prefix."""

Expand Down

0 comments on commit 5fe2a21

Please sign in to comment.