reversed the progress we made on quoting standardization

master
Luis Alvarez 4 years ago
parent 3b4e2193ec
commit 98a7dabf8c

@ -15,7 +15,7 @@ class Bucket:
def __init__(self):
self.signature = LEVIN_SIGNATURE
if self.signature != LEVIN_SIGNATURE:
raise BadArgumentException("Bender's nightmare")
raise BadArgumentException('Bender\'s nightmare')
self.cb = None
self.return_data = None
@ -59,7 +59,7 @@ class Bucket:
def create_handshake_request(
my_port: int = 0,
network_id: bytes = None,
peer_id: bytes = b"\x41\x41\x41\x41\x41\x41\x41\x41",
peer_id: bytes = b'\x41\x41\x41\x41\x41\x41\x41\x41',
):
"""
Helper function to create a handshake request. Does not require
@ -136,10 +136,10 @@ class Bucket:
return bucket
def header(self):
return b"".join((
return b''.join((
bytes(LEVIN_SIGNATURE),
bytes(self.cb),
b"\x01" if self.return_data else b"\x00",
b'\x01' if self.return_data else b'\x00',
bytes(self.command),
bytes(self.return_code),
bytes(self.flags),

@ -25,13 +25,13 @@ P2P_COMMAND_REQUEST_NETWORK_STATE = c_uint32(P2P_COMMANDS_POOL_BASE + 5)
P2P_COMMAND_REQUEST_PEER_ID = c_uint32(P2P_COMMANDS_POOL_BASE + 6)
P2P_COMMAND_REQUEST_SUPPORT_FLAGS = c_uint32(P2P_COMMANDS_POOL_BASE + 7)
P2P_COMMANDS = {
P2P_COMMAND_HANDSHAKE: "handshake",
P2P_COMMAND_TIMED_SYNC: "timed_sync",
P2P_COMMAND_PING: "ping",
P2P_COMMAND_REQUEST_STAT_INFO: "stat_info",
P2P_COMMAND_REQUEST_NETWORK_STATE: "network_state",
P2P_COMMAND_REQUEST_PEER_ID: "peer_id",
P2P_COMMAND_REQUEST_SUPPORT_FLAGS: "support_flags",
P2P_COMMAND_HANDSHAKE: 'handshake',
P2P_COMMAND_TIMED_SYNC: 'timed_sync',
P2P_COMMAND_PING: 'ping',
P2P_COMMAND_REQUEST_STAT_INFO: 'stat_info',
P2P_COMMAND_REQUEST_NETWORK_STATE: 'network_state',
P2P_COMMAND_REQUEST_PEER_ID: 'peer_id',
P2P_COMMAND_REQUEST_SUPPORT_FLAGS: 'support_flags',
}
PORTABLE_STORAGE_SIGNATUREA = c_uint32(0x01011101)

@ -27,7 +27,7 @@ class _CType:
self.endian = endian
@classmethod
def from_buffer(cls, buffer, endian: str = "little", padding: bytes = None):
def from_buffer(cls, buffer, endian: str = 'little', padding: bytes = None):
if isinstance(buffer, BytesIO):
buffer = buffer.read(cls.NBYTES)
elif isinstance(buffer, socket.socket):
@ -35,23 +35,23 @@ class _CType:
if len(buffer) < cls.NBYTES:
if not padding:
_bytes = "bytes" if cls.NBYTES > 1 else "byte"
_bytes = 'bytes' if cls.NBYTES > 1 else 'byte'
raise BadArgumentException("requires a buffer of %d %s, received %d" % (cls.NBYTES, _bytes, len(buffer)))
func_padding = bytes.ljust if endian == "little" else bytes.rjust
func_padding = bytes.ljust if endian == 'little' else bytes.rjust
buffer = func_padding(buffer, cls.NBYTES, padding)
_endian = "<" if endian == "little" else ">"
type_struct = cls.TYPE_STRUCT if hasattr(cls, "TYPE_STRUCT") else cls.TYPE._type_
value = struct.unpack("%s%s" % (_endian, type_struct), buffer)[0]
_endian = '<' if endian == 'little' else '>'
type_struct = cls.TYPE_STRUCT if hasattr(cls, 'TYPE_STRUCT') else cls.TYPE._type_
value = struct.unpack('%s%s' % (_endian, type_struct), buffer)[0]
return cls(value, endian)
def to_bytes(self):
if isinstance(self.value, (int, bool)):
type_struct = self.TYPE_STRUCT if hasattr(self, "TYPE_STRUCT") else self.TYPE._type_
return struct.pack("%s%s" % ("<" if self.endian == "little" else ">", type_struct), self.value)
type_struct = self.TYPE_STRUCT if hasattr(self, 'TYPE_STRUCT') else self.TYPE._type_
return struct.pack('%s%s' % ('<' if self.endian == 'little' else '>', type_struct), self.value)
elif isinstance(self.value, bytes):
if self.endian == "little":
if self.endian == 'little':
return self.value[::-1]
return self.value
@ -60,7 +60,7 @@ class _CType:
try:
self.value.to_bytes(self.NBYTES, byteorder=self.endian, signed=self.SIGNED)
except OverflowError as e:
raise OverflowError("Value '%d' does not fit in %s." % (self.value, self.__class__.__name__))
raise OverflowError("Value \'%d\' does not fit in %s." % (self.value, self.__class__.__name__))
def __len__(self):
if isinstance(self.value, bytes):
@ -181,15 +181,15 @@ class _CType:
class _IntType(_CType):
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(_IntType, self).__init__(value, endian)
self._overflows()
def __repr__(self):
_signed = "unsigned" if not self.SIGNED else "signed"
_bytes = "bytes" if self.NBYTES > 1 else "byte"
_signed = 'unsigned' if not self.SIGNED else 'signed'
_bytes = 'bytes' if self.NBYTES > 1 else 'byte'
_cls_name = self.__class__.__name__
return "'%d' - %s - %s %d %s" % (self.value, _cls_name, _signed, self.NBYTES, _bytes)
return '\'%d\' - %s - %s %d %s' % (self.value, _cls_name, _signed, self.NBYTES, _bytes)
class c_int16(_IntType):
@ -197,7 +197,7 @@ class c_int16(_IntType):
NBYTES = 2
SIGNED = True
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_int16, self).__init__(value, endian)
@ -206,7 +206,7 @@ class c_uint16(_IntType):
NBYTES = 2
SIGNED = False
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_uint16, self).__init__(value, endian)
@ -215,7 +215,7 @@ class c_int32(_IntType):
NBYTES = 4
SIGNED = True
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_int32, self).__init__(value, endian)
@ -224,7 +224,7 @@ class c_uint32(_IntType):
NBYTES = 4
SIGNED = False
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_uint32, self).__init__(value, endian)
@property
@ -234,11 +234,11 @@ class c_uint32(_IntType):
class c_int64(_IntType):
TYPE = _c_uint64
TYPE_STRUCT = "q"
TYPE_STRUCT = 'q'
NBYTES = 8
SIGNED = True
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_int64, self).__init__(value, endian)
@property
@ -248,11 +248,11 @@ class c_int64(_IntType):
class c_uint64(_IntType):
TYPE = _c_uint64
TYPE_STRUCT = "Q"
TYPE_STRUCT = 'Q'
NBYTES = 8
SIGNED = False
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_uint64, self).__init__(value, endian)
@property
@ -265,12 +265,12 @@ class c_byte(_IntType):
NBYTES = 1
SIGNED = True
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_byte, self).__init__(value, endian)
class c_bytes(_CType):
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_bytes, self).__init__(value, endian)
@ -279,12 +279,12 @@ class c_ubyte(_IntType):
NBYTES = 1
SIGNED = False
def __init__(self, value, endian="little"):
def __init__(self, value, endian='little'):
super(c_ubyte, self).__init__(value, endian)
class c_string(_CType):
ENCODING = "ascii"
ENCODING = 'ascii'
def __init__(self, value):
super(c_string, self).__init__(value)
@ -295,10 +295,10 @@ class c_string(_CType):
class c_bool(_CType):
TYPE_STRUCT = "?"
TYPE_STRUCT = '?'
NBYTES = 1
def __init__(self, value, endian=None):
if value not in [True, False]:
raise BadArgumentException("bool")
raise BadArgumentException('bool')
super(c_bool, self).__init__(value)

@ -43,7 +43,7 @@ class LevinReader:
def read_section_name(self) -> str:
len_name = c_ubyte.from_buffer(self.buffer)
name = self.buffer.read(len_name.value)
return name.decode("ascii")
return name.decode('ascii')
def load_storage_entry(self):
_type = c_ubyte.from_buffer(self.buffer)
@ -125,7 +125,7 @@ class LevinReader:
elif size_mask == PORTABLE_RAW_SIZE_MARK_INT64:
v = rshift(self.read_rest(b, 7), 2)
else:
raise IOError("invalid var_int")
raise IOError('invalid var_int')
return v
def read_rest(self, first_byte: int, _bytes: int):

@ -23,7 +23,7 @@ class LevinWriter:
def put_section(self, section: Section):
self.write_var_in(len(section))
for k, v in section.entries.items():
_k = k.encode("ascii")
_k = k.encode('ascii')
self.write(bytes(c_ubyte(len(_k))))
self.write(_k)
self.serialized_write(v)

Loading…
Cancel
Save