class RemoteService(object): __namespaces = {} def __init__(self, services=None, show_stack_trace=False): ''' ``RemoteService`` wraps our remote service namespace. Takes a json-rpc string and invokes the correct call to the underlying service namespace kw_args: @service(None) - class or namespace/list of namespaces that exposes remote methods. If service is None the namespace is assumed to be ``__main__``. @show_stack_trace(False) - If True, when an exception occurs durring a remote method call send the stack trace to the client. ''' if not services: import __main__ as services if not isinstance(services, list): services = [services] for service in services: #service namespaces can be classes, instances or modules if inspect.ismodule(service): self.__namespaces[service.__name__] = service elif inspect.isclass(service): self.__namespaces[service.__name__] = service() elif hasattr(service, '__class__'): self.__namespaces[service.__class__.__name__] = service else: raise ServiceException(None, 'Invalid service object') log.debug('Namespaces: %s', str(self.__namespaces)) self.show_stack_trace = show_stack_trace self.object_hook = ObjectHook({'RPCRequest': RPCRequest}) def add_object_hook(self, name, handler): self.object_hook.add_hook(name, handler) def handle(self, json_rpc_str): """ Handle a service call and dispatch to the correct method. """ try: rpc = json.loads(json_rpc_str, object_hook=self.object_hook) except ValueError, e: raise except JSONRPCDecodeError, e: return self.error(e)
def __init__(self, services=None, show_stack_trace=False): ''' ``RemoteService`` wraps our remote service namespace. Takes a json-rpc string and invokes the correct call to the underlying service namespace kw_args: @service(None) - class or namespace/list of namespaces that exposes remote methods. If service is None the namespace is assumed to be ``__main__``. @show_stack_trace(False) - If True, when an exception occurs durring a remote method call send the stack trace to the client. ''' if not services: import __main__ as services if not isinstance(services, list): services = [services] for service in services: #service namespaces can be classes, instances or modules if inspect.ismodule(service): self.__namespaces[service.__name__] = service elif inspect.isclass(service): self.__namespaces[service.__name__] = service() elif hasattr(service, '__class__'): self.__namespaces[service.__class__.__name__] = service else: raise ServiceException(None, 'Invalid service object') log.debug('Namespaces: %s', str(self.__namespaces)) self.show_stack_trace = show_stack_trace self.object_hook = ObjectHook({'RPCRequest': RPCRequest})