1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-06-01 02:05:07 +00:00
libsearpc/pysearpc/client.py

138 lines
3.3 KiB
Python
Raw Normal View History

2014-07-28 06:14:24 +00:00
import json
2019-06-06 09:36:19 +00:00
from .common import SearpcError
2011-04-08 12:58:15 +00:00
2012-06-03 16:19:16 +00:00
def _fret_int(ret_str):
try:
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
2012-10-09 01:48:15 +00:00
2019-06-06 09:36:19 +00:00
if 'err_code' in dicts:
2012-06-03 16:19:16 +00:00
raise SearpcError(dicts['err_msg'])
2012-07-08 12:18:19 +00:00
2019-06-06 09:36:19 +00:00
if 'ret' in dicts:
2012-06-03 16:19:16 +00:00
return dicts['ret']
2012-07-08 12:18:19 +00:00
else:
raise SearpcError('Invalid response format')
2012-06-03 16:19:16 +00:00
def _fret_string(ret_str):
try:
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
2012-10-09 01:48:15 +00:00
2019-06-06 09:36:19 +00:00
if 'err_code' in dicts:
2012-06-03 16:19:16 +00:00
raise SearpcError(dicts['err_msg'])
2012-09-20 02:11:46 +00:00
2019-06-06 09:36:19 +00:00
if 'ret' in dicts:
2012-06-03 16:19:16 +00:00
return dicts['ret']
2012-09-20 02:11:46 +00:00
else:
raise SearpcError('Invalid response format')
2012-06-03 16:19:16 +00:00
2012-05-15 13:50:16 +00:00
class _SearpcObj(object):
'''A compact class to emulate gobject.GObject
'''
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
2012-05-15 13:50:16 +00:00
# For compatibility with old usage peer.props.name
self.props = self
self._dict = new_dict
def __getattr__(self, key):
try:
2012-05-15 13:50:16 +00:00
return self._dict[key]
except:
return None
2013-02-20 08:42:33 +00:00
class SearpcObjEncoder(json.JSONEncoder):
def default(self, obj):
if not isinstance(obj, _SearpcObj):
2014-07-28 06:14:24 +00:00
return super(SearpcObjEncoder, self).default(obj)
2013-02-20 08:42:33 +00:00
return obj._dict
def _fret_obj(ret_str):
try:
2012-05-15 13:50:16 +00:00
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
2012-10-09 01:48:15 +00:00
2019-06-06 09:36:19 +00:00
if 'err_code' in dicts:
raise SearpcError(dicts['err_msg'])
2012-10-09 01:48:15 +00:00
if dicts['ret']:
return _SearpcObj(dicts['ret'])
else:
return None
2012-10-09 01:48:15 +00:00
def _fret_objlist(ret_str):
try:
2012-05-15 13:50:16 +00:00
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
2012-10-09 01:48:15 +00:00
2019-06-06 09:36:19 +00:00
if 'err_code' in dicts:
raise SearpcError(dicts['err_msg'])
2012-10-09 01:48:15 +00:00
l = []
if dicts['ret']:
for elt in dicts['ret']:
l.append(_SearpcObj(elt))
2012-10-09 01:48:15 +00:00
return l
2011-04-08 12:58:15 +00:00
def _fret_json(ret_str):
try:
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
2019-06-06 09:36:19 +00:00
if 'err_code' in dicts:
raise SearpcError(dicts['err_msg'])
if dicts['ret']:
return dicts['ret']
else:
return None
2012-06-03 16:19:16 +00:00
2012-05-08 03:59:31 +00:00
def searpc_func(ret_type, param_types):
2012-06-03 16:19:16 +00:00
2011-04-08 12:58:15 +00:00
def decorate(func):
if ret_type == "void":
fret = None
elif ret_type == "object":
fret = _fret_obj
elif ret_type == "objlist":
fret = _fret_objlist
2012-06-03 16:19:16 +00:00
elif ret_type == "int":
fret = _fret_int
elif ret_type == "int64":
fret = _fret_int
elif ret_type == "string":
fret = _fret_string
elif ret_type == "json":
fret = _fret_json
2011-04-08 12:58:15 +00:00
else:
2012-06-03 16:19:16 +00:00
raise SearpcError('Invial return type')
2011-04-08 12:58:15 +00:00
def newfunc(self, *args):
2012-06-03 16:19:16 +00:00
array = [func.__name__] + list(args)
fcall_str = json.dumps(array)
2011-04-08 12:58:15 +00:00
ret_str = self.call_remote_func_sync(fcall_str)
if fret:
2012-06-03 16:19:16 +00:00
return fret(ret_str)
2011-04-08 12:58:15 +00:00
2012-06-03 16:19:16 +00:00
return newfunc
2011-04-08 12:58:15 +00:00
return decorate
class SearpcClient(object):
def call_remote_func_sync(self, fcall_str):
raise NotImplementedError()