mirror of
https://github.com/haiwen/libsearpc.git
synced 2025-08-04 14:09:21 +00:00
Merge branch 'obj'
This commit is contained in:
commit
3d2bcde10c
2
.gitignore
vendored
2
.gitignore
vendored
@ -39,3 +39,5 @@ lib/searpc-dfun.h
|
||||
lib/searpc-fcall.h
|
||||
lib/searpc-signature.h
|
||||
tests/test-searpc
|
||||
cscope*
|
||||
pysearpc/rpc_table.py
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -22,7 +22,6 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <pygobject.h>
|
||||
|
||||
#include <../lib/searpc-client.h>
|
||||
#include <structmember.h>
|
||||
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user