compDecls.py 2.45 KB
#!/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
#