-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdf2py.py
50 lines (40 loc) · 1.52 KB
/
rdf2py.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
import sys
from rdflib import Graph
from rdflib.namespace import OWL
from datetime import datetime
# example usage: python rdf2py.py atlas14 semantics/rdf/atlas14.ttl
if __name__ == "__main__":
args = sys.argv
namespace = args[1]
rdf_path = args[2]
pyrdf_path = "blobfish/pyrdf/_{0}.py".format(namespace.upper())
print(pyrdf_path, rdf_path.lower())
g = Graph()
g.parse(rdf_path.lower(), format="ttl")
with open(pyrdf_path, "w") as f:
header = '''\
\nfrom rdflib import URIRef, Namespace \
\nfrom rdflib.namespace import DefinedNamespace \
\n
\nclass {0}(DefinedNamespace): \
\n\t""" \
\n\t{0} Ontology for S3 cloud mirror \
\n\tCreated on {1} \
\n\t"""\n \
'''.format(
namespace.upper(), datetime.now().strftime(format="%Y-%m-%d %H:%M")
)
print(header, file=f)
print("\t#Classes", file=f)
for s, _, _ in g.triples((None, None, OWL.Class)):
print(f"\t{s.fragment}: URIRef", file=f)
print("\n\t#Data Properties", file=f)
for s, _, _ in g.triples((None, None, OWL.DatatypeProperty)):
print(f"\t{s.fragment}: URIRef", file=f)
print("\n\t#Object Properties", file=f)
for s, _, _ in g.triples((None, None, OWL.ObjectProperty)):
print(f"\t{s.fragment}: URIRef", file=f)
print(
"""\n\t_NS = Namespace("http://github.com/Dewberry/blobfish/semantics/rdf/{0}#")""".format(namespace),
file=f,
)