Skip to content

Commit

Permalink
Proactively update Prefix.cc (#1056)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt authored Mar 18, 2024
1 parent b52f972 commit ec20164
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/update_prefix_cc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Send data to Prefix.cc
on:
workflow_dispatch:
schedule:
- cron: "5 4 * * *" # run at 4:05 in the morning (cron format is minute hour day month day, check https://crontab.guru)
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
fetch-depth: 0 # otherwise, you will fail to push refs to dest repo
- uses: actions/setup-python@v2
with:
python-version: 3.12

- name: Install dependencies
run: pip install tox tox-uv

- name: Send a prefix to Prefix.cc
run: tox -e send-prefixcc
56 changes: 56 additions & 0 deletions src/bioregistry/export/prefixcc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Update Prefix.cc to reflect the content of the Bioregistry.
.. seealso:: https://github.com/OBOFoundry/OBOFoundry.github.io/issues/1038
"""

import random

import click
import requests

import bioregistry


def create(curie_prefix: str, uri_prefix: str) -> requests.Response:
"""Send a CURIE prefix/URI prefix to the Prefix.cc "create" endpoint."""
return requests.post(
f"https://prefix.cc/{curie_prefix}",
data={"create": uri_prefix},
)


def main():
"""Add an OBO Foundry prefix to Prefix.cc."""
prefix_cc_map = requests.get("https://prefix.cc/context").json()["@context"]
records = []
for record in bioregistry.resources():
if not record.get_obofoundry_prefix():
continue
uri_prefix = record.get_uri_prefix()
if not uri_prefix:
continue
if uri_prefix == prefix_cc_map.get(record.prefix):
# No need to re-create something that's already
# both available and correct wrt Bioregistry/OBO
continue
records.append(record)

if not records:
click.echo("No records left to submit to Prefix.cc, good job!")
return

click.echo(f"{len(records):,} records remain to submit to Prefix.cc")

# shuffle records to make sure that if there's an error, it doesn't
# lock the update permenantly
random.shuffle(records)

# Pick only the first record, since we can only make one update per day
record = records[0]
click.echo(f"Attempting to create a record for {record.prefix} {uri_prefix}")
res = create(record.prefix, uri_prefix)
click.echo(res.text)


if __name__ == "__main__":
main()
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ description = Lint the Bioregistry data
deps =
mypy
pydantic
types-PyYAML
types-Markdown
types-tabulate
types-requests
skip_install = true
commands = mypy --install-types --non-interactive --ignore-missing-imports src/bioregistry/
description = Run the mypy tool to check static typing on the project.
Expand Down Expand Up @@ -197,6 +201,12 @@ commands =
erdantic bioregistry.Registry bioregistry.Resource -o docs/img/datamodel_umls.svg
erdantic bioregistry.Registry bioregistry.Resource -o docs/img/datamodel_umls.png

[testenv:send-prefixcc]
usedevelop = true
commands =
python -m bioregistry.export.prefixcc
description = Send a Bioregistry CURIE prefix/URI prefix pair to Prefix.cc via its "create" API endpoint. Note that only one request to this endpoint can be sent per day.

####################
# Deployment tools #
####################
Expand Down

0 comments on commit ec20164

Please sign in to comment.