|
| 1 | +#!/usr/bin/env python26 |
| 2 | + |
| 3 | +#from ctypes import CDLL, c_char_p, pointer, byref |
| 4 | +from ctypes import * |
| 5 | + |
| 6 | +libgenders = CDLL("libgenders.so") |
| 7 | + |
| 8 | +class Genders(object): |
| 9 | + def __init__(self, genders_file=None): |
| 10 | + pass |
| 11 | +# self.handle_create() |
| 12 | + |
| 13 | +# if self.load_data(genders_file) != 0: |
| 14 | +# raise Exception(self.errormsg()) |
| 15 | + |
| 16 | + def handle_create(self): |
| 17 | + self._handle = c_void_p(libgenders.genders_handle_create()) |
| 18 | + |
| 19 | + if not self._handle: |
| 20 | + raise Exception("Error allocating memory") |
| 21 | + |
| 22 | + def handle_destroy(self): |
| 23 | + if libgenders.genders_handle_destroy(self._handle) != 0: |
| 24 | + raise Exception(self.errormsg()) |
| 25 | + |
| 26 | + def load_data(self, genders_file=None): |
| 27 | + if libgenders.genders_load_data(self._handle, c_char_p(genders_file)) != 0: |
| 28 | + raise Exception(self.errormsg()) |
| 29 | + |
| 30 | + def isnode(self, node=None): |
| 31 | + node = c_char_p(node) |
| 32 | + return bool(libgenders.genders_isnode(self._handle, node)) |
| 33 | + |
| 34 | + def isattr(self, attr=None): |
| 35 | + attr = c_char_p(attr) |
| 36 | + return bool(libgenders.genders_isattr(self._handle, attr)) |
| 37 | + |
| 38 | + def isattrval(self, attr=None, val=None): |
| 39 | + attr = c_char_p(attr) |
| 40 | + val = c_char_p(val) |
| 41 | + return bool(libgenders.genders_isattrval(self._handle, attr, val)) |
| 42 | + |
| 43 | + def errnum(self): |
| 44 | + return libgenders.genders_errnum(self._handle) |
| 45 | + |
| 46 | + def strerror(self, err): |
| 47 | + libgenders.genders_strerror.restype = c_char_p |
| 48 | + return libgenders.genders_strerror(err) |
| 49 | + |
| 50 | + def errormsg(self): |
| 51 | + libgenders.genders_errormsg.restype = c_char_p |
| 52 | + return libgenders.genders_errormsg(self._handle) |
| 53 | + |
| 54 | + def perror(self, msg=None): |
| 55 | + libgenders.genders_perror(self._handle, msg) |
| 56 | + |
| 57 | + def nodelist_create(self): |
| 58 | + self.node_list = POINTER(POINTER(POINTER(c_char))) |
| 59 | + #libgenders.genders_nodelist_create. |
| 60 | + libgenders.genders_nodelist_create(self._handle, self.node_list) |
| 61 | + |
| 62 | + def query(self, query_str): |
| 63 | + query_str = c_char_p(query_str) |
| 64 | + |
| 65 | + #nodes = byref(c_char_p * 500) |
| 66 | + #nodes = pointer(c_char_p * 500) |
| 67 | + #nodes = POINTER(c_char_p * 500) |
| 68 | + node_buf = (c_char_p * 500)() |
| 69 | + libgenders.genders_query.argtypes = [c_int, POINTER(c_char_p), c_int, c_char_p] |
| 70 | + query_ret = libgenders.genders_query(self._handle, cast(node_buf, POINTER(c_char_p)), 500, query_str) |
| 71 | + print "ret:", query_ret |
| 72 | + |
| 73 | + return node_buf |
0 commit comments