compDecls.py
2.45 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
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
#!/bin/env python
""" compDecls.py -- 'compile' declarations from iCalendar ontology
USAGE:
python compDecls.py foo.rdf >foo.py
Then copy/paste stuff from foo.py into fromIcal.py.
TODO:
- subcomponent connections
- geo label. (wierd bug?)
"""
__version__ = '$Id: compDecls.py,v 1.2 2004/04/07 18:35:16 connolly Exp $'
from myStore import Namespace, load # http://www.w3.org/2000/10/swap/
from warnings import warn
RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
RDFS = Namespace("http://www.w3.org/2000/01/rdf-schema#")
ICAL = Namespace("http://www.w3.org/2002/12/cal/ical#")
SPEC = Namespace("http://www.w3.org/2002/12/cal/icalSpec#")
OWL = Namespace("http://www.w3.org/2002/07/owl#")
def compile(ont):
"""find the classes with labels; treat them as iCalendar component types.
Then find ?P where [?C rdfs:subClassOf [ owl:onProperty ?P ] ].
And find ?T where [?P spec:valueType ?T].
"""
progress("looking for Classes")
comps = {}
for c in ont.each(pred = RDF.type, obj = OWL.Class):
#progress("looking for label on ", c)
l = ont.any(c, RDFS.label)
if l:
ln = c.uriref().split("#")[1]
props = {}
for r in ont.each(subj = c, pred = RDFS.subClassOf):
p = ont.any(r, OWL.onProperty)
if p:
pln = p.uriref().split("#")[1]
pl = ont.any(p, RDFS.label)
if pl:
pt = ont.any(p, SPEC.valueType)
if pt:
ty = str(pt)
else:
plt = ont.any(p, SPEC.valueListType)
if plt:
ty = (str(plt),)
else:
warn("no value type for %s" % p)
ty = 'None'
props[str(pl)] = (pln, ty, None, None)
else:
warn("no label for %s" %p)
comps[str(l)] = (ln, props, "@@subcomponents")
return comps
def progress(*args):
for i in args:
sys.stderr.write(str(i))
sys.stderr.write("\n")
def usage():
import sys
print >>sys.stderr, __doc__
import pprint
def main(argv):
try:
ontf = argv[1]
except IndexError:
usage()
return 2
progress("loading ", ontf, "...")
ont = load(ontf)
decls = compile(ont)
pprint.pprint(decls)
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
# $Log: compDecls.py,v $
# Revision 1.2 2004/04/07 18:35:16 connolly
# valueListType
#
# Revision 1.1 2004/02/29 14:35:04 connolly
# useful
#