receiver: use built-ins for conversions between int and byte string

This commit is contained in:
Peter F. Patel-Schneider 2022-06-07 07:29:56 -04:00
parent db9c065821
commit f1e2a0c449
7 changed files with 14 additions and 35 deletions

View File

@ -27,6 +27,7 @@ from logging import DEBUG as _DEBUG
from logging import INFO as _INFO
from logging import getLogger
from random import getrandbits as _random_bits
from struct import pack as _pack
from time import time as _timestamp
import hidapi as _hid
@ -37,7 +38,6 @@ from .base_usb import ALL as _RECEIVER_USB_IDS
from .base_usb import DEVICES as _DEVICE_IDS
from .base_usb import other_device_check as _other_device_check
from .common import KwException as _KwException
from .common import pack as _pack
from .common import strhex as _strhex
_log = getLogger(__name__)

View File

@ -20,7 +20,6 @@
from binascii import hexlify as _hexlify
from collections import namedtuple
from struct import pack, unpack
is_string = lambda d: isinstance(d, str)
@ -228,35 +227,15 @@ def strhex(x):
return _hexlify(x).decode('ascii').upper()
def bytes2int(x):
"""Convert a bytes string to an int.
The bytes are assumed to be in most-significant-first order.
"""
assert isinstance(x, bytes)
assert len(x) < 9
qx = (b'\x00' * 8) + x
result, = unpack('!Q', qx[-8:])
# assert x == int2bytes(result, len(x))
return result
def bytes2int(x, signed=False):
return int.from_bytes(x, signed=signed, byteorder='big')
def int2bytes(x, count=None):
"""Convert an int to a bytes representation.
The bytes are ordered in most-significant-first order.
If 'count' is not given, the necessary number of bytes is computed.
"""
assert isinstance(x, int)
result = pack('!Q', x)
assert isinstance(result, bytes)
# assert x == bytes2int(result)
if count is None:
return result.lstrip(b'\x00')
assert isinstance(count, int)
assert count > 0
assert x.bit_length() <= count * 8
return result[-count:]
def int2bytes(x, count=None, signed=False):
if count:
return x.to_bytes(length=count, byteorder='big', signed=signed)
else:
return x.to_bytes(length=8, byteorder='big', signed=signed).lstrip(b'\x00')
class KwException(Exception):

View File

@ -26,6 +26,7 @@ from logging import DEBUG as _DEBUG
from logging import INFO as _INFO
from logging import getLogger
from math import sqrt as _sqrt
from struct import unpack as _unpack
import evdev
import keysyms.keysymdef as _keysymdef
@ -38,7 +39,6 @@ from yaml import dump_all as _yaml_dump_all
from yaml import safe_load_all as _yaml_safe_load_all
from .common import NamedInt
from .common import unpack as _unpack
from .hidpp20 import FEATURE as _F
from .special_keys import CONTROL as _CONTROL

View File

@ -25,6 +25,8 @@ from logging import ERROR as _ERROR
from logging import INFO as _INFO
from logging import WARNING as _WARNING
from logging import getLogger
from struct import pack as _pack
from struct import unpack as _unpack
from typing import List
from . import special_keys
@ -36,8 +38,6 @@ from .common import NamedInts as _NamedInts
from .common import UnsortedNamedInts as _UnsortedNamedInts
from .common import bytes2int as _bytes2int
from .common import int2bytes as _int2bytes
from .common import pack as _pack
from .common import unpack as _unpack
_log = getLogger(__name__)
del getLogger

View File

@ -22,13 +22,13 @@ import threading as _threading
from logging import DEBUG as _DEBUG
from logging import INFO as _INFO
from logging import getLogger
from struct import unpack as _unpack
from . import diversion as _diversion
from . import hidpp10 as _hidpp10
from . import hidpp20 as _hidpp20
from .base import DJ_MESSAGE_ID as _DJ_MESSAGE_ID
from .common import strhex as _strhex
from .common import unpack as _unpack
from .i18n import _
from .status import ALERT as _ALERT
from .status import KEYS as _K

View File

@ -21,13 +21,13 @@ import math
from logging import DEBUG as _DEBUG
from logging import WARNING as _WARNING
from logging import getLogger
from struct import unpack as _unpack
from . import hidpp20 as _hidpp20
from .common import NamedInt as _NamedInt
from .common import NamedInts as _NamedInts
from .common import bytes2int as _bytes2int
from .common import int2bytes as _int2bytes
from .common import unpack as _unpack
from .i18n import _
_log = getLogger(__name__)

View File

@ -20,6 +20,7 @@ from logging import DEBUG as _DEBUG
from logging import INFO as _INFO
from logging import WARN as _WARN
from logging import getLogger
from struct import unpack as _unpack
from time import time as _time
from . import hidpp10 as _hidpp10
@ -29,7 +30,6 @@ from .common import NamedInt as _NamedInt
from .common import NamedInts as _NamedInts
from .common import bytes2int as _bytes2int
from .common import int2bytes as _int2bytes
from .common import unpack as _unpack
from .i18n import _
from .settings import ActionSettingRW as _ActionSettingRW
from .settings import BitFieldSetting as _BitFieldSetting