Change from dynamic to explicit module loading

dynamic loading was removed to have an overall better flow control,
Remove Abstract Module, 
clear out module __init__.py
This commit is contained in:
Marcel Schwarz 2020-03-06 02:34:57 +01:00
parent 8bdef1d4e8
commit 6061a7d224
14 changed files with 19 additions and 76 deletions

View File

@ -1,12 +0,0 @@
from abc import ABC, abstractmethod
class AbstractModule(ABC):
"""docstring for Module."""
def __init__(self):
super(AbstractModule, self).__init__()
@abstractmethod
def run(self):
pass

27
main.py
View File

@ -1,20 +1,17 @@
import importlib
import os
import fire import fire
from modules.helloworld.HelloWorld import HelloWorld
from modules.install.ToolsInstaller import ToolsInstaller
from modules.swap.SwapModule import SwapModule
from modules.systemupdate.Systemupdate import Systemupdate
from modules.vim.VimModule import VimModule
def load_modules(): modules = {
print("Loading modules") "helloworld": HelloWorld,
loaded_modules = {} "install": ToolsInstaller,
for folder in os.listdir("./modules"): "swap": SwapModule,
try: "update": Systemupdate,
curr_module = importlib.import_module('.' + folder, package="modules") "vim": VimModule
loaded_modules[folder] = curr_module.get_module() }
except AttributeError:
pass
print("Modules loaded successfully.\n")
return loaded_modules
modules = load_modules()
fire.Fire(modules) fire.Fire(modules)

View File

@ -1,12 +1,6 @@
from AbstractModule import AbstractModule class HelloWorld:
class HelloWorld(AbstractModule):
"""docstring for SampleModule.""" """docstring for SampleModule."""
def __init__(self):
super(HelloWorld, self).__init__()
def run(self): def run(self):
print("This shows that your installation is working correctly.") print("This shows that your installation is working correctly.")
print("You can now start using the tool.") print("You can now start using the tool.")

View File

@ -1,5 +0,0 @@
from .helloWorld import HelloWorld
def get_module():
return HelloWorld()

View File

@ -1,17 +1,14 @@
import json import json
import subprocess import subprocess
from AbstractModule import AbstractModule
class ToolsInstaller:
class CmdToolsModule(AbstractModule):
def __init__(self): def __init__(self):
super().__init__() with open("modules/install/programs.json") as config_file:
with open("modules/program-installer/programs.json") as config_file:
self.programs_json = json.load(config_file) self.programs_json = json.load(config_file)
def run(self): def basic_tools(self):
"""Install the most basic tools!""" """Install the most basic tools!"""
print("Try to install basic Command-line tools") print("Try to install basic Command-line tools")

View File

View File

@ -1,5 +0,0 @@
from .module import CmdToolsModule
def get_module():
return CmdToolsModule()

View File

@ -1,7 +1,6 @@
import subprocess import subprocess
from typing import List from typing import List
from AbstractModule import AbstractModule
from PyInquirer import prompt from PyInquirer import prompt
@ -9,10 +8,7 @@ def _run_shell_command(cmd: List[str]) -> str:
return subprocess.check_output(cmd).decode("UTF-8").strip() return subprocess.check_output(cmd).decode("UTF-8").strip()
class SwapModule(AbstractModule): class SwapModule:
def __init__(self):
super().__init__()
def run(self): def run(self):
actions = { actions = {

View File

@ -1,5 +0,0 @@
from .module import SwapModule
def get_module():
return SwapModule()

View File

@ -1,9 +1,7 @@
import subprocess import subprocess
from AbstractModule import AbstractModule
class Systemupdate:
class Systemupdate(AbstractModule):
def run(self): def run(self):
print("Running update") print("Running update")

View File

@ -1,5 +0,0 @@
from .systemupdate import Systemupdate
def get_module():
return Systemupdate()

View File

@ -5,10 +5,8 @@ from pathlib import Path
from PyInquirer import prompt from PyInquirer import prompt
from AbstractModule import AbstractModule
class VimModule:
class VimModule(AbstractModule):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self._debugFlag = False self._debugFlag = False

View File

@ -1,5 +0,0 @@
from .module import VimModule
def get_module():
return VimModule()