Commits

imbolc  committed c04e04a

tyrant support

  • Participants
  • Parent commits 8053247

Comments (0)

Files changed (4)

 tclite
-====
+======
+
+Tokyo cabinet and tokyo tyrant object wrapper.
+
 
 Installation
 ------------
     ...     is_active   = tl.BooleanProperty(default=True)
     ...     created     = tl.DateTimeProperty(index=True) # indexed field
     ...
-    ...     __filename__ = 'var/readme_test.tclite'
+    ...     __file__ = 'var/readme_test.tclite'
+    ...     #__host__ = 'localhost:1978'
 
     
     >>> User.db.clear()
         author_email = 'imbolc@imbolc.name',
         url = 'http://bitbucket.org/imbolc/tclite/',
         name = PACKAGE,
-        #~ requires = ['tokyo-python'],
+        requires = ['tokyo-python'],
 
         packages = packages,
         package_data = {'tclite': data_files},

File tclite/__init__.py

 import marshal
 
 import tokyo.cabinet as tc
+import tokyo.tyrant as tt
 
 from properties import *
 
 
-class class_cached_property(object):
+class cached_classmethod(object):
     '''
-    Кеширующий classmethod
+    cached_classmethod
     '''
     def __init__(self, f):
         self.f = f
 
 
 class Model(object):
-    __filename__ = None
+    __file__ = None
+    __host__ = None
     key = None
     
     def __init__(self, key=None, _raw_data=None, **kwargs):
         self._raw_data = {}
         self._cached_data = {}
 
-    @class_cached_property
+    @cached_classmethod
     def db(cls):
-        '''Отложенное подключение к бд'''
-        if not cls.__filename__:
-            raise Error('set __filename__ in model "%s"' % cls.__name__)
-        db = tc.TDB()
-        db.open(cls.__filename__, tc.HDBOWRITER | tc.HDBOCREAT)
+        '''Lazy db connection'''
+        if cls.__file__:
+            db = tc.TDB()
+            db.open(cls.__file__, tc.HDBOWRITER | tc.HDBOCREAT)
+        elif cls.__host__:
+            host, port = cls.__host__.split(':')
+            db = tt.RTDB()
+            db.open(host=host, port=int(port))
+        else:
+            raise Error('set __file__ or __host__ in model "%s"' % 
+                cls.__name__)
         for prop in (cls.__dict__[n] for n in cls._properties):
             if prop.index:
                 if not prop.INDEX:
-                    raise Error('field "%s" is not indexed' % prop.__class__.__name__)
+                    raise Error('field "%s" is not indexed' % 
+                        prop.__class__.__name__)
                 db.setindex(prop.key, prop.INDEX['set'])
             else:
                 try:
-                    db.setindex(prop.key, tc.TDBITVOID)
+                    db.setindex(prop.key, tt.TDBITVOID)
                 except (tc.Error, AttributeError):
                     pass
         return db
     
-    @class_cached_property
+    @cached_classmethod
     def _properties(cls):
         '''
         {prop_attr_name: save_key_name}
                 ret[k] = prop.key
         return ret
 
-    @class_cached_property
+    @cached_classmethod
     def _presave_sync_properties(cls):
         '''
         {prop_attr_name: save_key_name}

File test_speed.py

File contents unchanged.