Commits

Brian Beck committed c596728

Moved mapper modules to telescope.mapper submodule.

Comments (0)

Files changed (8)

lib/telescope/declarative.py

-from telescope.properties import PropertyManager, Term
-from telescope.mapper import mapper
-
-class DeclarativeMeta(type):
-    def __init__(cls, name, bases, attrs):
-        manager = cls._manager = PropertyManager(cls)
-        for base in reversed(bases):
-            if hasattr(base, '_manager'):
-                manager.update(base._manager)
-        for key, value in attrs.iteritems():
-            if isinstance(value, Term):
-                manager.add_property(key, value)
-        if hasattr(cls, 'RDF_TYPE'):
-            mapper(cls, cls.RDF_TYPE)
-
-class Subject(object):
-    __metaclass__ = DeclarativeMeta
-    
-    def __new__(cls, *args, **kwargs):
-        instance = object.__new__(cls, *args, **kwargs)
-        cls._mapper.setup_instance(instance)
-        return instance

lib/telescope/mapper/declarative.py

+from telescope.mapper import mapper
+from telescope.mapper.properties import PropertyManager, Term
+
+class DeclarativeMeta(type):
+    def __init__(cls, name, bases, attrs):
+        manager = cls._manager = PropertyManager(cls)
+        for base in reversed(bases):
+            if hasattr(base, '_manager'):
+                manager.update(base._manager)
+        for key, value in attrs.iteritems():
+            if isinstance(value, Term):
+                manager.add_property(key, value)
+        if hasattr(cls, 'RDF_TYPE'):
+            mapper(cls, cls.RDF_TYPE)
+
+class Subject(object):
+    __metaclass__ = DeclarativeMeta
+    
+    def __new__(cls, *args, **kwargs):
+        instance = object.__new__(cls, *args, **kwargs)
+        cls._mapper.setup_instance(instance)
+        return instance

lib/telescope/mapper/properties.py

+from telescope import Namespace, Variable, Literal, URIRef
+from telescope.sparql.patterns import Triple
+
+__all__ = ['Term', 'Property', 'Label']
+
+RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#')
+
+class Term(object):
+    def __init__(self, predicate, default=None):
+        self.predicate = predicate
+        self.default = default
+    
+    def __get__(self, instance, owner):
+        if instance is not None:
+            state = instance.__dict__.get('_state', {})
+            return state.get(self, self.default)
+        return self
+    
+    def __set__(self, instance, value):
+        state = instance.__dict__.setdefault('_state', {})
+        state[self] = value
+    
+    def to_python(self, graph, value):
+        return value
+    
+    def resolve_subject(self, graph, uri):
+        return uri
+    
+    def triples(self, subject, object):
+        yield Triple(subject, self.predicate, object)
+
+class Property(Term):
+    def to_python(self, graph, value):
+        if isinstance(value, Literal):
+            value = value.toPython()
+            if isinstance(value, Literal):
+                return unicode(value)
+            else:
+                return value
+        elif isinstance(value, URIRef):
+            return self.resolve_subject(graph, value)
+        else:
+            return value
+
+class Label(Property):
+    def resolve_subject(self, graph, uri):
+        for label in graph.objects(uri, RDFS.label):
+            return self.to_python(graph, label)
+        return uri
+
+class Relationship(Property):
+    def __init__(self, class_, predicate):
+        self.class_ = class_
+        self.predicate = predicate
+    
+    def __get__(self, instance, owner):
+        if instance is not None:
+            state = instance.__dict__.get('_state', {})
+            return state.get(self, [])
+        return self
+    
+    def triples(self, subject, object):
+        related_mapper = self.class_._mapper
+        related_mapper.select._clone(variables=(related_mapper.identifier,))
+        
+
+class PropertyManager(object):
+    def __init__(self, class_):
+        self.class_ = class_
+        self.names = {}
+        self.properties = {}
+    
+    def __getitem__(self, key):
+        return self.properties[key]
+    
+    def __setitem__(self, key, value):
+        return self.add_property(key, value)
+    
+    def __iter__(self):
+        return self.properties.iteritems()
+    
+    def get(self, key, default=None):
+        return self.properties.get(key, default)
+    
+    def add_property(self, key, descriptor):
+        self.names[descriptor] = key
+        self.properties[key] = descriptor
+        setattr(self.class_, key, descriptor)
+    
+    def update(self, properties, **kwargs):
+        properties = dict(properties, **kwargs)
+        for key, descriptor in properties.iteritems():
+            self.add_property(key, descriptor)

lib/telescope/mapper/query.py

