Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
value = result[name][0] # drop units
self._log.debug('Found reference %r=%r' % (name, value))
try:
return self.dynamic[(name, value)].parse(data, count, endian, result, message)
except KeyError:
pass
# self._log.warn('No match for dynamic field %s (message %s)' % (self.name, message.name))
for option, field in self.__dynamic_lookup.items():
# self._log.debug('Option: %s -> %r' % (option, field.name))
pass
# and if nothing found, fall though to default behaviour
# have to return name because of dynamic fields
return super().parse(data, count, endian, result, message)
class Message(Named):
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 _add_field(self, field):
self._profile_to_field.add_named(field)
self._number_to_field[field.number] = field
def profile_to_field(self, name):
return self._profile_to_field[name]
def number_to_field(self, value):
return self._number_to_field[value]
self.__add_type(cls(self.__log, name))
return self.profile_to_type(name)
raise
def scale_offset(log, cell, default, name):
if cell is None or cell == '':
return default
try:
return int(cell)
except:
log.warn('Could not parse %r for %s (scale/offset)' % (cell, name))
return default
class MessageField(Named):
def __init__(self, log, name, number, units, type, count=None, scale=1, offset=0):
super().__init__(log, name)
self.number = number
self.units = units if units else ''
self.is_dynamic = False
self.type = type
self.count = count
self.scale = scale_offset(log, scale, 1, name)
self.offset = scale_offset(log, offset, 0, name)
self.__is_scaled = (self.scale != 1 or self.offset != 0)
def parse(self, data, count, endian, result, message):
values = self.type.parse(data, count, endian)
if self.__is_scaled and values is not None:
values = tuple(value / self.scale - self.offset for value in values)
def __init__(self, log, error_msg):
self.__log = log
self.__error_msg = error_msg
super().__init__()
def __getitem__(self, item):
try:
return super().__getitem__(item)
except IndexError:
msg = self.__error_msg % item
self.__log.error(msg)
raise IndexError(msg)
class AbstractType(Named):
def __init__(self, log, name, size, base_type=None):
super().__init__(log, name)
self.base_type = base_type
self.size = size
@abstractmethod
def profile_to_internal(self, cell_contents):
raise NotImplementedError('%s: %s' % (self.__class__.__name__, self.name))
@abstractmethod
def parse(self, bytes, count, endian):
raise NotImplementedError('%s: %s' % (self.__class__.__name__, self.name))
class BaseType(AbstractType):