Files
controller-wisetopdataserver/python/biopro/exp_pro/main.py
T
2021-12-20 14:52:55 +08:00

283 lines
7.8 KiB
Python

from .manager import *
# noinspection PyUnusedLocal
class Main(CliMain):
def __init__(self):
super().__init__()
self.options = self.extend_options(ExpManagerOption())
@cli_flags('-h', '--help', force_return=True)
def _help(self, opt: str):
"""print help document"""
self.print_help()
@cli_flags('-v', '--verbose')
def _verbose(self, opt: str):
"""verbose mode, enable logger"""
LoggerFlag.set_formatter(LoggerFlag.OPTIONS.STYLE_DEFAULT)
@cli_options('-O', value='MODULE')
def _open(self, opt: str, value: str):
"""open module"""
self.options.allow_module.append(value)
@cli_flags('-l', force_return=True)
def _list(self, flag: str):
"""list available protocol"""
m = ExpManager(None, self.options)
m.reload_protocol()
_print_result('list_available_protocol', m.list_available_protocol())
@cli_command('load')
def _load(self, cmd: str, argv: List[str]):
"""load protocol"""
return _LoadMain(cmd, self.options)
@cli_command('info')
def _info(self, cmd: str, argv: List[str]):
"""print protocol information"""
return _InfoMain(cmd, self.options)
@cli_command('eval')
def _eval(self, cmd: str, argv: List[str]):
"""eval protocol in debug mode"""
return _EvalMain(cmd, self.options)
@cli_command('repl')
def _repl(self, cmd: str, argv: List[str]):
"""start repl in experimental protocol manager environment"""
ExpReplMain(self.options).main(argv)
def run(self):
self.print_help()
class _LoadMain(CliSubCommandMain):
def __init__(self, command: str, options: ExpManagerOption):
super().__init__(command)
self.options = options
self.protocol: Optional[str] = None
@cli_flags('-h', '--help', force_return=True)
def _help(self, opt: str):
"""print help document"""
self.print_help()
@cli_arguments(0, value='NAME')
def _protocol(self, pos: int, value: str):
"""protocol name"""
self.protocol = value
def run(self):
if self.protocol is None:
raise ValueError('lost protocol NAME')
m = ExpManager(None, self.options)
for p in m.load_protocol(Path(self.protocol), suppress_warning=False):
_print_exp_pro_type(p)
class _InfoMain(CliSubCommandMain):
def __init__(self, command: str, options: ExpManagerOption):
super().__init__(command)
self.options = options
self.protocol: Optional[str] = None
@cli_flags('-h', '--help', force_return=True)
def _help(self, opt: str):
"""print help document"""
self.print_help()
@cli_arguments(0, value='NAME')
def _protocol(self, pos: int, value: str):
"""protocol name"""
self.protocol = value
def run(self):
if self.protocol is None:
raise ValueError('lost protocol NAME')
manager = ExpManager(None, self.options)
manager.reload_protocol()
info = manager.get_protocol_info(self.protocol)
_print_exp_pro_type(info)
print()
pro = info.new_protocol()
for opt in pro.pro_options():
_print_exp_opt(opt)
class _EvalMain(CliSubCommandMain):
DEFAULT_USER = 'test'
def __init__(self, command: str, options: ExpManagerOption):
super().__init__(command)
self.options = options
self.protocol: Optional[str] = None
self.eval_user = self.DEFAULT_USER
options.debug_mode = True
@cli_flags('-h', '--help', force_return=True)
def _help(self, opt: str):
"""print help document"""
self.print_help()
@cli_options('-u', '--user', value='USER')
def _eval_user(self, opt: str, value: str):
"""eval protocol with user, default is %DEFAULT_USER%"""
self.eval_user = value
@cli_arguments(0, value='NAME')
def _protocol(self, pos: int, value: str):
"""protocol name"""
self.protocol = value
def run(self):
if self.protocol is None:
raise ValueError('lost protocol NAME')
m = ExpManager(_init_server(self.options), self.options)
m.reload_protocol()
m.setup()
p = m.create_protocol(self.protocol, self.eval_user)
m.submit_protocol(p)
m.scheduler.close_on_completed()
m.scheduler.join()
print(m.protocol_status_code(p))
def _init_server(option: ExpManagerOption) -> ControlServerAPI:
from biopro.server.debug import (DebugControllerServer,
DebugControllerOptions,
ControlServerOptions,
DeviceManagerOptions,
DeviceHardwareInterface)
o = DebugControllerOptions()
o.user_validation = False
o.allow_routine_thread = True
o.allow_task_thread = True
c = ControlServerOptions()
c.storage_root = '.BPS'
d = DeviceManagerOptions()
d.interface = DeviceHardwareInterface.INTERFACE_NULL
d.repository = option.repository
return DebugControllerServer(o, c, d)
def _print_result(command: str, result: Any):
if command == 'list_available_protocol':
for exp in result: # type: ExpProtocolInfo
_print_exp_pro_type(exp)
elif command == 'list_protocol':
for exp in result: # type: ExpProtocol
_print_exp_pro(exp)
elif command == 'protocol_options':
for opt in result: # type: ExpOption
_print_exp_opt(opt)
elif isinstance(result, ExpProtocol):
_print_exp_pro(result)
elif isinstance(result, ExpOption):
_print_exp_opt(result)
else:
print(result)
def _print_exp_pro_type(exp: ExpProtocolInfo):
doc = exp.exp_desp
print(pc('protocol', YELLOW), exp.exp_name,
pc('class', GREEN), exp.exp_class,
pc('from', GREEN), exp.exp_filepath)
if doc is not None:
for line in doc.split('\n'):
print(' ', line)
def _print_exp_pro(exp: ExpProtocol):
print('[%d]' % exp.pro_id,
pc('protocol', YELLOW), exp.pro_name,
pc('class', GREEN), exp.pro_class,
pc('from', GREEN), exp.pro_filepath)
def _print_exp_opt(opt: ExpOption):
print(pc('option', YELLOW), opt.name, ':', opt.value_type.__name__, '=', opt.default())
if opt.desp is not None and len(opt.desp) > 0:
print(' ', opt.desp)
print()
class ExpReplMain(CliReplMain, metaclass=CliCommandDelegateMacro(ExpManagerApi, 'exp_manager', _print_result)):
DEFAULT_USER = 'test'
def __init__(self, options: ExpManagerOption):
super().__init__()
self.option = options
self.eval_user = self.DEFAULT_USER
self._manager: ExpManager = None
@cli_flags('-h', '--help', force_return=True)
def _help(self, opt: str):
"""print help document"""
self.print_help()
@cli_options('-u', '--user', value='USER')
def _eval_user(self, opt: str, value: str):
"""eval protocol with user, default is %DEFAULT_USER%"""
self.eval_user = value
@property
def exp_manager(self) -> ExpManager:
return self._manager
def __enter__(self):
if self._manager is None:
self._manager = ExpManager(_init_server(self.option), self.option)
self._manager.reload_protocol()
self._manager.setup()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
m: ExpManager = self._manager
self._manager = None
if m is not None:
m.shutdown()
@cli_command('h', 'help')
def _help_1(self, cmd: str, argv: List[str]):
"""print help documents"""
self.print_help()
if __name__ == '__main__':
LoggerFlag.set_formatter(LoggerFlag.OPTIONS.STYLE_QUIET)
Main().main()