serial-device/command_parser/__init__.py
2023-10-09 01:10:37 +03:00

25 lines
719 B
Python

import re
class CommandParser:
@staticmethod
def parse(command_string):
if not command_string:
raise ValueError("Empty command received!")
if not isinstance(command_string, str):
raise ValueError("Command must be a sting!")
if re.search('[^a-zA-Z0-9_\-]', command_string):
raise ValueError('Only [a-zA-Z0-9_\-] allowed')
if re.search('^GET_.*', command_string):
parsed_command = command_string.split('_', 1)
command = parsed_command[0]
value = parsed_command[1]
return {'command': command, 'value': value}
else:
raise ValueError("Unknown command: " + command_string)