-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add TSV writer and update other writers' docs #107
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a8f05a0
Improve JSON-LD output documentation
cthoyt 2fcbe86
Update exports
cthoyt a795d98
Cleanup
cthoyt f1a19de
Update api.py
cthoyt d180771
Update api.py
cthoyt 38e3ced
Update docs
cthoyt a98f5c9
Update tutorial.rst
cthoyt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ | |
"write_extended_prefix_map", | ||
"write_jsonld_context", | ||
"write_shacl", | ||
"write_tsv", | ||
] | ||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -2129,29 +2130,83 @@ def _ensure_path(path: Union[str, Path]) -> Path: | |
return path | ||
|
||
|
||
def _get_jsonld_context( | ||
converter: Converter, *, expand: bool = False, include_synonyms: bool = False | ||
) -> Dict[str, Any]: | ||
"""Get a JSON-LD context based on the converter.""" | ||
context = {} | ||
for record in converter.records: | ||
term = _get_expanded_term(record, expand=expand) | ||
context[record.prefix] = term | ||
if include_synonyms: | ||
for prefix_synonym in record.prefix_synonyms: | ||
context[prefix_synonym] = term | ||
return {"@context": context} | ||
|
||
|
||
def write_jsonld_context( | ||
converter: Converter, | ||
path: Union[str, Path], | ||
*, | ||
include_synonyms: bool = False, | ||
expand: bool = False, | ||
) -> None: | ||
"""Write the converter's bijective map as a JSON-LD context to a file.""" | ||
"""Write the converter's bijective map as a JSON-LD context to a file. | ||
|
||
:param converter: The converter to export | ||
:param path: The path to a file to write to | ||
:param include_synonyms: If true, includes CURIE prefix synonyms. | ||
URI prefix synonyms are not output. | ||
:param expand: If False, output a dictionary-like ``@context`` element. | ||
If True, use ``@prefix`` and ``@id`` as keys for the CURIE prefix | ||
and URI prefix, respectively, to maximize compatibility. | ||
|
||
The following example shows writing a JSON-LD context: | ||
|
||
.. code-block:: python | ||
|
||
import curies | ||
converter = curies.load_prefix_map({ | ||
"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_", | ||
}) | ||
curies.write_jsonld_context(converter, "example_context.json") | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"@context": { | ||
"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_" | ||
} | ||
} | ||
|
||
Because some implementations of JSON-LD do not like URI prefixes that end | ||
with an underscore ``_``, we can use the ``expand`` keyword to turn on more | ||
verbose JSON-LD context output that contains explicit ``@prefix`` and | ||
``@id`` annotations | ||
|
||
.. code-block:: python | ||
|
||
import curies | ||
converter = curies.load_prefix_map({ | ||
"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_", | ||
}) | ||
curies.write_jsonld_context(converter, "example_context.json", expand=True) | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"@context": { | ||
"CHEBI": { | ||
"@id": "http://purl.obolibrary.org/obo/CHEBI_", | ||
"@prefix": true | ||
} | ||
} | ||
} | ||
""" | ||
path = _ensure_path(path) | ||
context = {} | ||
for record in converter.records: | ||
term = _get_expanded_term(record, expand=expand) | ||
context[record.prefix] = term | ||
if include_synonyms: | ||
for prefix_synonym in record.prefix_synonyms: | ||
context[prefix_synonym] = term | ||
obj = _get_jsonld_context(converter, include_synonyms=include_synonyms, expand=expand) | ||
with path.open("w") as file: | ||
json.dump( | ||
fp=file, | ||
indent=4, | ||
sort_keys=True, | ||
obj={"@context": context}, | ||
) | ||
json.dump(obj, file, indent=4, sort_keys=True) | ||
|
||
|
||
def _get_expanded_term(record: Record, *, expand: bool) -> Union[str, Dict[str, Any]]: | ||
|
@@ -2183,6 +2238,24 @@ def write_shacl( | |
URI prefix synonyms are not output. | ||
|
||
.. seealso:: https://www.w3.org/TR/shacl/#sparql-prefixes | ||
|
||
.. code-block:: python | ||
|
||
import curies | ||
converter = curies.load_prefix_map({ | ||
"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_", | ||
}) | ||
curies.write_shacl(converter, "example_shacl.ttl") | ||
|
||
.. code-block:: | ||
|
||
@prefix sh: <http://www.w3.org/ns/shacl#> . | ||
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
|
||
[ | ||
sh:declare | ||
[ sh:prefix "CHEBI" ; sh:namespace "http://purl.obolibrary.org/obo/CHEBI_"^^xsd:anyURI ] | ||
] . | ||
""" | ||
text = dedent( | ||
"""\ | ||
|
@@ -2207,6 +2280,41 @@ def write_shacl( | |
path.write_text(text.format(entries=",\n".join(lines))) | ||
|
||
|
||
def write_tsv( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! |
||
converter: Converter, path: Union[str, Path], *, header: Tuple[str, str] = ("prefix", "base") | ||
) -> None: | ||
"""Write a simple prefix map CSV file. | ||
|
||
:param converter: The converter to export | ||
:param path: The path to a file to write to | ||
:param header: A 2-tuple of strings representing the header used in the file, | ||
where the first element is the label for CURIE prefixes and the second | ||
element is the label for URI prefixes | ||
|
||
.. code-block:: python | ||
|
||
import curies | ||
converter = curies.load_prefix_map({ | ||
"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_", | ||
}) | ||
curies.write_tsv(converter, "example_context.tsv") | ||
|
||
.. code-block:: | ||
|
||
prefix base | ||
CHEBI http://purl.obolibrary.org/obo/CHEBI_ | ||
""" | ||
import csv | ||
|
||
path = _ensure_path(path) | ||
|
||
with path.open("w") as csvfile: | ||
writer = csv.writer(csvfile, delimiter="\t") | ||
writer.writerow(header) | ||
for record in converter.records: | ||
writer.writerow((record.prefix, record.uri_prefix)) | ||
|
||
|
||
def _get_shacl_line(prefix: str, uri_prefix: str, pattern: Optional[str] = None) -> str: | ||
line = f' [ sh:prefix "{prefix}" ; sh:namespace "{uri_prefix}"^^xsd:anyURI ' | ||
if pattern: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't even think to check for this. You write great docstrings!