mirror of
https://github.com/haiwen/libsearpc.git
synced 2025-09-07 07:40:54 +00:00
[pysearpc] remove pygobject dependency.
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user