mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 17:23:36 +02:00
jw.pkg.cmds.distro.Cmd: Add property util
Provide a property .util from Cmd, instanciated on demand from to the respective distribution directory's Util class. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
05f3f33931
commit
78835ecc9c
1 changed files with 27 additions and 9 deletions
|
|
@ -4,12 +4,17 @@ import os, importlib
|
||||||
|
|
||||||
from ..Cmd import Cmd as Base
|
from ..Cmd import Cmd as Base
|
||||||
from ..CmdDistro import CmdDistro
|
from ..CmdDistro import CmdDistro
|
||||||
|
from .backend.Util import Util
|
||||||
|
|
||||||
class Cmd(Base): # export
|
class Cmd(Base): # export
|
||||||
|
|
||||||
|
from .backend.Backend import Backend
|
||||||
|
|
||||||
def __init__(self, parent: CmdDistro, name: str, help: str) -> None:
|
def __init__(self, parent: CmdDistro, name: str, help: str) -> None:
|
||||||
super().__init__(parent, name, help)
|
super().__init__(parent, name, help)
|
||||||
self.__backend = None
|
self.__backend_path: str|None = None
|
||||||
|
self.__util: Util|None = None
|
||||||
|
self.__backend: Backend|None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def distro_id(self) -> str:
|
def distro_id(self) -> str:
|
||||||
|
|
@ -20,9 +25,8 @@ class Cmd(Base): # export
|
||||||
return self.parent.interactive
|
return self.parent.interactive
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _backend(self):
|
def _backend_path(self):
|
||||||
if self.__backend is None:
|
if self.__backend_path is None:
|
||||||
class_name = self.__class__.__name__[3:] # Get rid of "Cmd"
|
|
||||||
backend_id = self.parent.distro_id.lower().replace('-', '_')
|
backend_id = self.parent.distro_id.lower().replace('-', '_')
|
||||||
match backend_id:
|
match backend_id:
|
||||||
case 'ubuntu' | 'raspbian' | 'kali':
|
case 'ubuntu' | 'raspbian' | 'kali':
|
||||||
|
|
@ -31,14 +35,28 @@ class Cmd(Base): # export
|
||||||
backend_id = 'redhat'
|
backend_id = 'redhat'
|
||||||
case 'opensuse_tumbleweed':
|
case 'opensuse_tumbleweed':
|
||||||
backend_id = 'suse'
|
backend_id = 'suse'
|
||||||
module_name = (
|
self.__backend_path = (
|
||||||
os.path.splitext(__name__)[0]
|
os.path.splitext(__name__)[0]
|
||||||
+ '.backend.'
|
+ '.backend.'
|
||||||
+ backend_id
|
+ backend_id
|
||||||
+ '.'
|
+ '.'
|
||||||
+ class_name
|
|
||||||
)
|
)
|
||||||
module = importlib.import_module(module_name)
|
return self.__backend_path
|
||||||
cls = getattr(module, class_name)
|
|
||||||
self.__backend = cls(self)
|
def _instantiate(self, name: str, *args, **kwargs):
|
||||||
|
module = importlib.import_module(self._backend_path + name)
|
||||||
|
cls = getattr(module, name)
|
||||||
|
return cls(self, *args, **kwargs)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def util(self) -> Util:
|
||||||
|
if self.__util is None:
|
||||||
|
self.__util = self._instantiate('Util')
|
||||||
|
return self.__util
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _backend(self) -> Backend:
|
||||||
|
if self.__backend is None:
|
||||||
|
name = self.__class__.__name__[3:] # Get rid of "Cmd"
|
||||||
|
self.__backend = self._instantiate(name)
|
||||||
return self.__backend
|
return self.__backend
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue