2023-10-09 01:10:37 +03:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
2023-10-01 22:41:21 +03:00
|
|
|
class CommandParser:
|
|
|
|
@staticmethod
|
|
|
|
def parse(command_string):
|
|
|
|
|
2023-10-09 01:10:37 +03:00
|
|
|
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
|
|
|
|
2023-10-09 01:10:37 +03:00
|
|
|
return {'command': command, 'value': value}
|
|
|
|
else:
|
|
|
|
raise ValueError("Unknown command: " + command_string)
|