Commits

Sebastian Hubbard  committed 2d7583b

a python wrapper

  • Participants
  • Parent commits 92450ba

Comments (0)

Files changed (3)

   if (!x)
     return 0;
 
-  int i;
   x->slab = (void *) x + sizeof(ticket_slab);
   x->count = SLAB_SIZE - 1;
   x->next = next;
   if (!PyArg_ParseTuple(args, "|ssss", &host, &user, &passwd, &bucket))
     return 0;
 
+  if (!strlen(host))
+    host = 0;
+  if (!strlen(user))
+    user = 0;
+  if (!strlen(passwd))
+    passwd = 0;
+  if (!strlen(bucket))
+    passwd = 0;
+
+
   pylibcb_instance *z = calloc(1, sizeof(pylibcb_instance));
   if (!z) {
     PyErr_SetString(OutOfMemory, "ran out of memory while allocating pylibcb instance");
   { 0, 0, 0, 0 }
 };
 
-PyMODINIT_FUNC initpylibcb() {
+PyMODINIT_FUNC init_pylibcb() {
   PyObject *m;
 
-  m = Py_InitModule("pylibcb", PylibcbMethods);
+  m = Py_InitModule("_pylibcb", PylibcbMethods);
   if (!m)
     return;
 
-  Timeout = PyErr_NewException("pylibcb.Timeout", 0, 0);
+  Timeout = PyErr_NewException("_pylibcb.Timeout", 0, 0);
   Py_INCREF(Timeout);
   PyModule_AddObject(m, "Timeout", Timeout);
   
-  OutOfMemory = PyErr_NewException("pylibcb.OutOfMemory", 0, 0);
+  OutOfMemory = PyErr_NewException("_pylibcb.OutOfMemory", 0, 0);
   Py_INCREF(OutOfMemory);
   PyModule_AddObject(m, "OutOfMemory", OutOfMemory);
 
-  ConnectionFailure = PyErr_NewException("pylibcb.ConnectionFailure", 0, 0);
+  ConnectionFailure = PyErr_NewException("_pylibcb.ConnectionFailure", 0, 0);
   Py_INCREF(ConnectionFailure);
   PyModule_AddObject(m, "ConnectionFailure", ConnectionFailure);
   
-  Failure = PyErr_NewException("pylibcb.Failure", 0, 0);
+  Failure = PyErr_NewException("_pylibcb.Failure", 0, 0);
   Py_INCREF(Failure);
   PyModule_AddObject(m, "Failure", Failure);
 
-  KeyExists = PyErr_NewException("pylibcb.KeyExists", 0, 0);
+  KeyExists = PyErr_NewException("_pylibcb.KeyExists", 0, 0);
   Py_INCREF(KeyExists);
   PyModule_AddObject(m, "KeyExists", KeyExists);
 }
 int main(int argc, char **argv) {  
   Py_SetProgramName(argv[0]);
   Py_Initialize();
-  initpylibcb();
+  init_pylibcb();
   return 0;
 }
+import _pylibcb
+
+# aliasing these exceptions for convenience
+ConnectionFailure = _pylibcb.ConnectionFailure
+Failure = _pylibcb.Failure
+KeyExists = _pylibcb.KeyExists
+OutOfMemory = _pylibcb.OutOfMemory
+Timeout = _pylibcb.Timeout
+
+class Client(object):
+    def __init__(self, host='localhost', user=None, passwd=None, bucket=None, timeout=0):
+        if not user:
+            user = ''
+        if not passwd:
+            passwd = ''
+        if not bucket:
+            bucket = ''
+
+        self.instance = _pylibcb.open(host, user, passwd, bucket)
+        self.timeout = int(timeout * 1000)
+        
+    def get(self, key, timeout=None):
+        if timeout:
+            return _pylibcb.get(self.instance, key, int(timeout * 1000))
+        return _pylibcb.get(self.instance, key, self.timeout)
+
+    def gat(self, key, new_expiry, timeout=None):
+        if timeout:
+            return _pylibcb.get(self.instance, key, int(timeout * 1000), new_expiry)
+        return _pylibcb.get(self.instance, key, self.timeout, new_expiry)
+
+    def get_cas(self, key, timeout=None):
+        if timeout:
+            return _pylibcb.get(self.instance, key, int(timeout * 1000), 0, 1)
+        return _pylibcb.get(self.instance, key, self.timeout, 0, 1)
+
+    def gat_cas(self, key, new_expiry, timeout=None):
+        if timeout:
+            return _pylibcb.get(self.instance, key, int(timeout * 1000), new_expiry, 1)
+        return _pylibcb.get(self.instance, key, self.timeout, new_expiry, 1)
+
+    def set(self, key, value, expiry=0, cas=0):
+        return _pylibcb.set(self.instance, key, value, expiry, cas)
+
+    def remove(self, key, cas=0):
+        return _pylibcb.remove(self.instance, key)
 
 #gcc pylibcb.c -I/opt/couchbase/include -Wl,-rpath -Wl,/opt/couchbase/lib -levent -lcouchbase
 
-
-pylibcb = Extension('pylibcb',
+pylibcb = Extension('_pylibcb',
                     #include_dirs = ['/opt/couchbase/include'],
                     libraries = ['event', 'couchbase'],
                     library_dirs = ['/opt/couchbase/lib'],
 setup (name = 'Pylibcb',
        version = '0.1',
        description = 'pylibcb',
-       ext_modules = [pylibcb])
+       ext_modules = [pylibcb],
+       py_modules = ['pylibcb'])