46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# IMPORTS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
from src.thirdparty.config import *;
|
|
|
|
from models.generated.commands import *;
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# EXPORTS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
__all__ = [
|
|
'command_from_json',
|
|
'interpret_command',
|
|
];
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# METHODS Convert to appropriate command type
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def command_from_json(command_json: str) -> Command:
|
|
try:
|
|
instructions = json.loads(command_json);
|
|
except:
|
|
raise Exception('Invalid json!');
|
|
try:
|
|
command = Command(**instructions);
|
|
except:
|
|
raise Exception('Invalid instruction format - consult schema!');
|
|
command = interpret_command(command);
|
|
return command;
|
|
|
|
def interpret_command(command: Command) -> Command:
|
|
match command.name:
|
|
case EnumAlgorithmNames.tarjan:
|
|
return CommandTarjan(**command.dict());
|
|
case EnumAlgorithmNames.tsp:
|
|
return CommandTsp(**command.dict());
|
|
case EnumAlgorithmNames.hirschberg:
|
|
return CommandHirschberg(**command.dict());
|
|
raise command;
|