serial-device/command_parser/__init__.py

26 lines
719 B
Python
Raw Normal View History

import re
2023-10-01 22:41:21 +03:00
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]
2023-10-01 22:41:21 +03:00
return {'command': command, 'value': value}
else:
raise ValueError("Unknown command: " + command_string)