124 lines
4.7 KiB
Python
124 lines
4.7 KiB
Python
from unicodedata import category
|
|
|
|
from modelspace.ModelPackageSelector import format_bytes
|
|
from modelspace.ModelSpace import ModelSpace
|
|
from modules.civit.client import Client
|
|
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,
|
|
'add-to-collection': self._add_to_collection,
|
|
'init-civit': self._init_civit,
|
|
'pull-civit': self._pull_civit,
|
|
# 'show': self._show,
|
|
# 'activate': self._activate,
|
|
|
|
}
|
|
self._loaded_instances: dict[str, ModelSpace] = {}
|
|
self._active_instance: ModelSpace | None = None
|
|
self.client: Client | None = None
|
|
|
|
def _init_civit(self, command: list[str], pos=0):
|
|
keys, args = self.parse_arguments(command[pos:], ['path', 'key'])
|
|
self._check_arg(keys, 'path')
|
|
self.client = Client(keys['path'], keys['key'])
|
|
self.succeed = True
|
|
|
|
def _create_inter(self, command: list[str], pos=0):
|
|
global_repo.add_model_package_interactive()
|
|
self.succeed = True
|
|
|
|
def _add_to_collection(self, command: list[str], pos=0):
|
|
keys, args = self.parse_arguments(command[pos:], ['pkg', 'collection', 'category', 'ext'])
|
|
self._check_arg(keys, 'pkg')
|
|
self._check_arg(keys, 'collection')
|
|
if keys['ext']:
|
|
internal = False
|
|
category = None
|
|
else:
|
|
internal = True
|
|
category = keys['category']
|
|
global_repo.model_sub_repo.add_package_to_collection(keys['pkg'], keys['collection'], category, internal)
|
|
self.succeed = True
|
|
|
|
def _pull_civit(self, command: list[str], pos=0):
|
|
keys, args = self.parse_arguments(command[pos:], ['model', 'version', 'file'])
|
|
|
|
if keys['model']:
|
|
global_repo.model_sub_repo.pull_civit_package(self.client, keys['model'], keys['version'], keys['file'])
|
|
else:
|
|
while True:
|
|
model = input("Model ID:")
|
|
global_repo.model_sub_repo.pull_civit_package(self.client, model)
|
|
|
|
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'])
|
|
self.succeed = True
|
|
|
|
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
|
|
|
|
|