Brian Beck avatar Brian Beck committed 81600e1

Moved mapper modules to telescope.mapper submodule.

Comments (0)

Files changed (2)

lib/telescope/mapper/__init__.py

+from telescope import Variable
+from telescope.exceptions import *
+from telescope.sparql.patterns import Triple
+from telescope.sparql.queryforms import Select
+from telescope.sparql.helpers import is_a
+from telescope.mapper.properties import PropertyManager
+from telescope.mapper.query import Query
+
+__all__ = ['Mapper', 'mapper', 'get_mapper']
+
+class Mapper(object):
+    def __init__(self, class_, type_or_select, identifier=None, properties=None):
+        self.class_ = class_
+        
+        if identifier is None:
+            identifier = Variable(class_.__name__)
+        self.identifier = identifier
+        
+        if isinstance(type_or_select, Select):
+            select = type_or_select
+        else:
+            rdf_type = type_or_select
+            select = Select([identifier], [(identifier, is_a, rdf_type)])
+        
+        if identifier not in select.projection:
+            raise InvalidRequestError("Select must include identifier.")
+        
+        self.select = select
+        self.setup_class(properties or {})
+    
+    def setup_class(self, properties):
+        if not hasattr(self.class_, '_manager'):
+            self.class_._manager = PropertyManager(self.class_)
+        self.class_._manager.update(properties)
+    
+    def new_instance(self):
+        return self.class_.__new__(self.class_)
+    
+    def bind_instance(self, graph, instance, data):
+        instance._id = data.pop(self.identifier)
+        for key, value in data.iteritems():
+            descriptor = self.class_._manager.get(key)
+            if descriptor is not None:
+                value = descriptor.to_python(graph, value)
+                descriptor.__set__(instance, value)
+    
+    def bind_results(self, graph, query, results):
+        for result in results:
+            data = dict(zip(query.projection, result))
+            instance = self.new_instance()
+            self.bind_instance(graph, instance, data)
+            yield instance
+
+def mapper(class_, *args, **kwargs):
+    class_._mapper = Mapper(class_, *args, **kwargs)
+    return class_._mapper
+
+def get_mapper(class_):
+    return class_._mapper

tests/test_mapper.py

 from nose.tools import assert_raises
 from telescope import Namespace, ConjunctiveGraph, Variable, BNode, URIRef
 from telescope.mapper import Mapper, mapper
-from telescope.properties import Property, Relationship
-from telescope.query import Query
-from telescope.session import Session
+from telescope.mapper.properties import Property, Relationship
+from telescope.mapper.query import Query
+from telescope.mapper.session import Session
 import helpers
 
 RDF = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.