本文整理汇总了Python中dNG.data.binary.Binary.bytes方法的典型用法代码示例。如果您正苦于以下问题:Python Binary.bytes方法的具体用法?Python Binary.bytes怎么用?Python Binary.bytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dNG.data.binary.Binary
的用法示例。
在下文中一共展示了Binary.bytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_blake2_password
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def _get_blake2_password(self, variant, password, username = None):
"""
Returns the BLAKE2 generated password hash.
:param password: User profile password
:param username: User name used while generating BLAKE2 hash
:return: (str) Hash on success; None if not supported
:since: v0.2.00
"""
blake2 = None
blake2_person = None
blake2_salt = None
salt = Settings.get("pas_user_profile_password_salt")
if (salt is None): raise ValueException("User profile password salt is not defined")
if (variant == PasswordGeneratorsMixin.PASSWORD_TYPE_BLAKE2B):
blake2 = blake2b
blake2_salt = Binary.bytes(salt[:blake2b.SALT_SIZE])
blake2_person = Binary.utf8_bytes(username[:blake2b.PERSON_SIZE])
#
if (variant == PasswordGeneratorsMixin.PASSWORD_TYPE_BLAKE2S):
blake2 = blake2s
blake2_salt = Binary.bytes(salt[:blake2s.SALT_SIZE])
blake2_person = Binary.utf8_bytes(username[:blake2s.PERSON_SIZE])
#
if (blake2 is None): raise ValueException("BLAKE2 variant given is invalid")
return blake2(Binary.utf8_bytes(password), salt = blake2_salt, person = blake2_person).hexdigest()
示例2: read
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def read(self, n = 0):
"""
Reads data using the given body reader and parse the JSON response. Chunked
transfer-encoded data is handled automatically.
:param n: Bytes to read
:return: (bytes) Data received
:since: v1.0.0
"""
# pylint: disable=access-member-before-definition, attribute-defined-outside-init, not-callable, used-before-assignment
if (n > 0): raise OperationNotSupportedException()
if (self.json_resource is None):
json_data = self.body_reader()
self.body_reader = None
self.json_resource = JsonResource()
self.json_resource.parse(Binary.str(json_data))
if (self.json_resource.data is None): raise ValueException("Data received is not a valid JSON encoded response")
_return = Binary.bytes(json_data)
else: _return = Binary.bytes("")
return _return
示例3: get_upnp_value
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def get_upnp_value(variable, value):
"""
Returns a valid UPnP encoded value for the given variable definition and
value.
:param variable: Variable definition
:param value: Native python value
:return: (str) UPnP encoded value
:since: v0.2.00
"""
native_type = Variable.get_native_type(variable)
if (type(native_type) is tuple):
value_normalized = (Binary.str(value) if (native_type[0] == str) else value)
value_normalized_type = type(value_normalized)
if (value_normalized_type != native_type[0]): raise ValueException("Given value mismatches defined format")
elif (len(native_type) > 2):
if (native_type[1] != "xmlns"): raise ValueException("Invalid native type definition")
_return = value_normalized
elif (len(native_type[1]) > 1):
if (native_type[1] == "base64"): _return = Binary.str(b64encode(Binary.bytes(value) if (value == value_normalized) else value))
elif (native_type[1] == "f14.4"): _return = "{0:14.4g}".format(value).strip()
elif (native_type[1] == "date"): _return = strftime("%Y-%m-%d", localtime(value))
elif (native_type[1] == "dateTime"): _return = strftime("%Y-%m-%dT%H:%M:%S", localtime(value))
elif (native_type[1] == "dateTime.tz"): _return = strftime("%Y-%m-%dT%H:%M:%S%Z", localtime(value))
elif (native_type[1] == "hex"): _return = Binary.str(hexlify(Binary.bytes(value) if (value == value_normalized) else value))
elif (native_type[1] == "time"): _return = strftime("%H:%M:%S", localtime(value))
elif (native_type[1] == "time.tz"): _return = strftime("%H:%M:%S%Z", localtime(value))
elif (native_type[1] == "uri" and len(urlsplit(value).scheme.strip()) < 1): raise ValueException("Given value is not a valid URI")
elif (native_type[1] == "uuid" and Variable.RE_UUID.match(value_normalized) is None): raise ValueException("Given value is not a valid UUID")
else: _return = value_normalized
else:
pack("={0}".format(native_type[1]),
(Binary.utf8_bytes(value)
if (value_normalized_type == str and value == value_normalized) else
value
)
)
_return = "{0}".format(value_normalized)
#
else:
if (native_type is str): value = Binary.str(value)
if (type(value) is not native_type): raise ValueException("Given value mismatches defined format")
if (native_type is bool): _return = "{0:b}".format(value)
elif (native_type is int): _return = str(value)
elif (native_type is float): _return = "{0:f}".format(value)
else: _return = value
#
return _return
示例4: compress
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def compress(self, string):
"""
python.org: Compress string, returning a string containing compressed data
for at least part of the data in string.
:param string: Original string
:return: (bytes) Compressed string
:since: v1.0.0
"""
if (self.compressor is None): raise IOException("Gzip compressor already flushed and closed")
data = Binary.bytes(string)
if (self.size is None): compressed_data = self.compressor.compress(data)
else:
self.crc32 = (crc32(data) if (self.crc32 is None) else crc32(data, self.crc32))
self.size += len(data)
compressed_data = (self.compressor.compress(data) if (self.header is None) else self.compressor.compress(data)[2:])
#
if (self.header is None): _return = compressed_data
else:
_return = self.header + compressed_data
self.header = None
#
return _return
示例5: __init__
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def __init__(self, level = 6):
"""
Constructor __init__(GzipCompressor)
:since: v1.0.0
"""
self.compressor = None
"""
Deflate compressor instance
"""
self.crc32 = None
"""
CRC32 from previous run
"""
self.header = None
"""
Gzip header
"""
self.size = None
"""
Total size of compressed data
"""
# Use the zlib magic +16 to generate the GZip header and trailer on flush() if supported
try: self.compressor = compressobj(level, wbits = 16 + MAX_WBITS)
except TypeError:
self.compressor = compressobj(level)
if (level == 9): deflate_flag = 2
elif (level == 1): deflate_flag = 4
else: deflate_flag = 0
self.header = pack("<8s2B", Binary.bytes("\x1f\x8b" + ("\x00" if (level == 0) else "\x08") + "\x00\x00\x00\x00\x00"), deflate_flag, 255)
self.size = 0
示例6: read
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def read(self, n = None):
"""
Reads from the current streamer session.
:param n: How many bytes to read from the current position (0 means until
EOF)
:return: (bytes) Data; None if EOF
:since: v1.0.0
"""
if (self._wrapped_resource is None): raise IOException("Wrapped resource not available for reading with {0!r}".format(self))
_return = self._wrapped_resource.read(n)
is_data_uncompressed = (self.compressor is not None)
while (is_data_uncompressed):
if (_return is None):
_return = self.compressor.flush()
self.compressor = None
break
else:
_return = self.compressor.compress(Binary.bytes(_return))
# Feed compressor object with data until it returns at least one byte
if (len(_return) < 1): _return = self._wrapped_resource.read(self.io_chunk_size)
else: is_data_uncompressed = False
#
#
return _return
示例7: chunkify
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def chunkify(self, data):
"""
Returns the formats the client accepts.
:return: (list) Accepted formats
:since: v1.0.0
"""
data = Binary.bytes(data)
if (data is None): _return = Binary.bytes("0\r\n\r\n")
elif (type(data) is type(ChunkedReaderMixin.BINARY_NEWLINE)
and len(data) > 0
): _return = Binary.bytes("{0:x}\r\n".format(len(data))) + data + ChunkedReaderMixin.BINARY_NEWLINE
else: _return = Binary.BYTES_TYPE()
return _return
示例8: data
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def data(self):
"""
Returns buffered data to be transmitted.
:return: (bytes) Data to be send
:since: v1.0.0
"""
return Binary.bytes(self._data)
示例9: data
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def data(self):
"""
Returns buffered data to be transmitted.
:return: (bytes) Data to be send
:since: v1.0.0
"""
return Binary.bytes(JsonResource().data_to_json(self.result))
示例10: marshal
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def marshal(self, serial = None):
"""
Marshals the message for transmission.
:return: (bytes) Wire-formatted message
:since: v0.2.00
"""
if (self.type is None): raise ValueException("D-Bus message type is not defined")
if (serial is None): serial = self.serial
if (serial is None): raise ValueException("D-Bus message serial is not defined")
if (not self.is_header_valid()): raise ValueException("D-Bus message header is not valid")
body_data = Binary.BYTES_TYPE()
body_signature = self.get_body_signature()
body_size = 0
is_le = (sys.byteorder == "little")
header_fields = self._get_header_fields()
header_fields_list = [ ( key, header_fields[key] ) for key in header_fields ]
if (self.body is not None
and Message.HEADER_FIELD_SIGNATURE not in header_fields
): header_fields_list.append(( Message.HEADER_FIELD_SIGNATURE, body_signature ))
header_fields_data = Message.marshal_data("a(yv)", [ header_fields_list ], is_le, 12)
if (self.body is not None):
body_data = Message.marshal_data(body_signature,
self.body,
is_le,
12 + len(header_fields_data)
)
body_size = len(body_data)
#
header_data = pack("ccBBII",
Binary.bytes("l" if (is_le) else "B"),
self.type,
self.flags,
self.__class__.PROTOCOL_VERSION,
body_size,
serial
)
return (header_data + header_fields_data + body_data)
示例11: unmarshal_data
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def unmarshal_data(signature, data, is_le = False, position = 0):
"""
Unmarshals data based on the given D-Bus signature.
:param signature: D-Bus signature
:param data: Wire-formatted data
:param is_le: True if message contains data in little endian byte order
:param position: Current read position in the wire-formatted data
:return: (mixed) Single basic type data or list of unmarshaled data
:since: v0.2.00
"""
data = Binary.bytes(data)
return Message._unmarshal_data_walker(signature, data, is_le, position)[1]
示例12: compress
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def compress(self, string):
"""
python.org: Compress string, returning a string containing compressed data
for at least part of the data in string.
:param string: Original string
:return: (bytes) Compressed string
:since: v1.0.0
"""
if (self.compressor is None): raise IOException("brotli compressor already flushed and closed")
data = Binary.bytes(string)
return (self.compressor.process(data)
if (self._is_compressor_process_defined) else
self.compressor.compress(data)
)
示例13: unmarshal
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def unmarshal(data):
"""
Unmarshals a D-Bus message and returns a Message instance.
:param data: Wire-formatted data
:return: (object) Message instance
:since: v0.2.00
"""
# pylint: disable=protected-access
data = Binary.bytes(data)
data_size = len(data)
if (data_size < 16): raise IOException("D-Bus message is invalid")
header = [ Message.unmarshal_data("y", data[:1]) ]
is_le = (Binary.str(header[0]) == "l")
header += Message.unmarshal_data("yyyuu", data[:12], is_le, 1)
header_size = 12
body_size = header[4]
header_size += Message.get_marshaled_data_size("a(yv)", data, is_le, 12)
if (header_size > data_size): raise IOException("D-Bus message is invalid (calculated header size < size)")
header_fields = Message.unmarshal_data("a(yv)", data[:header_size], is_le, 12)
if (header_size + body_size > data_size): raise IOException("D-Bus message truncated")
elif (header_size + body_size < data_size): raise IOException("D-Bus message is invalid (calculated message size < size)")
_return = Message(header[1])
_return.set_flags(unpack(("<" if (is_le) else ">") + "B", header[2])[0])
_return.set_serial(header[5])
for header_field in header_fields: _return._set_header_field(header_field[0], header_field[1])
if (body_size > 0):
body_signature = _return.get_body_signature()
if (body_signature is None): raise IOException("D-Bus message contains a body without a signature header")
_return.set_body(Message.unmarshal_data(body_signature, data, is_le, header_size))
#
return _return
示例14: _write
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def _write(self, data):
"""
Writes the given data.
:param data: Data to be send
:since: v1.0.0
"""
# pylint: disable=broad-except
if (not self.headers_sent): self.send_headers()
try:
if (self.active and (not self.send_only_headers) and self.wsgi_write is not None):
data = Binary.bytes(data)
self.wsgi_write(data)
#
except Exception: self.active = False
示例15: _prepare_output_data
# 需要导入模块: from dNG.data.binary import Binary [as 别名]
# 或者: from dNG.data.binary.Binary import bytes [as 别名]
def _prepare_output_data(self, data):
"""
Prepare data for output. Compress and transform it if required.
:param data: Data for output
:return: (bytes) Transformed data
:since: v1.0.0
"""
is_chunked_response = (self.stream_mode & AbstractHttpStreamResponse.STREAM_CHUNKED == AbstractHttpStreamResponse.STREAM_CHUNKED)
if (self.compressor is not None):
if (data is None): data = self.compressor.flush()
elif (len(data) > 0): data = self.compressor.compress(Binary.bytes(data))
#
if (is_chunked_response): data = self.chunkify(data)
return data