Skip to content

Commit 9c078c4

Browse files
authored
Add tutorial for running CLI (#31)
1 parent 95ef0c5 commit 9c078c4

3 files changed

Lines changed: 53 additions & 17 deletions

File tree

README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ Idiomatic conversion between URIs and compact URIs (CURIEs).
4747
from curies import Converter
4848

4949
converter = Converter.from_prefix_map({
50-
"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_",
51-
"MONDO": "http://purl.obolibrary.org/obo/MONDO_",
52-
"GO": "http://purl.obolibrary.org/obo/GO_",
53-
# ... and so on
54-
"OBO": "http://purl.obolibrary.org/obo/",
50+
"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_",
51+
"MONDO": "http://purl.obolibrary.org/obo/MONDO_",
52+
"GO": "http://purl.obolibrary.org/obo/GO_",
53+
# ... and so on
54+
"OBO": "http://purl.obolibrary.org/obo/",
5555
})
5656

5757
>>> converter.compress("http://purl.obolibrary.org/obo/CHEBI_1")
@@ -72,13 +72,15 @@ URI prefix will always be matched. For example, compressing
7272
`http://purl.obolibrary.org/obo/GO_0032571`
7373
will return `GO:0032571` instead of `OBO:GO_0032571`.
7474

75-
A converter can be instantiated from a web-based resource in JSON-LD format:
75+
All loader function work on local file paths, remote URLs, and pre-loaded
76+
data structures. For example, a converter can be instantiated from a web-based
77+
resource in JSON-LD format:
7678

7779
```python
7880
from curies import Converter
7981

8082
url = "https://raw.githubusercontent.com/biopragmatics/bioregistry/main/exports/contexts/semweb.context.jsonld"
81-
converter = Converter.from_jsonld_url(url)
83+
converter = Converter.from_jsonld(url)
8284
```
8385

8486
Several converters can be instantiated from pre-defined web-based resources:
@@ -109,7 +111,7 @@ obo_converter.pd_compress(df, column=0)
109111
obo_converter.pd_expand(df, column=0)
110112
```
111113

112-
Apply in bulk to a CSV file with `Converter.file_expand` and
114+
Apply in bulk to a CSV file with `Converter.file_expand` and
113115
`Converter.file_compress` (defaults to using tab separator):
114116

115117
```python
@@ -125,6 +127,24 @@ obo_converter.file_expand(path, column=0)
125127

126128
Full documentation is available [here](https://curies.readthedocs.io).
127129

130+
## CLI Usage
131+
132+
This package comes with a built-in CLI for running a resolver web application:
133+
134+
```shell
135+
$ python -m curies --host 0.0.0.0 --port 8000 bioregistry
136+
```
137+
138+
The positional argument can be one of the following:
139+
140+
1. A pre-defined prefix map to get from the web (bioregistry, go, obo, monarch, prefixcommons)
141+
2. A local file path or URL to a prefix map, extended prefix map, or one of several formats. Requires specifying
142+
a `--format`.
143+
144+
The framework can be swapped to use Flask (default) or FastAPI with `--framework`. The
145+
server can be swapped to use Werkzeug (default) or Uvicorn with `--server`. These functionalities
146+
are also available programmatically, see the docs for more information.
147+
128148
## 🧑‍🤝‍🧑 Related
129149

130150
Other packages that convert between CURIEs and URIs:
@@ -147,7 +167,8 @@ $ pip install curies
147167
## 👐 Contributing
148168

149169
Contributions, whether filing an issue, making a pull request, or forking, are appreciated. See
150-
[CONTRIBUTING.md](https://github.com/cthoyt/curies/blob/master/.github/CONTRIBUTING.md) for more information on getting involved.
170+
[CONTRIBUTING.md](https://github.com/cthoyt/curies/blob/master/.github/CONTRIBUTING.md) for more information on getting
171+
involved.
151172

152173
## 👋 Attribution
153174

@@ -231,4 +252,5 @@ This script does the following:
231252
4. Push to GitHub. You'll need to make a release going with the commit where the version was bumped.
232253
5. Bump the version to the next patch. If you made big changes and want to bump the version by minor, you can
233254
use `tox -e bumpversion minor` after.
255+
234256
</details>

docs/source/index.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,21 @@ The most recent code and data can be installed directly from GitHub with:
1717
1818
.. automodapi:: curies
1919
:no-inheritance-diagram:
20+
21+
CLI Usage
22+
---------
23+
This package comes with a built-in CLI for running a resolver web application:
24+
25+
.. code-block::
26+
27+
$ python -m curies --host 0.0.0.0 --port 8000 bioregistry
28+
29+
The positional argument can be one of the following:
30+
31+
1. A pre-defined prefix map to get from the web (bioregistry, go, obo, monarch, prefixcommons)
32+
2. A local file path or URL to a prefix map, extended prefix map, or one of several formats. Requires specifying
33+
a `--format`.
34+
35+
The framework can be swapped to use Flask (default) or FastAPI with `--framework`. The
36+
server can be swapped to use Werkzeug (default) or Uvicorn with `--server`. These functionalities
37+
are also available programmatically (see :func:`get_flask_app` and :func:`get_fastapi_app`).

src/curies/web.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
FAILURE_CODE = 422
2323

2424

25-
def _prefix_list(converter: Converter) -> str:
26-
return "".join(f"\n- {p}" for p in sorted(converter.get_prefixes()))
27-
28-
2925
def get_flask_blueprint(converter: Converter, **kwargs: Any) -> "flask.Blueprint":
3026
"""Get a blueprint for :class:`flask.Flask`.
3127
@@ -80,9 +76,8 @@ def resolve(prefix: str, identifier: str) -> "Response":
8076
"""Resolve a CURIE."""
8177
location = converter.expand_pair(prefix, identifier)
8278
if location is None:
83-
return abort(
84-
FAILURE_CODE, f"Invalid prefix: {prefix}. Use one of:{_prefix_list(converter)}"
85-
)
79+
prefixes = "".join(f"\n- {p}" for p in sorted(converter.get_prefixes()))
80+
return abort(FAILURE_CODE, f"Invalid prefix: {prefix}. Use one of:{prefixes}")
8681
return redirect(location)
8782

8883
return blueprint
@@ -210,9 +205,10 @@ def resolve(
210205
"""Resolve a CURIE."""
211206
location = converter.expand_pair(prefix, identifier)
212207
if location is None:
208+
prefixes = ", ".join(sorted(converter.get_prefixes()))
213209
raise HTTPException(
214210
status_code=FAILURE_CODE,
215-
detail=f"Invalid prefix: {prefix}. Use one of:{_prefix_list(converter)}",
211+
detail=f"Invalid prefix: {prefix}. Use one of: {prefixes}",
216212
)
217213
return RedirectResponse(location, status_code=302)
218214

0 commit comments

Comments
 (0)