def _parse_datagram(self) -> None: try: self._address_regexp, index = osc_types.get_string(self._dgram, 0) if not self._dgram[index:]: # No params is legit, just return now. return # Get the parameters types. type_tag, index = osc_types.get_string(self._dgram, index) if type_tag.startswith(','): type_tag = type_tag[1:] params = [] param_stack = [params] # Parse each parameter given its type. for param in type_tag: if param == "i": # Integer. val, index = osc_types.get_int(self._dgram, index) elif param == "f": # Float. val, index = osc_types.get_float(self._dgram, index) elif param == "d": # Double. val, index = osc_types.get_double(self._dgram, index) elif param == "s": # String. val, index = osc_types.get_string(self._dgram, index) elif param == "b": # Blob. val, index = osc_types.get_blob(self._dgram, index) elif param == "r": # RGBA. val, index = osc_types.get_rgba(self._dgram, index) elif param == "m": # MIDI. val, index = osc_types.get_midi(self._dgram, index) elif param == "t": # osc time tag: val, index = osc_types.get_ttag(self._dgram, index) elif param == "T": # True. val = True elif param == "F": # False. val = False elif param == "[": # Array start. array = [] param_stack[-1].append(array) param_stack.append(array) elif param == "]": # Array stop. if len(param_stack) < 2: raise ParseError( 'Unexpected closing bracket in type tag: {0}'. format(type_tag)) param_stack.pop() # TODO: Support more exotic types as described in the specification. else: logging.warning( 'Unhandled parameter type: {0}'.format(param)) continue if param not in "[]": param_stack[-1].append(val) if len(param_stack) != 1: raise ParseError( 'Missing closing bracket in type tag: {0}'.format( type_tag)) self._parameters = params except osc_types.ParseError as pe: raise ParseError('Found incorrect datagram, ignoring it', pe)
def _parse_datagram(self): try: self._address_regexp, index = osc_types.get_string(self._dgram, 0) if not self._dgram[index:]: # No params is legit, just return now. return # Get the parameters types. type_tag, index = osc_types.get_string(self._dgram, index) if type_tag.startswith(','): type_tag = type_tag[1:] params = [] param_stack = [params] # Parse each parameter given its type. for param in type_tag: if param == "i": # Integer. val, index = osc_types.get_int(self._dgram, index) elif param == "f": # Float. val, index = osc_types.get_float(self._dgram, index) elif param == "d": # Double. val, index = osc_types.get_double(self._dgram, index) elif param == "s": # String. val, index = osc_types.get_string(self._dgram, index) elif param == "b": # Blob. val, index = osc_types.get_blob(self._dgram, index) elif param == "r": # RGBA. val, index = osc_types.get_rgba(self._dgram, index) elif param == "m": # MIDI. val, index = osc_types.get_midi(self._dgram, index) elif param == "t": # osc time tag: val, index = osc_types.get_ttag(self._dgram, index) elif param == "T": # True. val = True elif param == "F": # False. val = False elif param == "[": # Array start. array = [] param_stack[-1].append(array) param_stack.append(array) elif param == "]": # Array stop. if len(param_stack) < 2: raise ParseError('Unexpected closing bracket in type tag: {0}'.format(type_tag)) param_stack.pop() # TODO: Support more exotic types as described in the specification. else: logging.warning('Unhandled parameter type: {0}'.format(param)) continue if param not in "[]": param_stack[-1].append(val) if len(param_stack) != 1: raise ParseError('Missing closing bracket in type tag: {0}'.format(type_tag)) self._parameters = params except osc_types.ParseError as pe: raise ParseError('Found incorrect datagram, ignoring it', pe)
def test_get_double(self): cases = { b'\x00\x00\x00\x00\x00\x00\x00\x00': (0.0, 8), b'?\xf0\x00\x00\x00\x00\x00\x00': (1.0, 8), b'@\x00\x00\x00\x00\x00\x00\x00': (2.0, 8), b'\xbf\xf0\x00\x00\x00\x00\x00\x00': (-1.0, 8), b'\xc0\x00\x00\x00\x00\x00\x00\x00': (-2.0, 8), b"\x00\x00\x00\x00\x00\x00\x00\x00GARBAGE": (0.0, 8), } for dgram, expected in cases.items(): self.assertAlmostEqual(expected, osc_types.get_double(dgram, 0))