root / rdfalchemy / trunk / rdfalchemy / orm.py

Revision 127, 1.6 kB (checked in by phil, 7 months ago)

This update to rdfalchemy moves some functions in to a sub-module and has the beginings of a sparql script
It adds some unit tests (for initBinding)

Breaking your Programs:

This update could break your code if you:


from rdfalchemy.sesame2 import SesameGraph?


you now need to import from rdfalchemy.sparql.sesame2
allows for jython or other sesame graph implimentations not dependent on the http sparql endpoint


you should probably be using create_engine instead

Line 
1#!/usr/bin/env python
2# encoding: utf-8
3"""
4orm.py
5
6Created by Philip Cooper on 2007-11-23.
7Copyright (c) 2007 Openvest. All rights reserved.
8"""
9
10from rdfalchemy.rdfSubject import rdfSubject
11from rdfalchemy.descriptors import rdfAbstract
12
13import logging
14log=logging.getLogger(__name__)
15
16def allsub(cl, beenthere = set([])):
17    "return all subclasses of the given class"
18    sub = set(cl.__subclasses__()) | beenthere
19    newsubs = set(cl.__subclasses__()) - beenthere
20    for onesub in newsubs:
21        sub |= allsub(onesub, sub)
22    return sub
23   
24
25def mapper(*classes):
26    """Map the classes given to allow descriptors with ranges to the proper Class of that type
27    default if no args is to map all subclasses(recursivly) of rdfSubject
28   
29    preforms the mapping
30   
31    returns a dict of {rdf_type: mapped_class} for further processing"""
32    if not classes:
33        classes = allsub(rdfSubject)
34    class_dict = dict([(str(cl.rdf_type), cl) for cl in classes])
35    for cl in classes:  # for each class
36        for v in cl.__dict__.values():  # for each desciptor
37            if isinstance(v,rdfAbstract) and v.range_type:  #if its a descriptor with a range
38                try:
39                    v._mappedClass = class_dict[str(v.range_type)] 
40                except KeyError:
41                    log.warn("No Class Found\nFailed to map %s range of %s"%(v,v.range_type))
42    return class_dict
43
44#def mapBase(baseclass):
45#    """This maps all classes below baseclass as in mapper()
46#    AND puts the dict of {rdf_type: mapped_class}  in an baseclass._type2class attribute"""
47#    baseclass._type2class = mapper(*allsub(baseclass))
48   
Note: See TracBrowser for help on using the browser.