forked from OBOFoundry/OBOFoundry.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-metadata.py
executable file
·133 lines (107 loc) · 3.5 KB
/
extract-metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
__author__ = 'cjm'
import argparse
import logging
#from yaml import load, dump
#from yaml import Loader, Dumper
import yaml
def main():
parser = argparse.ArgumentParser(description='OBO'
'Helper utils for OBO',
formatter_class=argparse.RawTextHelpFormatter)
subparsers = parser.add_subparsers(dest='subcommand', help='sub-command help')
# SUBCOMMAND
parser_n = subparsers.add_parser('validate', help='validate yml inside md')
##parser_n.add_argument('-d', '--depth', type=int, help='number of hops')
parser_n.set_defaults(function=validate_markdown)
parser_n.add_argument('files',nargs='*')
# SUBCOMMAND
parser_n = subparsers.add_parser('concat', help='concat ontology yamls')
parser_n.add_argument('-i', '--include', help='yml file to include for header')
parser_n.add_argument('-o', '--output', help='output yaml')
parser_n.set_defaults(function=concat_ont_yaml)
parser_n.add_argument('files',nargs='*')
# SUBCOMMAND
parser_n = subparsers.add_parser('concat-principles', help='concat principles yamls')
parser_n.add_argument('-i', '--include', help='yml file to include for header')
parser_n.add_argument('-o', '--output', help='output yaml')
parser_n.set_defaults(function=concat_principles_yaml)
parser_n.add_argument('files',nargs='*')
args = parser.parse_args()
func = args.function
func(args)
def validate_markdown(args):
"""
ensure the yaml encoded inside a YAML file is syntactically valid
"""
for fn in args.files:
print("VALIDATING:"+fn)
# we don't do anything with the results; an
# error is thrown
load_md(fn)
print("OK:"+fn)
def concat_ont_yaml(args):
objs = []
cfg = {}
if (args.include):
f = open(args.include, 'r')
cfg = yaml.load(f.read())
for fn in args.files:
(obj, md) = load_md(fn)
objs.append(obj)
cfg['ontologies'] = objs
f = open(args.output, 'w')
f.write(yaml.dump(cfg))
return cfg
def concat_principles_yaml(args):
objs = []
cfg = {}
if (args.include):
f = open(args.include, 'r')
cfg = yaml.load(f.read())
for fn in args.files:
(obj, md) = load_md(fn)
objs.append(obj)
cfg['principles'] = objs
f = open(args.output, 'w')
f.write(yaml.dump(cfg))
return cfg
def load_md(fn):
f = open(fn, 'r')
text = f.read()
return extract(text)
def extract(mdtext):
lines = mdtext.split("\n")
n = 0
ylines = []
mlines = []
for line in lines:
if (line == "---"):
n=n+1
hlines = []
else:
if (n == 1):
ylines.append(line)
else:
mlines.append(line)
yamltext = "\n".join(ylines)
obj = yaml.load(yamltext)
return (obj, "\n".join(mlines))
def write_legacy_metadata_objects(onts, stream):
"""
write to the old ontologies.txt format
"""
for ont in onts:
write_legacy_metadata_object(ont, stream)
def write_legacy_metadata_object(ont, stream):
"""
write to the old ontologies.txt format (single object) - TODO
"""
write_pv('id', ont['id'], stream)
write_pv('title', ont['title'], stream)
write_pv('namespace', ont['id'].upper, stream)
write_pv('foundry', ont['is_foundry'], stream)
def write_pv(k,v,s):
s.write(p + "\t" + v)
if __name__ == "__main__":
main()