+from telescope import Variable
+
+class Query(object):
+    def __init__(self, class_, session=None):
+        self.class_ = class_
+        self.session = session
+        self.select = class_._mapper.select
+    
+    def __iter__(self):
+        return self.execute()
+    
+    def _clone(self, **kwargs):
+        clone = self.__class__.__new__(self.__class__)
+        clone.__dict__.update(self.__dict__)
+        clone.__dict__.update(kwargs)
+        return clone
+    
+    def execute(self, graph=None):
+        if graph is None:
+            graph = self.session.graph
+        manager = self.class_._manager
+        mapper = self.class_._mapper
+        variables = map(Variable, self.class_._manager.properties)
+        triples = []
+        for name, property in self.class_._manager:
+            triples.extend(property.triples(mapper.identifier, Variable(name)))
+        select = self.select.project(variables, append=True).where(*triples)
+        results = select.execute(graph)
+        return mapper.bind_results(graph, select, results)
+    
+    def filter(self, *constraints, **kwargs):
+        manager = self.class_._manager
+        mapper = self.class_._mapper
+        triples = []
+        for key, value in kwargs.iteritems():
+            property = self.class_._manager[key]
+            triples.extend(property.triples(self.class_))
+        return self._clone(select=self.select.filter())

lib/telescope/mapper/session.py

+from telescope.mapper.query import Query
+
+class Session(object):
+    def __init__(self, graph=None):
+        self.identity_map = {}
+        self.graph = graph
+    
+    def query(self, class_):
+        return Query(class_, self)

lib/telescope/properties.py

-from telescope import Namespace, Variable, Literal, URIRef
-from telescope.sparql.patterns import Triple
-
-__all__ = ['Term', 'Property', 'Label']
-
-RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#')
-
-class Term(object):
-    def __init__(self, predicate, default=None):
-        self.predicate = predicate
-        self.default = default
-    
-    def __get__(self, instance, owner):
-        if instance is not None:
-            state = instance.__dict__.get('_state', {})
-            return state.get(self, self.default)
-        return self
-    
-    def __set__(self, instance, value):
-        state = instance.__dict__.setdefault('_state', {})
-        state[self] = value
-    
-    def to_python(self, graph, value):
-        return value
-    
-    def resolve_subject(self, graph, uri):
-        return uri
-    
-    def triples(self, subject, object):
-        yield Triple(subject, self.predicate, object)
-
-class Property(Term):
-    def to_python(self, graph, value):
-        if isinstance(value, Literal):
-            value = value.toPython()
-            if isinstance(value, Literal):
-                return unicode(value)
-            else:
-                return value
-        elif isinstance(value, URIRef):
-            return self.resolve_subject(graph, value)
-        else:
-            return value
-
-class Label(Property):
-    def resolve_subject(self, graph, uri):
-        for label in graph.objects(uri, RDFS.label):
-            return self.to_python(graph, label)
-        return uri
-
-class Relationship(Property):
-    def __init__(self, class_, predicate):
-        self.class_ = class_
-        self.predicate = predicate
-    
-    def __get__(self, instance, owner):
-        if instance is not None:
-            state = instance.__dict__.get('_state', {})
-            return state.get(self, [])
-        return self
-    
-    def triples(self, subject, object):
-        related_mapper = self.class_._mapper
-        related_mapper.select._clone(variables=(related_mapper.identifier,))
-        
-
-class PropertyManager(object):
-    def __init__(self, class_):
-        self.class_ = class_
-        self.names = {}
-        self.properties = {}
-    
-    def __getitem__(self, key):
-        return self.properties[key]
-    
-    def __setitem__(self, key, value):
-        return self.add_property(key, value)
-    
-    def __iter__(self):
-        return self.properties.iteritems()
-    
-    def get(self, key, default=None):
-        return self.properties.get(key, default)
-    
-    def add_property(self, key, descriptor):
-        self.names[descriptor] = key
-        self.properties[key] = descriptor
-        setattr(self.class_, key, descriptor)
-    
-    def update(self, properties, **kwargs):
-        properties = dict(properties, **kwargs)
-        for key, descriptor in properties.iteritems():
-            self.add_property(key, descriptor)

lib/telescope/query.py

-from telescope import Variable
-
-class Query(object):
-    def __init__(self, class_, session=None):
-        self.class_ = class_
-        self.session = session
-        self.select = class_._mapper.select
-    
-    def __iter__(self):
-        return self.execute()
-    
-    def _clone(self, **kwargs):
-        clone = self.__class__.__new__(self.__class__)
-        clone.__dict__.update(self.__dict__)
-        clone.__dict__.update(kwargs)
-        return clone
-    
-    def execute(self, graph=None):
-        if graph is None:
-            graph = self.session.graph
-        manager = self.class_._manager
-        mapper = self.class_._mapper
-        variables = map(Variable, self.class_._manager.properties)
-        triples = []
-        for name, property in self.class_._manager:
-            triples.extend(property.triples(mapper.identifier, Variable(name)))
-        select = self.select.project(variables, append=True).where(*triples)
-        results = select.execute(graph)
-        return mapper.bind_results(graph, select, results)
-    
-    def filter(self, *constraints, **kwargs):
-        manager = self.class_._manager
-        mapper = self.class_._mapper
-        triples = []
-        for key, value in kwargs.iteritems():
-            property = self.class_._manager[key]
-            triples.extend(property.triples(self.class_))
-        return self._clone(select=self.select.filter())

lib/telescope/session.py

-from telescope.query import Query
-
-class Session(object):
-    def __init__(self, graph=None):
-        self.identity_map = {}
-        self.graph = graph
-    
-    def query(self, class_):
-        return Query(class_, self)