Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@abstractmethod
def parse(self, bytes, count, endian):
raise NotImplementedError('%s: %s' % (self.__class__.__name__, self.name))
class BaseType(AbstractType):
def __init__(self, log, name, size, func):
super().__init__(log, name, size)
self.__func = func
def profile_to_internal(self, cell_contents):
return self.__func(cell_contents)
class StructSupport(BaseType):
def _pack_bad(self, value):
bad = (bytearray(self.size), bytearray(self.size))
for endian in (LITTLE, BIG):
bytes = value
for i in range(self.size):
j = i if endian == LITTLE else self.size - i - 1
bad[endian][j] = bytes & 0xff
bytes >>= 8
return bad
def _is_bad(self, data, bad):
size = len(bad)
count = len(data) // size
return all(bad == data[size*i:size*(i+1)] for i in range(count))
bytes >>= 8
return bad
def _is_bad(self, data, bad):
size = len(bad)
count = len(data) // size
return all(bad == data[size*i:size*(i+1)] for i in range(count))
def _unpack(self, data, formats, bad, count, endian):
if self._is_bad(data, bad[endian]):
return None
else:
return unpack(formats[endian] % count, data[0:count * self.size])
class String(BaseType):
def __init__(self, log, name):
super().__init__(log, name, 1, str)
def parse(self, bytes, count, endian):
return [str(b''.join(unpack('%dc' % count, bytes)), encoding='utf-8')]
class Boolean(BaseType):
def __init__(self, log, name):
super().__init__(log, name, 1, bool)
def parse(self, bytes, count, endian):
return tuple(bool(byte) for byte in bytes)
match = self.pattern.match(name)
bits = int(match.group(1))
if bits % 8:
raise Exception('Size of %r not a multiple of 8 bits' % name)
super().__init__(log, name, bits // 8, float)
if self.size not in self.size_to_format:
raise Exception('Cannot unpack %d bytes as a float' % self.size)
format = self.size_to_format[self.size]
self.formats = ['<%d' + format, '>%d' + format]
self.bad = self._pack_bad(2 ** bits - 1)
def parse(self, data, count, endian):
return self._unpack(data, self.formats, self.bad, count, endian)
class Mapping(AbstractType):
def __init__(self, log, row, rows, types):
name = row[0]
base_type_name = row[1]
base_type = types.profile_to_type(base_type_name, auto_create=True)
super().__init__(log, name, base_type.size, base_type=base_type)
self._profile_to_internal = ErrorDict(log, 'No internal value for profile %r')
self._internal_to_profile = ErrorDict(log, 'No profile value for internal %r')
for row in rows:
if row[0] or row[2] is None or row[3] is None:
rows.prepend(row)
break
self.__add_mapping(row)
# log.debug('Parsed %d values' % len(self._profile_to_internal))
def profile_to_internal(self, cell_contents):
def __init__(self, log, row, rows, types):
super().__init__(log, row, types)
self.__dynamic_tmp_data = []
self.__dynamic_lookup = ErrorDict(log, 'No dynamic field for %r')
self.references = set()
try:
peek = rows.peek()
while peek[2] and peek[1] is None:
row = next(rows)
for name, value in zip(row[11].split(','), row[12].split(',')):
self.__save_dynamic(name.strip(), value.strip(), row)
peek = rows.peek()
except StopIteration:
return
def make_bound_reminder(db, log, tabs, bar, insert_callback=None):
binder = SingleTableStatic(db, log, 'reminder',
transforms={'start': DATE_ORDINAL, 'finish': DATE_ORDINAL},
insert_callback=insert_callback)
injury = Reminder(log, tabs, bar, binder)
return injury, binder
def make_bound_aim(db, log, tabs, bar, insert_callback=None):
binder = SingleTableStatic(db, log, 'aim',
transforms={'start': DATE_ORDINAL, 'finish': DATE_ORDINAL},
insert_callback=insert_callback)
injury = Definition(log, tabs, bar, binder)
return injury, binder
def __init__(self, log, sheet, types):
self.__log = log
self.__profile_to_message = ErrorDict(log, 'No message for profile %r')
self.__number_to_message = ErrorDict(log, 'No message for number %r')
rows = peekable([cell.value for cell in row] for row in sheet.iter_rows())
for row in rows:
if row[0] and row[0][0].isupper():
self.__log.debug('Skipping %s' % row)
elif row[0]:
# self.__log.info('Parsing message %s' % row[0])
self.__add_message(RowMessage(self.__log, row, rows, types))
self.__add_message(Header(self.__log, types))
def __init__(self, log, name, number=None):
super().__init__(log, name)
self.number = number
self._profile_to_field = ErrorDict(log, 'No field for profile %r')
self._number_to_field = ErrorDict(log, 'No field for number %r')
def __init__(self, log, sheet, to_datetime=True):
self.__log = log
self.__profile_to_type = ErrorDict(log, 'No type for profile %r')
# these are not 'base types' in the same sense as types having base types.
# rather, they are the 'base (integer) types' described in the docs
self.base_types = ErrorList(log, 'No base type for number %r')
self.__add_known_types(to_datetime)
rows = peekable([cell.value for cell in row] for row in sheet.iter_rows())
for row in rows:
if row[0] and row[0][0].isupper():
self.__log.debug('Skipping %s' % row)
elif row[0]:
# self.__log.info('Parsing type %s' % row[0])
self.__add_type(Mapping(self.__log, row, rows, self))
def __init__(self, log, sheet, types):
self.__log = log
self.__profile_to_message = ErrorDict(log, 'No message for profile %r')
self.__number_to_message = ErrorDict(log, 'No message for number %r')
rows = peekable([cell.value for cell in row] for row in sheet.iter_rows())
for row in rows:
if row[0] and row[0][0].isupper():
self.__log.debug('Skipping %s' % row)
elif row[0]:
# self.__log.info('Parsing message %s' % row[0])
self.__add_message(RowMessage(self.__log, row, rows, types))
self.__add_message(Header(self.__log, types))