mirror of
https://github.com/koush/scrypted.git
synced 2026-06-21 00:50:30 +01:00
server: python formatting
This commit is contained in:
@@ -16,7 +16,7 @@ jsonSerializable.add(list)
|
||||
|
||||
|
||||
async def maybe_await(value):
|
||||
if (inspect.isawaitable(value)):
|
||||
if inspect.isawaitable(value):
|
||||
return await value
|
||||
return value
|
||||
|
||||
@@ -58,69 +58,112 @@ class LocalProxiedEntry(TypedDict):
|
||||
|
||||
|
||||
class RpcProxy(object):
|
||||
def __init__(self, peer, entry: LocalProxiedEntry, proxyConstructorName: str, proxyProps: any, proxyOneWayMethods: List[str]):
|
||||
self.__dict__['__proxy_id'] = entry['id']
|
||||
self.__dict__['__proxy_entry'] = entry
|
||||
self.__dict__['__proxy_constructor'] = proxyConstructorName
|
||||
def __init__(
|
||||
self,
|
||||
peer,
|
||||
entry: LocalProxiedEntry,
|
||||
proxyConstructorName: str,
|
||||
proxyProps: any,
|
||||
proxyOneWayMethods: List[str],
|
||||
):
|
||||
self.__dict__["__proxy_id"] = entry["id"]
|
||||
self.__dict__["__proxy_entry"] = entry
|
||||
self.__dict__["__proxy_constructor"] = proxyConstructorName
|
||||
self.__dict__[RpcPeer.PROPERTY_PROXY_PEER] = peer
|
||||
self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES] = proxyProps
|
||||
self.__dict__['__proxy_oneway_methods'] = proxyOneWayMethods
|
||||
self.__dict__["__proxy_oneway_methods"] = proxyOneWayMethods
|
||||
|
||||
def __aiter__(self):
|
||||
if self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES] and 'Symbol(Symbol.asyncIterator)' in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]:
|
||||
if (
|
||||
self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]
|
||||
and "Symbol(Symbol.asyncIterator)"
|
||||
in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]
|
||||
):
|
||||
return self
|
||||
raise Exception('RpcProxy is not an async iterable')
|
||||
raise Exception("RpcProxy is not an async iterable")
|
||||
|
||||
async def __anext__(self):
|
||||
if self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES] and 'Symbol(Symbol.asyncIterator)' in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]:
|
||||
if (
|
||||
self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]
|
||||
and "Symbol(Symbol.asyncIterator)"
|
||||
in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]
|
||||
):
|
||||
try:
|
||||
return await RpcProxyMethod(self, self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]['Symbol(Symbol.asyncIterator)']['next'])()
|
||||
return await RpcProxyMethod(
|
||||
self,
|
||||
self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES][
|
||||
"Symbol(Symbol.asyncIterator)"
|
||||
]["next"],
|
||||
)()
|
||||
except RPCResultError as e:
|
||||
if e.name == 'StopAsyncIteration':
|
||||
if e.name == "StopAsyncIteration":
|
||||
raise StopAsyncIteration()
|
||||
raise
|
||||
raise Exception('RpcProxy is not an async iterable')
|
||||
raise Exception("RpcProxy is not an async iterable")
|
||||
|
||||
async def aclose(self):
|
||||
if self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES] and 'Symbol(Symbol.asyncIterator)' in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]:
|
||||
if (
|
||||
self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]
|
||||
and "Symbol(Symbol.asyncIterator)"
|
||||
in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]
|
||||
):
|
||||
try:
|
||||
return await RpcProxyMethod(self, self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]['Symbol(Symbol.asyncIterator)']['return'])()
|
||||
return await RpcProxyMethod(
|
||||
self,
|
||||
self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES][
|
||||
"Symbol(Symbol.asyncIterator)"
|
||||
]["return"],
|
||||
)()
|
||||
except Exception:
|
||||
return
|
||||
raise Exception('RpcProxy is not an async iterable')
|
||||
raise Exception("RpcProxy is not an async iterable")
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == '__proxy_finalizer_id':
|
||||
return self.dict['__proxy_entry']['finalizerId']
|
||||
if name == "__proxy_finalizer_id":
|
||||
return self.dict["__proxy_entry"]["finalizerId"]
|
||||
if name in self.__dict__:
|
||||
return self.__dict__[name]
|
||||
if self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES] and name in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]:
|
||||
if (
|
||||
self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]
|
||||
and name in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]
|
||||
):
|
||||
return self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES][name]
|
||||
return RpcProxyMethod(self, name)
|
||||
|
||||
def __setattr__(self, name: str, value: Any) -> None:
|
||||
if name == '__proxy_finalizer_id':
|
||||
self.__dict__['__proxy_entry']['finalizerId'] = value
|
||||
if name == "__proxy_finalizer_id":
|
||||
self.__dict__["__proxy_entry"]["finalizerId"] = value
|
||||
|
||||
return super().__setattr__(name, value)
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self.__dict__[RpcPeer.PROPERTY_PROXY_PEER].__apply__(self.__dict__['__proxy_id'], self.__dict__['__proxy_oneway_methods'], None, args)
|
||||
|
||||
return self.__dict__[RpcPeer.PROPERTY_PROXY_PEER].__apply__(
|
||||
self.__dict__["__proxy_id"],
|
||||
self.__dict__["__proxy_oneway_methods"],
|
||||
None,
|
||||
args,
|
||||
)
|
||||
|
||||
def __apply__(self, method: str, args: list):
|
||||
return self.__dict__[RpcPeer.PROPERTY_PROXY_PEER].__apply__(self.__dict__['__proxy_id'], self.__dict__['__proxy_oneway_methods'], method, args)
|
||||
return self.__dict__[RpcPeer.PROPERTY_PROXY_PEER].__apply__(
|
||||
self.__dict__["__proxy_id"],
|
||||
self.__dict__["__proxy_oneway_methods"],
|
||||
method,
|
||||
args,
|
||||
)
|
||||
|
||||
|
||||
class RpcPeer:
|
||||
RPC_RESULT_ERROR_NAME = 'RPCResultError'
|
||||
PROPERTY_PROXY_PROPERTIES = '__proxy_props'
|
||||
PROPERTY_JSON_COPY_SERIALIZE_CHILDREN = '__json_copy_serialize_children'
|
||||
PROPERTY_PROXY_PEER = '__proxy_peer'
|
||||
RPC_RESULT_ERROR_NAME = "RPCResultError"
|
||||
PROPERTY_PROXY_PROPERTIES = "__proxy_props"
|
||||
PROPERTY_JSON_COPY_SERIALIZE_CHILDREN = "__json_copy_serialize_children"
|
||||
PROPERTY_PROXY_PEER = "__proxy_peer"
|
||||
|
||||
def __init__(self, send: Callable[[object, Callable[[Exception], None], Dict], None]) -> None:
|
||||
def __init__(
|
||||
self, send: Callable[[object, Callable[[Exception], None], Dict], None]
|
||||
) -> None:
|
||||
self.send = send
|
||||
self.peerName = 'Unnamed Peer'
|
||||
self.peerName = "Unnamed Peer"
|
||||
self.params: Mapping[str, any] = {}
|
||||
self.localProxied: Mapping[any, LocalProxiedEntry] = {}
|
||||
self.localProxyMap: Mapping[str, any] = {}
|
||||
@@ -132,7 +175,9 @@ class RpcPeer:
|
||||
self.killed = False
|
||||
self.tags = {}
|
||||
|
||||
def __apply__(self, proxyId: str, oneWayMethods: List[str], method: str, args: list):
|
||||
def __apply__(
|
||||
self, proxyId: str, oneWayMethods: List[str], method: str, args: list
|
||||
):
|
||||
oneway = oneWayMethods and method in oneWayMethods
|
||||
|
||||
if self.killed:
|
||||
@@ -140,7 +185,9 @@ class RpcPeer:
|
||||
if oneway:
|
||||
future.set_result(None)
|
||||
return future
|
||||
future.set_exception(RPCResultError(None, 'RpcPeer has been killed (apply) ' + str(method)))
|
||||
future.set_exception(
|
||||
RPCResultError(None, "RpcPeer has been killed (apply) " + str(method))
|
||||
)
|
||||
return future
|
||||
|
||||
serializationContext: Dict = {}
|
||||
@@ -149,23 +196,24 @@ class RpcPeer:
|
||||
serializedArgs.append(self.serialize(arg, serializationContext))
|
||||
|
||||
rpcApply = {
|
||||
'type': 'apply',
|
||||
'id': None,
|
||||
'proxyId': proxyId,
|
||||
'args': serializedArgs,
|
||||
'method': method,
|
||||
"type": "apply",
|
||||
"id": None,
|
||||
"proxyId": proxyId,
|
||||
"args": serializedArgs,
|
||||
"method": method,
|
||||
}
|
||||
|
||||
if oneway:
|
||||
rpcApply['oneway'] = True
|
||||
rpcApply["oneway"] = True
|
||||
self.send(rpcApply, None, serializationContext)
|
||||
future = Future()
|
||||
future.set_result(None)
|
||||
return future
|
||||
|
||||
async def send(id: str, reject: Callable[[Exception], None]):
|
||||
rpcApply['id'] = id
|
||||
rpcApply["id"] = id
|
||||
self.send(rpcApply, reject, serializationContext)
|
||||
|
||||
return self.createPendingResult(send)
|
||||
|
||||
def kill(self, message: str = None):
|
||||
@@ -174,7 +222,7 @@ class RpcPeer:
|
||||
return
|
||||
self.killed = True
|
||||
|
||||
error = RPCResultError(None, message or 'peer was killed')
|
||||
error = RPCResultError(None, message or "peer was killed")
|
||||
# this.killedDeferred.reject(error);
|
||||
for str, future in self.pendingResults.items():
|
||||
future.set_exception(error)
|
||||
@@ -187,14 +235,14 @@ class RpcPeer:
|
||||
|
||||
def createErrorResult(self, result: Any, e: Exception):
|
||||
s = self.serializeError(e)
|
||||
result['result'] = s
|
||||
result['throw'] = True
|
||||
result["result"] = s
|
||||
result["throw"] = True
|
||||
return result
|
||||
|
||||
def deserializeError(e: Dict) -> RPCResultError:
|
||||
error = RPCResultError(None, e.get('message'))
|
||||
error.stack = e.get('stack')
|
||||
error.name = e.get('name')
|
||||
error = RPCResultError(None, e.get("message"))
|
||||
error.stack = e.get("stack")
|
||||
error.name = e.get("name")
|
||||
return error
|
||||
|
||||
def serializeError(self, e: Exception):
|
||||
@@ -203,16 +251,16 @@ class RpcPeer:
|
||||
message = str(e)
|
||||
|
||||
serialized = {
|
||||
'stack': tb or '[no stack]',
|
||||
'name': name or '[no name]',
|
||||
'message': message or '[no message]',
|
||||
"stack": tb or "[no stack]",
|
||||
"name": name or "[no name]",
|
||||
"message": message or "[no message]",
|
||||
}
|
||||
|
||||
return {
|
||||
'__remote_constructor_name': RpcPeer.RPC_RESULT_ERROR_NAME,
|
||||
'__serialized_value': serialized,
|
||||
"__remote_constructor_name": RpcPeer.RPC_RESULT_ERROR_NAME,
|
||||
"__serialized_value": serialized,
|
||||
}
|
||||
|
||||
|
||||
# def getProxyProperties(value):
|
||||
# return getattr(value, RpcPeer.PROPERTY_PROXY_PROPERTIES, None)
|
||||
|
||||
@@ -220,15 +268,15 @@ class RpcPeer:
|
||||
# setattr(value, RpcPeer.PROPERTY_PROXY_PROPERTIES, properties)
|
||||
|
||||
def prepareProxyProperties(value):
|
||||
if not hasattr(value, '__aiter__') or not hasattr(value, '__anext__'):
|
||||
if not hasattr(value, "__aiter__") or not hasattr(value, "__anext__"):
|
||||
return getattr(value, RpcPeer.PROPERTY_PROXY_PROPERTIES, None)
|
||||
|
||||
props = getattr(value, RpcPeer.PROPERTY_PROXY_PROPERTIES, None) or {}
|
||||
if not props.get('Symbol(Symbol.asyncIterator)'):
|
||||
props['Symbol(Symbol.asyncIterator)'] = {
|
||||
'next': '__anext__',
|
||||
'throw': 'athrow',
|
||||
'return': 'aclose',
|
||||
if not props.get("Symbol(Symbol.asyncIterator)"):
|
||||
props["Symbol(Symbol.asyncIterator)"] = {
|
||||
"next": "__anext__",
|
||||
"throw": "athrow",
|
||||
"return": "aclose",
|
||||
}
|
||||
return props
|
||||
|
||||
@@ -236,34 +284,44 @@ class RpcPeer:
|
||||
return not value or (type(value) in jsonSerializable)
|
||||
|
||||
def serialize(self, value, serializationContext: Dict):
|
||||
if type(value) == dict and value.get(RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN, None):
|
||||
if type(value) == dict and value.get(
|
||||
RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN, None
|
||||
):
|
||||
ret = {}
|
||||
for (key, val) in value.items():
|
||||
for key, val in value.items():
|
||||
ret[key] = self.serialize(val, serializationContext)
|
||||
return ret
|
||||
|
||||
if (RpcPeer.isTransportSafe(value)):
|
||||
if RpcPeer.isTransportSafe(value):
|
||||
return value
|
||||
|
||||
__remote_constructor_name = 'Function' if callable(value) else value.__proxy_constructor if hasattr(
|
||||
value, '__proxy_constructor') else type(value).__name__
|
||||
__remote_constructor_name = (
|
||||
"Function"
|
||||
if callable(value)
|
||||
else (
|
||||
value.__proxy_constructor
|
||||
if hasattr(value, "__proxy_constructor")
|
||||
else type(value).__name__
|
||||
)
|
||||
)
|
||||
|
||||
if isinstance(value, Exception):
|
||||
return self.serializeError(value)
|
||||
|
||||
serializerMapName = self.constructorSerializerMap.get(
|
||||
type(value), None)
|
||||
serializerMapName = self.constructorSerializerMap.get(type(value), None)
|
||||
if serializerMapName:
|
||||
__remote_constructor_name = serializerMapName
|
||||
serializer = self.nameDeserializerMap.get(serializerMapName, None)
|
||||
serialized = serializer.serialize(value, serializationContext)
|
||||
ret = {
|
||||
'__remote_proxy_id': None,
|
||||
'__remote_proxy_finalizer_id': None,
|
||||
'__remote_constructor_name': __remote_constructor_name,
|
||||
'__remote_proxy_props': RpcPeer.prepareProxyProperties(value),
|
||||
'__remote_proxy_oneway_methods': getattr(value, '__proxy_oneway_methods', None),
|
||||
'__serialized_value': serialized,
|
||||
"__remote_proxy_id": None,
|
||||
"__remote_proxy_finalizer_id": None,
|
||||
"__remote_constructor_name": __remote_constructor_name,
|
||||
"__remote_proxy_props": RpcPeer.prepareProxyProperties(value),
|
||||
"__remote_proxy_oneway_methods": getattr(
|
||||
value, "__proxy_oneway_methods", None
|
||||
),
|
||||
"__serialized_value": serialized,
|
||||
}
|
||||
return ret
|
||||
|
||||
@@ -273,26 +331,28 @@ class RpcPeer:
|
||||
proxyId, __remote_proxy_props = self.onProxySerialization(value)
|
||||
else:
|
||||
__remote_proxy_props = RpcPeer.prepareProxyProperties(value)
|
||||
proxyId = proxiedEntry['id']
|
||||
proxyId = proxiedEntry["id"]
|
||||
|
||||
if proxyId != proxiedEntry['id']:
|
||||
raise Exception('onProxySerialization proxy id mismatch')
|
||||
if proxyId != proxiedEntry["id"]:
|
||||
raise Exception("onProxySerialization proxy id mismatch")
|
||||
|
||||
proxiedEntry['finalizerId'] = RpcPeer.generateId()
|
||||
proxiedEntry["finalizerId"] = RpcPeer.generateId()
|
||||
ret = {
|
||||
'__remote_proxy_id': proxyId,
|
||||
'__remote_proxy_finalizer_id': proxiedEntry['finalizerId'],
|
||||
'__remote_constructor_name': __remote_constructor_name,
|
||||
'__remote_proxy_props': __remote_proxy_props,
|
||||
'__remote_proxy_oneway_methods': getattr(value, '__proxy_oneway_methods', None),
|
||||
"__remote_proxy_id": proxyId,
|
||||
"__remote_proxy_finalizer_id": proxiedEntry["finalizerId"],
|
||||
"__remote_constructor_name": __remote_constructor_name,
|
||||
"__remote_proxy_props": __remote_proxy_props,
|
||||
"__remote_proxy_oneway_methods": getattr(
|
||||
value, "__proxy_oneway_methods", None
|
||||
),
|
||||
}
|
||||
return ret
|
||||
|
||||
__proxy_id = getattr(value, '__proxy_id', None)
|
||||
__proxy_id = getattr(value, "__proxy_id", None)
|
||||
__proxy_peer = getattr(value, RpcPeer.PROPERTY_PROXY_PEER, None)
|
||||
if __proxy_id and __proxy_peer == self:
|
||||
ret = {
|
||||
'__local_proxy_id': __proxy_id,
|
||||
"__local_proxy_id": __proxy_id,
|
||||
}
|
||||
return ret
|
||||
|
||||
@@ -303,18 +363,20 @@ class RpcPeer:
|
||||
proxyId = RpcPeer.generateId()
|
||||
|
||||
proxiedEntry = {
|
||||
'id': proxyId,
|
||||
'finalizerId': proxyId,
|
||||
"id": proxyId,
|
||||
"finalizerId": proxyId,
|
||||
}
|
||||
self.localProxied[value] = proxiedEntry
|
||||
self.localProxyMap[proxyId] = value
|
||||
|
||||
ret = {
|
||||
'__remote_proxy_id': proxyId,
|
||||
'__remote_proxy_finalizer_id': proxyId,
|
||||
'__remote_constructor_name': __remote_constructor_name,
|
||||
'__remote_proxy_props': __remote_proxy_props,
|
||||
'__remote_proxy_oneway_methods': getattr(value, '__proxy_oneway_methods', None),
|
||||
"__remote_proxy_id": proxyId,
|
||||
"__remote_proxy_finalizer_id": proxyId,
|
||||
"__remote_constructor_name": __remote_constructor_name,
|
||||
"__remote_proxy_props": __remote_proxy_props,
|
||||
"__remote_proxy_oneway_methods": getattr(
|
||||
value, "__proxy_oneway_methods", None
|
||||
),
|
||||
}
|
||||
|
||||
return ret
|
||||
@@ -322,22 +384,33 @@ class RpcPeer:
|
||||
def finalize(self, localProxiedEntry: LocalProxiedEntry):
|
||||
if self.killed:
|
||||
return
|
||||
id = localProxiedEntry['id']
|
||||
id = localProxiedEntry["id"]
|
||||
self.remoteWeakProxies.pop(id, None)
|
||||
rpcFinalize = {
|
||||
'__local_proxy_id': id,
|
||||
'__local_proxy_finalizer_id': localProxiedEntry['finalizerId'],
|
||||
'type': 'finalize',
|
||||
"__local_proxy_id": id,
|
||||
"__local_proxy_finalizer_id": localProxiedEntry["finalizerId"],
|
||||
"type": "finalize",
|
||||
}
|
||||
self.send(rpcFinalize)
|
||||
|
||||
def newProxy(self, proxyId: str, proxyConstructorName: str, proxyProps: any, proxyOneWayMethods: List[str]):
|
||||
def newProxy(
|
||||
self,
|
||||
proxyId: str,
|
||||
proxyConstructorName: str,
|
||||
proxyProps: any,
|
||||
proxyOneWayMethods: List[str],
|
||||
):
|
||||
localProxiedEntry: LocalProxiedEntry = {
|
||||
'id': proxyId,
|
||||
'finalizerId': None,
|
||||
"id": proxyId,
|
||||
"finalizerId": None,
|
||||
}
|
||||
proxy = RpcProxy(self, localProxiedEntry, proxyConstructorName,
|
||||
proxyProps, proxyOneWayMethods)
|
||||
proxy = RpcProxy(
|
||||
self,
|
||||
localProxiedEntry,
|
||||
proxyConstructorName,
|
||||
proxyProps,
|
||||
proxyOneWayMethods,
|
||||
)
|
||||
wr = weakref.ref(proxy)
|
||||
self.remoteWeakProxies[proxyId] = wr
|
||||
weakref.finalize(proxy, lambda: self.finalize(localProxiedEntry))
|
||||
@@ -350,23 +423,22 @@ class RpcPeer:
|
||||
if type(value) != dict:
|
||||
return value
|
||||
|
||||
copySerializeChildren = value.get(RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN, None)
|
||||
copySerializeChildren = value.get(
|
||||
RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN, None
|
||||
)
|
||||
if copySerializeChildren:
|
||||
ret = {}
|
||||
for (key, val) in value.items():
|
||||
for key, val in value.items():
|
||||
ret[key] = self.deserialize(val, deserializationContext)
|
||||
return ret
|
||||
|
||||
__remote_proxy_id = value.get('__remote_proxy_id', None)
|
||||
__remote_proxy_finalizer_id = value.get(
|
||||
'__remote_proxy_finalizer_id', None)
|
||||
__local_proxy_id = value.get('__local_proxy_id', None)
|
||||
__remote_constructor_name = value.get(
|
||||
'__remote_constructor_name', None)
|
||||
__serialized_value = value.get('__serialized_value', None)
|
||||
__remote_proxy_props = value.get('__remote_proxy_props', None)
|
||||
__remote_proxy_oneway_methods = value.get(
|
||||
'__remote_proxy_oneway_methods', None)
|
||||
__remote_proxy_id = value.get("__remote_proxy_id", None)
|
||||
__remote_proxy_finalizer_id = value.get("__remote_proxy_finalizer_id", None)
|
||||
__local_proxy_id = value.get("__local_proxy_id", None)
|
||||
__remote_constructor_name = value.get("__remote_constructor_name", None)
|
||||
__serialized_value = value.get("__serialized_value", None)
|
||||
__remote_proxy_props = value.get("__remote_proxy_props", None)
|
||||
__remote_proxy_oneway_methods = value.get("__remote_proxy_oneway_methods", None)
|
||||
|
||||
if __remote_constructor_name == RpcPeer.RPC_RESULT_ERROR_NAME:
|
||||
return RpcPeer.deserializeError(__serialized_value)
|
||||
@@ -375,66 +447,71 @@ class RpcPeer:
|
||||
weakref = self.remoteWeakProxies.get(__remote_proxy_id, None)
|
||||
proxy = weakref() if weakref else None
|
||||
if not proxy:
|
||||
proxy = self.newProxy(__remote_proxy_id, __remote_constructor_name,
|
||||
__remote_proxy_props, __remote_proxy_oneway_methods)
|
||||
setattr(proxy, '__proxy_finalizer_id', __remote_proxy_finalizer_id)
|
||||
proxy = self.newProxy(
|
||||
__remote_proxy_id,
|
||||
__remote_constructor_name,
|
||||
__remote_proxy_props,
|
||||
__remote_proxy_oneway_methods,
|
||||
)
|
||||
setattr(proxy, "__proxy_finalizer_id", __remote_proxy_finalizer_id)
|
||||
return proxy
|
||||
|
||||
if __local_proxy_id:
|
||||
ret = self.localProxyMap.get(__local_proxy_id, None)
|
||||
if not ret:
|
||||
raise RPCResultError(
|
||||
None, 'invalid local proxy id %s' % __local_proxy_id)
|
||||
None, "invalid local proxy id %s" % __local_proxy_id
|
||||
)
|
||||
return ret
|
||||
|
||||
deserializer = self.nameDeserializerMap.get(
|
||||
__remote_constructor_name, None)
|
||||
deserializer = self.nameDeserializerMap.get(__remote_constructor_name, None)
|
||||
if deserializer:
|
||||
return deserializer.deserialize(__serialized_value, deserializationContext)
|
||||
|
||||
return value
|
||||
|
||||
def sendResult(self, result: Dict, serializationContext: Dict):
|
||||
self.send(result, lambda e: self.send(self.createErrorResult(result, e, None), None), serializationContext)
|
||||
self.send(
|
||||
result,
|
||||
lambda e: self.send(self.createErrorResult(result, e, None), None),
|
||||
serializationContext,
|
||||
)
|
||||
|
||||
async def handleMessage(self, message: Dict, deserializationContext: Dict):
|
||||
try:
|
||||
messageType = message['type']
|
||||
if messageType == 'param':
|
||||
messageType = message["type"]
|
||||
if messageType == "param":
|
||||
result = {
|
||||
'type': 'result',
|
||||
'id': message['id'],
|
||||
"type": "result",
|
||||
"id": message["id"],
|
||||
}
|
||||
|
||||
serializationContext: Dict = {}
|
||||
try:
|
||||
value = self.params.get(message['param'], None)
|
||||
value = self.params.get(message["param"], None)
|
||||
value = await maybe_await(value)
|
||||
result['result'] = self.serialize(value, serializationContext)
|
||||
result["result"] = self.serialize(value, serializationContext)
|
||||
except Exception as e:
|
||||
tb = traceback.format_exc()
|
||||
self.createErrorResult(
|
||||
result, type(e).__name, str(e), tb)
|
||||
self.createErrorResult(result, type(e).__name, str(e), tb)
|
||||
|
||||
self.sendResult(result, serializationContext)
|
||||
|
||||
elif messageType == 'apply':
|
||||
elif messageType == "apply":
|
||||
result = {
|
||||
'type': 'result',
|
||||
'id': message.get('id', None),
|
||||
"type": "result",
|
||||
"id": message.get("id", None),
|
||||
}
|
||||
method = message.get('method', None)
|
||||
method = message.get("method", None)
|
||||
|
||||
try:
|
||||
serializationContext: Dict = {}
|
||||
target = self.localProxyMap.get(
|
||||
message['proxyId'], None)
|
||||
target = self.localProxyMap.get(message["proxyId"], None)
|
||||
if not target:
|
||||
raise Exception('proxy id %s not found' %
|
||||
message['proxyId'])
|
||||
raise Exception("proxy id %s not found" % message["proxyId"])
|
||||
|
||||
args = []
|
||||
for arg in (message['args'] or []):
|
||||
for arg in message["args"] or []:
|
||||
args.append(self.deserialize(arg, deserializationContext))
|
||||
|
||||
# if method == 'asend' and hasattr(target, '__aiter__') and hasattr(target, '__anext__') and not len(args):
|
||||
@@ -444,47 +521,53 @@ class RpcPeer:
|
||||
if method:
|
||||
if not hasattr(target, method):
|
||||
raise Exception(
|
||||
'target %s does not have method %s' % (type(target), method))
|
||||
"target %s does not have method %s"
|
||||
% (type(target), method)
|
||||
)
|
||||
invoke = getattr(target, method)
|
||||
value = await maybe_await(invoke(*args))
|
||||
else:
|
||||
value = await maybe_await(target(*args))
|
||||
|
||||
result['result'] = self.serialize(value, serializationContext)
|
||||
result["result"] = self.serialize(value, serializationContext)
|
||||
except StopAsyncIteration as e:
|
||||
self.createErrorResult(result, e)
|
||||
except Exception as e:
|
||||
self.createErrorResult(result, e)
|
||||
|
||||
if not message.get('oneway', False):
|
||||
if not message.get("oneway", False):
|
||||
self.sendResult(result, serializationContext)
|
||||
|
||||
elif messageType == 'result':
|
||||
id = message['id']
|
||||
elif messageType == "result":
|
||||
id = message["id"]
|
||||
future = self.pendingResults.get(id, None)
|
||||
if not future:
|
||||
raise RPCResultError(
|
||||
None, 'unknown result %s' % id)
|
||||
raise RPCResultError(None, "unknown result %s" % id)
|
||||
del self.pendingResults[id]
|
||||
deserialized = self.deserialize(message.get('result', None), deserializationContext)
|
||||
if message.get('throw'):
|
||||
deserialized = self.deserialize(
|
||||
message.get("result", None), deserializationContext
|
||||
)
|
||||
if message.get("throw"):
|
||||
future.set_exception(deserialized)
|
||||
else:
|
||||
future.set_result(deserialized)
|
||||
elif messageType == 'finalize':
|
||||
finalizerId = message.get('__local_proxy_finalizer_id', None)
|
||||
proxyId = message['__local_proxy_id']
|
||||
elif messageType == "finalize":
|
||||
finalizerId = message.get("__local_proxy_finalizer_id", None)
|
||||
proxyId = message["__local_proxy_id"]
|
||||
local = self.localProxyMap.get(proxyId, None)
|
||||
if local:
|
||||
localProxiedEntry = self.localProxied.get(local)
|
||||
if localProxiedEntry and finalizerId and localProxiedEntry['finalizerId'] != finalizerId:
|
||||
if (
|
||||
localProxiedEntry
|
||||
and finalizerId
|
||||
and localProxiedEntry["finalizerId"] != finalizerId
|
||||
):
|
||||
# print('mismatch finalizer id', file=sys.stderr)
|
||||
return
|
||||
self.localProxied.pop(local, None)
|
||||
local = self.localProxyMap.pop(proxyId, None)
|
||||
else:
|
||||
raise RPCResultError(
|
||||
None, 'unknown rpc message type %s' % type)
|
||||
raise RPCResultError(None, "unknown rpc message type %s" % type)
|
||||
except Exception as e:
|
||||
print("unhandled rpc error", self.peerName, e)
|
||||
pass
|
||||
@@ -492,12 +575,16 @@ class RpcPeer:
|
||||
randomDigits = string.ascii_uppercase + string.ascii_lowercase + string.digits
|
||||
|
||||
def generateId():
|
||||
return ''.join(random.choices(RpcPeer.randomDigits, k=8))
|
||||
return "".join(random.choices(RpcPeer.randomDigits, k=8))
|
||||
|
||||
async def createPendingResult(self, cb: Callable[[str, Callable[[Exception], None]], None]):
|
||||
async def createPendingResult(
|
||||
self, cb: Callable[[str, Callable[[Exception], None]], None]
|
||||
):
|
||||
future = Future()
|
||||
if self.killed:
|
||||
future.set_exception(RPCResultError(None, 'RpcPeer has been killed (createPendingResult)'))
|
||||
future.set_exception(
|
||||
RPCResultError(None, "RpcPeer has been killed (createPendingResult)")
|
||||
)
|
||||
return future
|
||||
|
||||
id = RpcPeer.generateId()
|
||||
@@ -508,9 +595,10 @@ class RpcPeer:
|
||||
async def getParam(self, param):
|
||||
async def send(id: str, reject: Callable[[Exception], None]):
|
||||
paramMessage = {
|
||||
'id': id,
|
||||
'type': 'param',
|
||||
'param': param,
|
||||
"id": id,
|
||||
"type": "param",
|
||||
"param": param,
|
||||
}
|
||||
self.send(paramMessage, reject)
|
||||
|
||||
return await self.createPendingResult(send)
|
||||
|
||||
Reference in New Issue
Block a user