mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
"""
|
|
This module offers some utilities, in a way they are work in both Python 2 and 3
|
|
"""
|
|
|
|
import binascii
|
|
import logging
|
|
import sys
|
|
from struct import unpack
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
if sys.version_info[0] == 2:
|
|
import Queue as queue
|
|
else:
|
|
import queue as queue
|
|
|
|
queue = queue # just to use it
|
|
|
|
|
|
def check_unpack(seq, index, pattern, size):
|
|
"""Check that we got size bytes, if so, unpack using pattern"""
|
|
data = seq[index: index + size]
|
|
if len(data) == size:
|
|
return unpack(pattern, data)[0]
|
|
else:
|
|
log.warning(
|
|
"Unpacking of %s bytes failed, insufficient data: %r", size, seq[index:]
|
|
)
|
|
raise ValueError(data, "Expected %s bytes" % (size,))
|
|
|
|
|
|
def usbyte(seq, index):
|
|
return check_unpack(seq, index, "<B", 1)
|
|
|
|
|
|
def ushort(seq, index):
|
|
return check_unpack(seq, index, "<H", 2)
|
|
|
|
|
|
def usint(seq, index):
|
|
return check_unpack(seq, index, "<I", 4)
|
|
|
|
|
|
def str2hex(data): # we need it for python 2+3 compatibility
|
|
# if sys.version_info[0] == 3:
|
|
# data = bytes(data, 'ascii')
|
|
if not isinstance(data, (bytes, bytearray)):
|
|
data = bytes(data, "ascii")
|
|
hexed = binascii.hexlify(data)
|
|
return hexed
|