#!/usr/bin/python # -*- coding: utf-8 -*- # I usually avoid "from x import *" but not when this is such a special-purpose # library that needs to access so many symbols from only one place. from ctypes import * from ctypes.util import * # For some reason these dependencies are invisible to ldd and don't get # handled when loading libchunkd itself. xlibs = {} for lib in ("xml2","ssl","glib-2.0"): full = find_library(lib) x = CDLL(full,mode=RTLD_GLOBAL) #print x xlibs[lib] = x the_lib = CDLL("./libchunkdc.so") #print "fallback lib =", the_lib # new: host, port, user, pw, ssl => client* the_lib.stc_new.argtypes = (c_char_p, c_int, c_char_p, c_char_p, c_int) the_lib.stc_new.restype = c_void_p # free: client* => void the_lib.stc_free.argtypes = (c_void_p,) # get: client*, key, len& => buffer # NULL (failure) gets translated to None the_lib.stc_get_inline.argtypes = (c_void_p, c_char_p, c_void_p) the_lib.stc_get_inline.restype = c_void_p # put: client*, key, buf, len => bool the_lib.stc_put_inline.argtypes = (c_void_p, c_char_p, c_char_p, c_uint64) the_lib.stc_put_inline.restype = c_int # delete: client*, key => bool the_lib.stc_del.argtypes = (c_void_p, c_char_p) the_lib.stc_del.restype = c_int # free (in glib): void* xlibs["glib-2.0"].g_free.argtypes = (c_void_p,) class Chunkd: def __init__ (self, host, port, user, pw): self.client = the_lib.stc_new(host,port,user,pw,0) def __del__ (self): the_lib.stc_free(self.client) def get (self, key, size): my_len = c_uint64(size) raw = the_lib.stc_get_inline(self.client,key,byref(my_len)) if raw: # It sucks to have a copy here, but there's no other # reasonable way to get the raw buffer into a Python # string. tmp = string_at(raw,my_len.value) xlibs["glib-2.0"].g_free(raw) raw = tmp return raw def put (self, key, buf): return the_lib.stc_put_inline(self.client,key,buf,len(buf)) def delete (self, key): return the_lib.stc_del(self.client,key)