| 1 | #!/usr/bin/env python |
|---|
| 2 | # encoding: utf-8 |
|---|
| 3 | """ |
|---|
| 4 | orm.py |
|---|
| 5 | |
|---|
| 6 | Created by Philip Cooper on 2007-11-23. |
|---|
| 7 | Copyright (c) 2007 Openvest. All rights reserved. |
|---|
| 8 | """ |
|---|
| 9 | |
|---|
| 10 | from rdfalchemy.rdfSubject import rdfSubject |
|---|
| 11 | from rdfalchemy.descriptors import rdfAbstract |
|---|
| 12 | |
|---|
| 13 | import logging |
|---|
| 14 | log=logging.getLogger(__name__) |
|---|
| 15 | |
|---|
| 16 | def 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 | |
|---|
| 25 | def 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 | |
|---|