Skip to content

Commit

Permalink
Update standardize_license_labels.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Nov 3, 2021
1 parent c6d9317 commit 522abc6
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions util/standardize_license_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-

"""
Run with: ``python standardize_license_labels.py``.
Author: `Charles Tapley Hoyt <https://cthoyt.com>`_.
"""

import pathlib
from typing import Union

import click
from bioregistry.license_standardizer import LICENSES
from tqdm import tqdm

HERE = pathlib.Path(__file__).parent.resolve()
ONTOLOGY_DIRECTORY = HERE.parent.joinpath("ontology").resolve()


def update_markdown(path: Union[str, pathlib.Path]) -> None:
"""Update the given markdown file."""
with open(path) as file:
lines = [line.rstrip("\n") for line in file]

try:
idx = min(i for i, line in enumerate(lines) if line.startswith("license:"))
except ValueError:
tqdm.write(f"no license in {path}")
return

license_url = None
for i in range(1, 3):
line = lines[idx + i].strip()
if "url:" in line:
_, license_url = line.split(':', 1)

if license_url is None:
tqdm.write(f"no license URL in {path}")
return

new_label = LICENSES.get(license_url.strip())
if new_label is None:
tqdm.write(f"could not standardize license URL {license_url} in {path}")
return

license_label, license_label_idx = None, None
for i in range(1, 3):
line = lines[idx + i].strip()
if "label:" in line:
license_label_idx = idx + i
_, license_label = line.split(":", 1)

if license_label.replace("-", "") == new_label.replace("-", ""):
return

if license_label_idx is None:
tqdm.write(f"no license label in {path}")
return

with open(path, "w") as file:
for i, line in enumerate(lines):
if i == license_label_idx:
print(f" label: {new_label}", file=file)
else:
print(line, file=file)


@click.command()
def main():
for path in ONTOLOGY_DIRECTORY.glob("*.md"):
update_markdown(path)


if __name__ == "__main__":
main()

0 comments on commit 522abc6

Please sign in to comment.