def __init__(self, id_range):
     self._id_range = id_range
     self._header = MappedBuffer(fields=(
         {
             "length": "B"
         },
         {
             "command_id": "B"
         },
     ))
class Command(object):
    """
    Template of generic command.
    """
    # pylint: disable=too-few-public-methods
    _ranges = ()

    def __init__(self, id_range):
        self._id_range = id_range
        self._header = MappedBuffer(fields=(
            {
                "length": "B"
            },
            {
                "command_id": "B"
            },
        ))

    def _add_range(self, new_range):
        """
        Checks that the range is both formally correct and
        doesn't overalp with existing ones.
        """
        try:
            if (len(new_range) is 2 and 0 <= new_range[0] and
                    new_range[0] <= new_range[1] and new_range[1] < 255) \
               is not True:
                return False
            for registered_range in self._ranges:
                if (registered_range[0] <= new_range[0] and
                        new_range[0] <= registered_range[1]) or \
                    (registered_range[0] <= new_range[1] and
                     new_range[1] <= registered_range[1]):
                    return False
        except TypeError as exception:
            logging.error("Exception {0}\n".format(exception) +
                          "while trying to register exception range: {0}".
                          format(new_range))
            return False

    def pack(self, command_id=CMD_PING, data=""):
        """
        To be used only by derived classes.
        Returns the compacted message.
        """
        self._header.length = \
            self._header.buffer_length + \
            len(data)
        self._header.command_id = command_id
        return self._header.pack() + data
Пример #3
0
class Command(object):
    """
    Template of generic command.
    """

    # pylint: disable=too-few-public-methods
    _ranges = ()

    def __init__(self, id_range):
        self._id_range = id_range
        self._header = MappedBuffer(fields=({"length": "B"}, {"command_id": "B"}))

    def _add_range(self, new_range):
        """
        Checks that the range is both formally correct and
        doesn't overalp with existing ones.
        """
        try:
            if (
                len(new_range) is 2 and 0 <= new_range[0] and new_range[0] <= new_range[1] and new_range[1] < 255
            ) is not True:
                return False
            for registered_range in self._ranges:
                if (registered_range[0] <= new_range[0] and new_range[0] <= registered_range[1]) or (
                    registered_range[0] <= new_range[1] and new_range[1] <= registered_range[1]
                ):
                    return False
        except TypeError as exception:
            logging.error(
                "Exception {0}\n".format(exception) + "while trying to register exception range: {0}".format(new_range)
            )
            return False

    def pack(self, command_id=CMD_PING, data=""):
        """
        To be used only by derived classes.
        Returns the compacted message.
        """
        self._header.length = self._header.buffer_length + len(data)
        self._header.command_id = command_id
        return self._header.pack() + data
Пример #4
0
 def __init__(self, id_range):
     self._id_range = id_range
     self._header = MappedBuffer(fields=({"length": "B"}, {"command_id": "B"}))