本文整理汇总了Python中krpc.decoder.Decoder.decode方法的典型用法代码示例。如果您正苦于以下问题:Python Decoder.decode方法的具体用法?Python Decoder.decode怎么用?Python Decoder.decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类krpc.decoder.Decoder
的用法示例。
在下文中一共展示了Decoder.decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decode_default_value
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def decode_default_value(value, typ):
value = base64.b64decode(value)
value = array.array('B', value).tostring()
# Note: following is a workaround for decoding EnumerationType,
# as set_values has not been called
if not isinstance(typ, EnumerationType):
return Decoder.decode(value, typ)
return Decoder.decode(value, Types().sint32_type)
示例2: _run_test_decode_value
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def _run_test_decode_value(self, typ, cases):
for decoded, encoded in cases:
value = Decoder.decode(unhexlify(encoded), self.types.as_type(typ))
if typ in ('float','double'):
self.assertEqual(str(decoded)[0:8], str(value)[0:8])
else:
self.assertEqual(decoded, value)
示例3: __init__
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def __init__(self, name, position, type, attributes, documentation, default_argument=None):
self.name = name
self.type = types.get_parameter_type(position, type, attributes)
self.has_default_argument = default_argument is not None
if default_argument is not None:
default_argument = Decoder.decode(str(bytearray(default_argument)), self.type)
self.default_argument = default_argument
self.documentation = documentation
示例4: update_thread
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def update_thread(connection, stop):
stream_message_type = Types().as_type('KRPC.StreamMessage')
response_type = Types().as_type('KRPC.Response')
while True:
# Read the size and position of the response message
data = b''
while True:
try:
data += connection.partial_receive(1)
size,position = Decoder.decode_size_and_position(data)
break
except IndexError:
pass
except:
#TODO: is there a better way to catch exceptions when the thread is forcibly stopped (e.g. by CTRL+c)?
return
if stop.is_set():
connection.close()
return
# Read and decode the response message
data = connection.receive(size)
response = Decoder.decode(data, stream_message_type)
# Add the data to the cache
with _stream_cache_lock:
for response in response.responses:
id = response.id
if id not in _stream_cache:
continue
# Check for an error response
if response.response.has_error:
_stream_cache[id].value = RPCError(response.response.error)
continue
# Decode the return value and store it in the cache
typ = _stream_cache[id].return_type
value = Decoder.decode(response.response.return_value, typ)
_stream_cache[id].update(value)
示例5: update_thread
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def update_thread(connection):
stream_message_type = Types().as_type('KRPC.StreamMessage')
response_type = Types().as_type('KRPC.Response')
while True:
# Read the size and position of the response message
data = b''
while True:
try:
data += connection.partial_receive(1)
size,position = Decoder.decode_size_and_position(data)
break
except IndexError:
pass
except socket.error:
return
# Read and decode the response message
try:
data = connection.receive(size)
except socket.error:
return
response = Decoder.decode(data, stream_message_type)
# Add the data to the cache
with _stream_cache_lock:
for response in response.responses:
id = response.id
if id not in _stream_cache:
continue
# Check for an error response
if response.response.HasField('error'):
_stream_cache[id].value = RPCError(response.response.error)
continue
# Decode the return value and store it in the cache
typ = _stream_cache[id].return_type
value = Decoder.decode(response.response.return_value, typ)
_stream_cache[id].update(value)
示例6: _parse_procedure
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def _parse_procedure(cls, procedure):
param_names = [_to_snake_case(param.name) for param in procedure.parameters]
param_types = [cls._client._types.get_parameter_type(i, param.type, procedure.attributes) for i,param in enumerate(procedure.parameters)]
param_required = [not param.HasField('default_argument') for param in procedure.parameters]
param_default = []
for param,typ in zip(procedure.parameters, param_types):
if param.HasField('default_argument'):
param_default.append(Decoder.decode(param.default_argument, typ))
else:
param_default.append(None)
return_type = None
if procedure.HasField('return_type'):
return_type = cls._client._types.get_return_type(procedure.return_type, procedure.attributes)
return param_names, param_types, param_required, param_default, return_type
示例7: _receive_response
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def _receive_response(self):
""" Receive data from the server and decode it into a KRPC.Response object """
# Read the size and position of the response message
data = b""
while True:
try:
data += self._rpc_connection.partial_receive(1)
size, position = Decoder.decode_size_and_position(data)
break
except IndexError:
pass
# Read and decode the response message
data = self._rpc_connection.receive(size)
return Decoder.decode(data, self._response_type)
示例8: __init__
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def __init__(
self, name, position, type, attributes, documentation, default_value=None
): # pylint: disable=redefined-builtin
super(Parameter, self).__init__()
self.name = name
self.type = self.types.get_parameter_type(position, type, attributes)
self.has_default_value = default_value is not None
if default_value is not None:
# Note: following is a workaround for decoding EnumType, as set_values has not been called
if not isinstance(self.type, EnumType):
typ = self.type
else:
typ = self.types.as_type("int32")
default_value = Decoder.decode(str(bytearray(base64.b64decode(default_value))), typ)
self.default_value = default_value
self.documentation = documentation
示例9: _parse_procedure
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def _parse_procedure(cls, procedure):
param_names = [snake_case(param.name)
for param in procedure.parameters]
param_types = [cls._client._types.as_type(param.type)
for param in procedure.parameters]
param_required = [not param.default_value
for param in procedure.parameters]
param_default = []
for param, typ in zip(procedure.parameters, param_types):
if param.default_value:
param_default.append(Decoder.decode(param.default_value, typ))
else:
param_default.append(None)
return_type = None
if not Types.is_none_type(procedure.return_type):
return_type = cls._client._types.as_type(procedure.return_type)
return param_names, param_types, param_required, \
param_default, return_type
示例10: _invoke
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def _invoke(self, service, procedure, args=[], kwargs={}, param_names=[], param_types=[], return_type=None):
""" Execute an RPC """
# Build the request
request = self._build_request(service, procedure, args, kwargs, param_names, param_types, return_type)
# Send the request
with self._rpc_connection_lock:
self._send_request(request)
response = self._receive_response()
# Check for an error response
if response.HasField("error"):
raise RPCError(response.error)
# Decode the response and return the (optional) result
result = None
if return_type is not None:
result = Decoder.decode(response.return_value, return_type)
return result
示例11: test_decode_class_none
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def test_decode_class_none(self):
typ = self.types.as_type('Class(ServiceName.ClassName)')
value = Decoder.decode(unhexlify('00'), typ)
self.assertIsNone(value)
示例12: test_decode_class
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def test_decode_class(self):
typ = self.types.as_type('Class(ServiceName.ClassName)')
value = Decoder.decode(unhexlify('ac02'), typ)
self.assertTrue(isinstance(value, typ.python_type))
self.assertEqual(300, value._object_id)
示例13: test_decode_unicode_string
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def test_decode_unicode_string(self):
value = Decoder.decode(unhexlify('03e284a2'), self.types.as_type('string'))
self.assertEqual(b'\xe2\x84\xa2'.decode('utf-8'), value)
示例14: test_decode_value
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def test_decode_value(self):
value = Decoder.decode(unhexlify('ac02'), self.types.as_type('int32'))
self.assertEqual(int(300), value)
示例15: test_decode_message
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode [as 别名]
def test_decode_message(self):
typ = krpc.schema.KRPC.Request
message = '0a0b536572766963654e616d65120d50726f6365647572654e616d65'
request = Decoder.decode(unhexlify(message), self.types.as_type('KRPC.Request'))
self.assertEqual('ServiceName', request.service)
self.assertEqual('ProcedureName', request.procedure)