예제 #1
0
    def inc_hop_count(self):
        """ Increment hop count value.

        @raise XplMessageError: exceeded hop count value
        """
        hop_count = self.hop_count + 1
        match_hop_count = XplMessage.__regexp_hop_count.match(str(hop_count))
        if match_hop_count is None:
            raise XplMessageError("Exceeded hop count value (%s)" %
                                  str(hop_count))
        self.hop_count = hop_count
예제 #2
0
    def set_type(self, type_):
        """ Set the message type.

        @param type_: message type, in ('xpl-stat', 'xpl-trig', 'xpl-cmnd')
        @type type_: str

        @raise XplMessageError: invalid type
        """
        match_type = XplMessage.__regexp_type.match(type_)
        if match_type is None:
            raise XplMessageError("Invalid type (%s)" % type_)
        self.type = type_
예제 #3
0
    def set_hop_count(self, hop_count):
        """ Set hop count value.

        @param hop_count: hop count
        @type hop_count: int or str

        @raise XplMessageError: invalid hop count value
        """
        match_hop_count = XplMessage.__regexp_hop_count.match(str(hop_count))
        if match_hop_count is None:
            raise XplMessageError("Invalid hop count value (%d)" %
                                  int(hop_count))
        self.hop_count = int(hop_count)
예제 #4
0
    def set_schema(self, schema):
        """ Set message schema.

        @param schema: message schema
        @type schema: str

        @raise XplMessageError: invalid schema
        """
        match_schema = XplMessage.__regexp_schema.match(schema)
        if match_schema is None:
            raise XplMessageError("Invalid schema (%s)" % schema)
        match_schema_dict = match_schema.groupdict()
        for key, value in match_schema_dict.iteritems():
            setattr(self, key, value)
예제 #5
0
    def set_target(self, target):
        """ Set target.

        @param target: message target
        @type targer: str

        @raise XplMessageError: invalid target
        """
        match_target = XplMessage.__regexp_target.match(target)
        if match_target is None:
            raise XplMessageError("Invalid target (%s)" % target)
        match_target_dict = match_target.groupdict()
        for key, value in match_target_dict.iteritems():
            setattr(self, key, value)
예제 #6
0
    def set_source(self, source):
        """ Set source.

        @param source: message source
        @type source: str

        @raise XplMessageError: invalid source
        """
        match_source = XplMessage.__regexp_source.match(source)
        if match_source is None:
            raise XplMessageError("Invalid source (%s)" % source)
        match_source_dict = match_source.groupdict()
        for key, value in match_source_dict.iteritems():
            setattr(self, key, value)
예제 #7
0
    def __parse_data(self, data):
        """ Parse message data.

        The data are added to existing ones.

        @param data: message data, as "<name1>=>value1>\n<name2>=<value2>\n..."
        @type data: str

        @raise XplMessageError: invalid data

        @todo: use OrderedDict for data storage
        """
        match_data = XplMessage.__regexp_data.match(data)
        if match_data is None:
            raise XplMessageError("Invalid data")
        match_data_dict = match_data.groupdict()
        data_list = match_data_dict['data'].split('\n')
        for data_ in data_list:
            if data_:
                self.__parse_single_data(data_)
예제 #8
0
    def __parse_single_data(self, data):
        """ Parse single message data.

        The data are added to existing ones.

        @param data: single message data, as "<name>=<value>"
        @type data: str

        @raise XplMessageError: invalid data
        """
        match_single_data = XplMessage.__regexp_single_data.match(data)
        if match_single_data is None:
            raise XplMessageError("Invalid data (%s)" % data)
        match_single_data_dict = match_single_data.groupdict()
        #self.data.append((match_single_data_dict['data_name'], match_single_data_dict['data_value']))
        key = match_single_data_dict['data_name']
        if key in self.data:
            if type(self.data[key]) != list:
                v = self.data[key]
                self.data[key] = [v]
            self.data[key].append(match_single_data_dict['data_value'])
        else:
            self.data[key] = match_single_data_dict['data_value']
예제 #9
0
    def from_packet(self, packet):
        """ Decode message from given packet.

        @param packet: message packet, as sent on the network
        @type packet: str

        @raise XplMessageError: the message packet is incorrect

        @raise XplMessageError: invalid message packet
        """
        match_global = XplMessage.__regexp_global.match(packet)
        if match_global is None:
            raise XplMessageError("Invalid message packet")
        else:
            match_global_dict = match_global.groupdict()
            self.set_type(match_global_dict['type_'])
            self.set_hop_count(match_global_dict['hop_count'])
            self.set_source(match_global_dict['source'])
            self.set_target(match_global_dict['target'])
            self.set_schema(match_global_dict['schema'])
            self.data = OrderedDict()
            data = match_global_dict[
                'data'] + '\n'  # The global match remove the last '\n'
            self.__parse_data(data)