Commits

March Liu  committed 3912da3

you can link socrates storage by a Session

  • Participants
  • Parent commits bb01978

Comments (0)

Files changed (2)

File src/socrates/core.py

 >>> storage.registed_type.keys()
 ['string', 'subject']
     """
-    def __init__(self, uri=None, create=False, schema=None, pool_size=20):
+    def __init__(self, uri=None, Session=None, create=False, schema=None, pool_size=20):
         """
 
         """
-        engine = create_engine(uri, pool_size=pool_size) if uri else create_engine("sqlite://:memory:")
-        self.engine = engine
-        self.Session = scoped_session(sessionmaker(bind=engine))
+        if uri:
+            engine = create_engine(uri, pool_size=pool_size)
+            self.engine = engine
+            self.Session = scoped_session(sessionmaker(bind=engine))
+        elif Session:
+            self.Session = scoped_session(sessionmaker(bind=engine))
+            self.engine = self.Session
+
         metadata = MetaData(bind=engine) 
         self.sequence = Serial(engine, metadata, create=create, schema=schema)
-        self.metadata = metadata
+        self.metadata = metadata 
         self.registed_type = {}
         self.registed_storage = {}
         self.prdParser = PrdExprParser()
             _self.obj = value
         properties = {"storage":self,
                       "table":table,
-                      "getSubject":lambda _self:self.getSubjectById(_self.subject_id),
-                      "getPredicate":lambda _self:self.getSubjectById(_self.predicate_id)}
+                      "__get_subject__":lambda _self:self.getSubjectById(_self.subject_id),
+                      "__get_predicate__":lambda _self:self.getSubjectById(_self.predicate_id)}
         if typename=='subject':
-            properties['getObject'] = lambda _self:self.getSubjectById(_self.obj)
-            properties['setObject'] = setSubject
+            properties['__get_object__'] = lambda _self:self.getSubjectById(_self.obj)
+            properties['__set_object__'] = setSubject
         else:
-            properties['getObject'] = lambda _self:_self.obj
-            properties['setObject'] = setObject
+            properties['__get_object__'] = lambda _self:_self.obj
+            properties['__set_object__'] = setObject
 
         SegmentType=type('Segment'+typename.title(), (Segment,), properties)
         mapper(SegmentType, table,)

File src/socrates/types.py

         self.subject_id = subject_id
         self.predicate_id = predicate_id
         self.obj=obj
-    def __getattr__(self, key):
-        if key == 'subject':
-            return self.getSubject()
-        elif key == 'predicate':
-            return self.getPredicate()
-        elif key == 'object':
-            return self.getObject()
-        else:
-            return self.__dict__[key]
-    def __setattr__(self, key, value):
-        if key == 'object':
-            self.setObject(value)
-        else:
-            self.__dict__[key] = value
+    def getSubject(self):
+        return self.__get_subject__()
+    def getPredicate(self):
+        return self.__get_predicate__()
+    def getObject(self):
+        return self.__get_object__()
+    def setObject(self, value):
+        return self.__set_object__(value)
 
+    subject = property(getSubject)
+    predicate = property(getPredicate)
+    object = property(getObject, setObject)