arguments and keywordarguments added for response callbacks
This commit is contained in:
parent
7322eacce9
commit
cd508340e3
14
__init__.py
14
__init__.py
@ -77,12 +77,12 @@ class callback_storage(dict):
|
||||
pass # nothing to append
|
||||
return None
|
||||
|
||||
def add(self, service_id, data_id, callback):
|
||||
def add(self, service_id, data_id, callback, *args, **kwargs):
|
||||
if self.get(service_id, data_id) is not None:
|
||||
raise RegistrationError("Callback for service_id (%s) and data_id (%s) already exists" % (repr(service_id), repr(data_id)))
|
||||
if service_id not in self:
|
||||
self[service_id] = {}
|
||||
self[service_id][data_id] = callback
|
||||
self[service_id][data_id] = (callback, args, kwargs)
|
||||
|
||||
|
||||
class data_storage(dict):
|
||||
@ -261,7 +261,7 @@ class struct_json_protocol(object):
|
||||
repr(msg.get_data_id()),
|
||||
repr(msg.get_data())
|
||||
)
|
||||
callback = self.__callbacks__.get(msg.get_service_id(), msg.get_data_id())
|
||||
callback, args, kwargs = self.__callbacks__.get(msg.get_service_id(), msg.get_data_id())
|
||||
if msg.get_service_id() in self.SID_RESPONSE_DICT.keys():
|
||||
#
|
||||
# REQUEST RECEIVED
|
||||
@ -277,7 +277,7 @@ class struct_json_protocol(object):
|
||||
else:
|
||||
try:
|
||||
logger.debug("%s Executing callback %s to process received data", self.LOG_PREFIX, callback.__name__)
|
||||
status, data = callback(msg)
|
||||
status, data = callback(msg, *args, **kwargs)
|
||||
except TypeError:
|
||||
raise TypeError('Check return value of callback function {callback_name} for service_id {service_id} and data_id {data_id}'.format(callback_name=callback.__name__, service_id=repr(msg.get_service_id()), data_id=repr(msg.get_data_id())))
|
||||
self.send(self.SID_RESPONSE_DICT[msg.get_service_id()], msg.get_data_id(), data, status=status)
|
||||
@ -294,7 +294,7 @@ class struct_json_protocol(object):
|
||||
else:
|
||||
try:
|
||||
logger.debug("%s Executing callback %s to process received data", self.LOG_PREFIX, callback.__name__)
|
||||
status, data = callback(msg)
|
||||
status, data = callback(msg, *args, **kwargs)
|
||||
except TypeError:
|
||||
raise TypeError('Check return value of callback function {callback_name} for service_id {service_id} and data_id {data_id}'.format(callback_name=callback.__name__, service_id=repr(msg.get_service_id()), data_id=repr(msg.get_data_id())))
|
||||
|
||||
@ -349,7 +349,7 @@ class struct_json_protocol(object):
|
||||
logger.log(log_lvl, '%s TX -> status: %d, service_id: %d, data_id: %d, data: "%s"', self.LOG_PREFIX, status, service_id, data_id, repr(data))
|
||||
return self.__comm_inst__.send(self.__build_frame__(service_id, data_id, data, status), timeout=timeout, log_lvl=logging.DEBUG)
|
||||
|
||||
def register_callback(self, service_id, data_id, callback):
|
||||
def register_callback(self, service_id, data_id, callback, *args, **kwargs):
|
||||
"""
|
||||
:param service_id: The Service-ID for the message. See class definitions starting with ``SID_``.
|
||||
:type service_id: int
|
||||
@ -373,7 +373,7 @@ class struct_json_protocol(object):
|
||||
:param msg: A :class:`dict` containing all message information.
|
||||
:returns: status (see class definition starting with ``STATUS_``), response_data (JSON compatible object)
|
||||
"""
|
||||
self.__callbacks__.add(service_id, data_id, callback)
|
||||
self.__callbacks__.add(service_id, data_id, callback, *args, **kwargs)
|
||||
|
||||
def authentificate(self, timeout=2):
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user