-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpackage.py
More file actions
64 lines (51 loc) · 2.09 KB
/
Copy pathpackage.py
File metadata and controls
64 lines (51 loc) · 2.09 KB
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
from datetime import datetime
from pathlib import Path
from rdflib import Graph, Literal, URIRef
from rdflib.namespace import OWL, DCTERMS
import re
import sys
import opencs
def main():
if len(sys.argv) != 4:
print(
'Packages the schema and the ontology\n'
'Args: \n'
'- input dir – root of OpenCS repo (no trailing slash);\n'
'- output dir (no trailing slash)\n'
'- version tag for the ontology (not schema)\n'
' The version tag can start with "v" – it will be stripped automatically.'
)
exit(1)
in_dir = sys.argv[1]
out_dir = sys.argv[2]
version = sys.argv[3]
if re.match(r'^v\d+\.\d+\.', version):
# strip "v" from the start of the version tag
version = version[1:]
Path(out_dir).mkdir(parents=True, exist_ok=True)
process_simple(in_dir + '/schema/*.ttl', out_dir + '/opencs_schema')
process_ontology(in_dir, out_dir, version)
# TODO: also save each entity separately? for slash path access to specific entities
# Would have to group by S, filter by those in OpenCS namespace.
def process_simple(in_glob: str, out_base_path: str, out_gzip: bool = False):
schema = Graph()
opencs.parse_all(schema, in_glob)
Path(out_base_path).parent.mkdir(parents=True, exist_ok=True)
opencs.serialize_multi(schema, out_base_path, use_gzip=out_gzip)
def process_ontology(in_dir: str, out_dir: str, version: str):
g = get_auto_header(version)
g.parse(in_dir + '/ontology/header.ttl')
opencs.serialize_multi(g, out_dir + '/opencs_header')
opencs.parse_all(g, in_dir + '/ontology/core/**/*.ttl')
g.parse(in_dir + '/ontology/authors.ttl')
opencs.serialize_multi(g, out_dir + '/opencs', use_gzip=True)
def get_auto_header(version: str) -> Graph:
# TODO: add more stuff?
g = Graph()
g.bind('dcterms', DCTERMS)
onto = URIRef('https://w3id.org/ocs/ont')
g.add((onto, OWL.versionIRI, URIRef(onto + '/' + version)))
g.add((onto, DCTERMS.issued, Literal(datetime.utcnow())))
return g
if __name__ == "__main__":
main()