"""Interfacing with RDF (based on rdflib)


"""

__version__ = "$Revision: 1.3 $"
# $Source: /sources/public/2007/asn/rdf.py,v $

class RDFError(Exception):
    """Unspecified RDF module error"""


# do we use the uri_table to find the module/module_name for an interface?
#
#   uri_table.get_python_object("http://www.w3.org/foo/owl#")
#       returns an ObjectModel for owl ?
#

def recognize(graph, model, module, object_map=None):
    """Extract all the python objects we see described in the graph

    >>> from opendata.rdf import recognize

    # read a graph
    >>> from rdflib.Graph import Graph
    >>> graph = Graph()
    >>> dummy = graph.parse("test-data/animal-movie-data.n3", format="n3")

    # load a dynamically generated module from an interface
    >>> import opendata.python

    >>> (model,module) = opendata.python.import_interface('test-data/animal-movies.asn', 'animal_movies')

    # see what's in it!
    >>> object_map = {}
    >>> opendata.rdf.recognize(graph, model, module, object_map)

    Notes:
       we could someday make "interface" optional by extracting it
       from the graph as OWL, maybe...
       
    """

    for prop in model.properties:
        for (subject, object) in graph.get_subject_objects(prop.uri):
            cls = model.classFor(prop)
            obj = get_object(object_map, subject, cls.uri)
            (ns, local) = qname.uri_split(prop.uri)
            objobj = get_object(object_map, object)
            setattr(obj, local, objobj)

def get_object(map, uri, clsuri, moduleName):
    """return a python instance of the right class, creating/copying
    as necessary.
    """
    try:
        old = map[uri]
    except KeyError:
        (ns, local) = qname.uri_split(clsuri)
        result = eval(moduleName+"."+local+"()")
        return result

    if eval(
        
    


if __name__ == "__main__":
    import doctest, sys
    doctest.testmod(sys.modules[__name__])









##   
##   from rdflib.Graph import Graph
##   from rdflib import RDF
##   from rdflib import URIRef, Literal, BNode, Namespace
##   
##   g = Graph()
##   g.bind("dc", "http://http://purl.org/dc/elements/1.1/")
##   g.bind("foaf", "http://xmlns.com/foaf/0.1/")
##   g.bind("rif", "http://www.w3.org/2007/01/rif")
##   g.bind("k", "http://www.w3.org/2007/01/24/example#")
##   
##   g.parse("test8.n3", format="n3")
##   
##   #for s, p, o in g:
##   #    print repr(s), repr(p), repr(o)
##   
##   #print "======="
##   
##   #for i in g.namespaces():
##   #    print i
##   
##   
##   # rs = u'http://www.w3.org/2007/01/24/example#rs'
##   K = Namespace("http://www.w3.org/2007/01/24/example#")
##   rs = K.rs
##   #  rs = g.qname(u'http://www.w3.org/2007/01/24/example#rs')
##   for (p, o) in g.predicate_objects(rs):
##       print p, o
##       for x in g.items(o):
##           print "   - ", x
##           
##   
##   
