initial commit
This commit is contained in:
88
shell/Handlers/PythonappHandler.py
Normal file
88
shell/Handlers/PythonappHandler.py
Normal file
@@ -0,0 +1,88 @@
|
||||
from shell.Handlers.ABS import Handler
|
||||
|
||||
from pythonapp.Instance.Instance import Instance
|
||||
|
||||
|
||||
class PythonappHandler(Handler):
|
||||
|
||||
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.forwarding_table: dict[str, Handler] = {
|
||||
'package': PackageHandler(self)
|
||||
}
|
||||
self.handle_table: dict = {
|
||||
'create': self._create,
|
||||
'load': self._load,
|
||||
'show': self._show,
|
||||
'activate': self._activate,
|
||||
}
|
||||
self._loaded_instances: dict[str, Instance] = {}
|
||||
self._active_instance: Instance | None = None
|
||||
|
||||
|
||||
def _load(self, command: list[str], pos = 0):
|
||||
keys, args = self.parse_arguments(command[pos:], ['path', 'name'])
|
||||
self._check_arg(keys, 'path')
|
||||
if not keys['name']: keys['name'] = 'app'
|
||||
i = Instance(path=str(keys['path']))
|
||||
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']}")
|
||||
|
||||
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
|
||||
|
||||
pass
|
||||
|
||||
@property
|
||||
def active_instance(self):
|
||||
return self._active_instance
|
||||
|
||||
|
||||
class PackageHandler(Handler):
|
||||
def __init__(self,parent: PythonappHandler):
|
||||
super().__init__()
|
||||
self.forwarding_table: dict[str, Handler] = {}
|
||||
self.handle_table: dict = {
|
||||
'install': self._install,
|
||||
'exclude': self._exclude
|
||||
}
|
||||
self.parent = parent
|
||||
|
||||
def _install(self, command: list[str], pos = 0):
|
||||
if not self.parent.active_instance: raise self.execution_error("I don't have active instance yet!")
|
||||
keys, args = self.parse_arguments(command[pos:], ['pin', 'index', 'extra'], key_mode=True)
|
||||
if keys['pin']: pin = True
|
||||
else: pin = False
|
||||
self.parent.active_instance.install_packages(pkgs=args, repo=keys.get('index', None), extra=keys.get('extra', None), pin=pin)
|
||||
|
||||
def _exclude(self, command: list[str], pos = 0):
|
||||
if not self.parent.active_instance: raise self.execution_error("I don't have active instance yet!")
|
||||
pkgs = command[pos:]
|
||||
self.parent.active_instance.exclude_packages(pkgs)
|
||||
|
||||
|
||||
|
||||
|
||||
self.succeed = True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user