From 0b8df07e4d1877c029922a345b76c5c0fd6a4b80 Mon Sep 17 00:00:00 2001 From: lins05 Date: Wed, 2 May 2012 15:33:42 +0800 Subject: [PATCH] [pysearpc] remove pygobject dependency. --- .gitignore | 4 ++- configure.ac | 4 --- pysearpc/Makefile.am | 2 +- pysearpc/client.py | 74 ++++++++++++++++++++++++++++++++++++++------ pysearpc/fcallfret.c | 55 -------------------------------- 5 files changed, 68 insertions(+), 71 deletions(-) diff --git a/.gitignore b/.gitignore index d2e2d6c..1eee3a2 100644 --- a/.gitignore +++ b/.gitignore @@ -38,4 +38,6 @@ lib/marshal.h lib/searpc-dfun.h lib/searpc-fcall.h lib/searpc-signature.h -tests/test-searpc \ No newline at end of file +tests/test-searpc +cscope* +pysearpc/rpc_table.py \ No newline at end of file diff --git a/configure.ac b/configure.ac index 7932a17..f3024dc 100644 --- a/configure.ac +++ b/configure.ac @@ -96,7 +96,6 @@ AC_SUBST(SERVER_PKG_RPATH) GLIB_REQUIRED=2.16.0 JSON_GLIB_REQUIRED=0.10.2 -PYGOBJECT_REQUIRED=2.0 # check and subst glib PKG_CHECK_MODULES(GLIB2, [glib-2.0 >= $GLIB_REQUIRED]) @@ -126,9 +125,6 @@ if test x${compile_python} = xyes; then fi fi AM_CHECK_PYTHON_HEADERS(,[AC_MSG_ERROR(could not find Python headers)]) - PKG_CHECK_MODULES(PYGOBJECT, [pygobject-2.0 >= $PYGOBJECT_REQUIRED]) - AC_SUBST(PYGOBJECT_CFLAGS) - AC_SUBST(PYGOBJECT_LIBS) if test "$bwin32" = true; then pyexecdir=${PYTHON_DIR}/Lib/site-packages diff --git a/pysearpc/Makefile.am b/pysearpc/Makefile.am index aa81074..e9f5afa 100644 --- a/pysearpc/Makefile.am +++ b/pysearpc/Makefile.am @@ -3,7 +3,7 @@ genrpc_files = pygencode.py rpc_table.py AM_CFLAGS = -DPKGDATADIR=\"$(pkgdatadir)\" \ -DPACKAGE_DATA_DIR=\""$(pkgdatadir)"\" \ - @GLIB2_CFLAGS@ @PYGOBJECT_CFLAGS@ \ + @GLIB2_CFLAGS@ \ -I/usr/include/python$(PYTHON_VERSION) \ -I${top_builddir}/lib \ -I${top_srcdir}/lib diff --git a/pysearpc/client.py b/pysearpc/client.py index c034514..842c738 100644 --- a/pysearpc/client.py +++ b/pysearpc/client.py @@ -1,6 +1,5 @@ - import fcallfret - +import simplejson as json class SearpcError(Exception): @@ -10,11 +9,65 @@ class SearpcError(Exception): def __str__(self): return self.msg +class _SearpcObjProps(object): + '''A compact class to emulate gobject.GProps + ''' + def __init__(self, dicts): + new_dict = {} + for key in dicts: + value = dicts[key] + # replace hyphen with with underline + new_key = key.replace('-', '_') + new_dict[new_key] = value + + self._dicts = new_dict + + def __getattr__(self, key): + try: + return self._dicts[key] + except: + return None + +class _SearpcObj(object): + '''A compact class to emulate gobject.GObject + ''' + def __init__(self, dicts): + self.props = _SearpcObjProps(dicts) + + +def _fret_obj(ret_str): + try: + dicts = json.loads(ret_str) + except: + raise SearpcError('Invalid response format') + + if dicts.has_key('err_code'): + raise SearpcError(dicts['err_msg']) + + if dicts['ret']: + return _SearpcObj(dicts['ret']) + else: + return None + + +def _fret_objlist(ret_str): + print ret_str + try: + dicts = json.loads(ret_str) + except: + raise SearpcError('Invalid response format') + + if dicts.has_key('err_code'): + raise SearpcError(dicts['err_msg']) + + l = [] + if dicts['ret']: + for elt in dicts['ret']: + l.append(_SearpcObj(elt)) + + return l def searpc_func(ret_type, param_types, ret_obj_class=None): - """ - ret_obj_class is for ret_type 'object', 'objlist' - """ def decorate(func): if len(param_types) == 0: fcall = getattr(fcallfret, 'fcall__void') @@ -22,6 +75,10 @@ def searpc_func(ret_type, param_types, ret_obj_class=None): fcall = getattr(fcallfret, 'fcall__' + '_'.join(param_types)) if ret_type == "void": fret = None + elif ret_type == "object": + fret = _fret_obj + elif ret_type == "objlist": + fret = _fret_objlist else: fret = getattr(fcallfret, 'fret__' + ret_type) @@ -37,11 +94,8 @@ def searpc_func(ret_type, param_types, ret_obj_class=None): def newfunc_obj(self, *args): fcall_str = fcall(func.__name__, *args) ret_str = self.call_remote_func_sync(fcall_str) - if fret: - try: - return fret(ret_obj_class, ret_str) - except fcallfret.error, e: - raise SearpcError(e) + + return fret(ret_str) if ret_obj_class: return newfunc_obj diff --git a/pysearpc/fcallfret.c b/pysearpc/fcallfret.c index c22509d..724dded 100644 --- a/pysearpc/fcallfret.c +++ b/pysearpc/fcallfret.c @@ -22,7 +22,6 @@ #include #include -#include #include <../lib/searpc-client.h> #include @@ -30,58 +29,6 @@ static PyObject *SearpcError; -static PyObject * -SearpcClient_Fret__Object(PyObject *self, PyObject *args) -{ - char *data; - GType type; - PyObject *type_obj; - GObject *res; - GError *error = NULL; - - if (!PyArg_ParseTuple(args, "Os", &type_obj, &data)) - return NULL; - - type = pyg_type_from_object(type_obj); - res = searpc_client_fret__object(type, data, strlen(data), &error); - if (error) { - PyErr_SetString(SearpcError, error->message); - return NULL; - } - - return pygobject_new(res); -} - - -static PyObject * -SearpcClient_Fret__Objlist(PyObject *self, PyObject *args) -{ - char *data; - GType type; - PyObject *type_obj, *res, *tmp; - GList *list, *p; - GError *error = NULL; - - if (!PyArg_ParseTuple(args, "Os", &type_obj, &data)) - return NULL; - - type = pyg_type_from_object(type_obj); - list = searpc_client_fret__objlist(type, data, strlen(data), &error); - if (error) { - PyErr_SetString(SearpcError, error->message); - return NULL; - } - - res = PyList_New(0); - for (p = list; p; p = p->next) { - tmp = pygobject_new(p->data); - PyList_Append(res, tmp); - } - - return res; -} - - static PyObject * SearpcClient_Fret__String(PyObject *self, PyObject *args) { @@ -155,8 +102,6 @@ DL_EXPORT(void) initfcallfret(void) { PyObject *m, *d; - init_pygobject(); - m = Py_InitModule("fcallfret", SearpcClientModule_Functions); d = PyModule_GetDict(m);