Skip to content

Commit 912182c

Browse files
committed
Create remove_field.py
1 parent b4bb707 commit 912182c

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/obofoundry/remove_field.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from io import StringIO
2+
from pathlib import Path
3+
4+
import click
5+
import yaml
6+
7+
from obofoundry.constants import ONTOLOGY_DIRECTORY
8+
from obofoundry.standardize_metadata import ModifiedDumper
9+
10+
11+
def remove_field(name: str) -> None:
12+
for path in ONTOLOGY_DIRECTORY.glob("*.md"):
13+
remove_field_from_file(path, name)
14+
15+
16+
def remove_field_from_file(path: Path, name: str) -> None:
17+
"""Update the given markdown file."""
18+
with open(path) as file:
19+
lines = [line.rstrip("\n") for line in file]
20+
21+
assert lines[0] == "---"
22+
idx = min(i for i, line in enumerate(lines[1:], start=1) if line == "---")
23+
24+
# Load the data like it is YAML
25+
data = yaml.safe_load(StringIO("\n".join(lines[1:idx])))
26+
27+
if name in data:
28+
del data[name]
29+
30+
dumped = ModifiedDumper.dump(data)
31+
32+
with open(path, "w") as file:
33+
print("---", file=file)
34+
print(dumped, file=file)
35+
print("---", file=file)
36+
for line in lines[idx + 1 :]:
37+
print(line, file=file)
38+
39+
40+
@click.command()
41+
@click.argument("name")
42+
def main(name) -> None:
43+
remove_field(name)
44+
45+
46+
if __name__ == "__main__":
47+
main()

0 commit comments

Comments
 (0)