#! /usr/bin/python

#
# Parses an ontology from a .xml file formatted in RDF/XML and outputs a
# loadable Python module representing the ontology
#

# API docs for rdflib on http://www.rdflib.net/rdflib-2.4.0/html/index.html
import rdflib

from rdflib.plugin import register
from rdflib.syntax import serializer, serializers

register('python', serializers.Serializer,
         'PythonSerializer', 'PythonSerializer')

def parse(trig_stream):
	"""
	Return a list of triples representing the ontology
	"""
	trig_in = rdflib.FileInputSource(trig_stream)
	ontology = rdflib.ConjunctiveGraph()
	ontology.parse(trig_in)
	pycode = ontology.serialize(format="python")
	print pycode	

if __name__ == "__main__":
	import sys, os
	
	# On no args, read from stdin
	if len(sys.argv) <= 1:
		print >> sys.stderr, "Parsing from stdin"
		parse(sys.stdin)
	else:
		for trig_filename in sys.argv[1:]:
			print >> sys.stderr, "Parsing " + trig_filename
			parse(file(trig_filename))


