Files
vaiola/shell/Handlers/ModelSpaceHandler.py
2025-09-16 20:05:55 +07:00

82 lines
3.2 KiB
Python

from modelspace.ModelPackageSelector import format_bytes
from modelspace.ModelSpace import ModelSpace
from shell.Handlers.ABS import Handler
from modelspace.Repository import global_repo
class ModelSpaceHandler(Handler):
def __init__(self):
super().__init__()
self.forwarding_table: dict[str, Handler] = {
}
self.handle_table: dict = {
'create-inter': self._create_inter,
'install': self._install,
'install-all': self._install_all,
# 'create': self._create,
'load': self._load,
'list': self._list,
'debug': self._debug,
# 'show': self._show,
# 'activate': self._activate,
}
self._loaded_instances: dict[str, ModelSpace] = {}
self._active_instance: ModelSpace | None = None
pass
def _create_inter(self, command: list[str], pos=0):
global_repo.add_model_package_interactive()
self.succeed = True
def _load(self, command: list[str], pos = 0):
keys, args = self.parse_arguments(command[pos:], ['path', 'layout', 'name'])
self._check_arg(keys, 'path')
self._check_arg(keys, 'layout')
if not keys['name']: keys['name'] = 'app'
i = ModelSpace(global_repo, path=keys['path'], layout=keys['layout'])
# if not i.config.created: raise self.execution_error("ACTIVATE INSTANCE: instance not exists")
self._loaded_instances[keys['name']] = i
self._active_instance = i
print(f"instance {keys['path']} loaded and activated. identified by {keys['name']}")
self.succeed = True
# def _activate(self, command: list[str], pos = 0):
# keys, args = self.parse_arguments(command[pos:], ['name'])
# self._check_arg(keys, 'name')
# i = self._loaded_instances.get(command[1], None)
# if i:
# self._active_instance = i
# else:
# raise ValueError(f"pyapp {keys['name']} not loaded")
#
# def _show(self, command: list[str], pos = 0):
# print("Environment type:", self._active_instance.config.env_type)
# # TODO Add new config info (app section)
# def _create(self, command: list[str], pos = 0):
# keys, args = self.parse_arguments(command[pos:], ['env', 'path', 'python'])
def _install(self, command: list[str], pos = 0):
keys, args = self.parse_arguments(command[pos:], ['answer'])
for res in args: self._active_instance.install(res, keys['answer'])
self.succeed = True
def _install_all(self, command: list[str], pos = 0):
for resource in global_repo.model_sub_repo.resources.keys:
self._active_instance.install(resource, answer='all')
self.succeed = True
def _list(self, command: list[str], pos = 0):
keys, args = self.parse_arguments(command[pos:], ['long'])
packages = list(self._active_instance.installed_packages)
lines = [f'{p.name:<{30}} {p.quantization:<{10}} {format_bytes(p.size_bytes):<{10}} {p.version:<{15}}' for p in packages]
lines.sort()
for line in lines:
print(line)
def _debug(self, command: list[str], pos = 0):
self.succeed = True