Merge branch '10-create-menu-to-choose-a-module-to-run-from-main-py' into 'master'

Resolve "Create menu to choose a module to run from main.py"

Closes #10

See merge request icaotix/linux-tools!4
This commit is contained in:
Marcel Schwarz 2019-12-22 01:39:17 +00:00
commit 058bdcd828
6 changed files with 55 additions and 52 deletions

12
AbstractModule.py Normal file
View File

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

View File

@ -1,18 +0,0 @@
class CommandRunner(object):
"""docstring for CommandRunner."""
def __init__(self):
super(CommandRunner).__init__()
def run(self, module):
print("REAL RUNNER")
print("Commands should be executed as subprocess.")
class CommandRunnerDebug(object):
def __init__(self):
super(CommandRunnerDebug).__init__()
def run(self, module):
print("DEBUG RUNNER")
print(module.get_command())

View File

@ -1,8 +0,0 @@
class IModule(object):
"""docstring for Module."""
def __init__(self):
super(IModule, self).__init__()
def get_command(self):
raise NotImplementedError

53
main.py
View File

@ -1,29 +1,48 @@
from CommandRunner import CommandRunnerDebug as Runner from PyInquirer import prompt
import importlib import importlib
import os import os
def get_modules(): def transform_list_into_choices(choices_list):
modules = [] mapped_list = []
for folder in os.listdir("./modules"): for choice in choices_list:
# skipping sample module mapped_list.append({
if folder == "sample": "name": choice
continue })
return mapped_list
print("Module found: " + folder)
def load_modules():
print("Loading modules")
modules = {}
for folder in os.listdir("./modules"):
try:
curr_module = importlib.import_module('.' + folder, package="modules") curr_module = importlib.import_module('.' + folder, package="modules")
print("Try to load module: " + folder) modules[folder] = curr_module.get_module()
modules.append(curr_module.get_module()) except AttributeError:
print("Module loaded successfully: " + folder) pass
print("Modules loaded successfully.\n")
return modules return modules
print("Loading modules: \n") def open_module_selection(modules):
print("Available Modules:")
module_selection = [
{
'type': 'checkbox',
'message': 'Select modules',
'name': 'modules',
'choices': transform_list_into_choices(
list(modules.keys())
)
}
]
return prompt(module_selection)
modules = get_modules()
print("\nRunning all modules \n") modules = load_modules()
cmdRunner = Runner() selected_modules = open_module_selection(modules)
for module in modules:
cmdRunner.run(module) for answer in selected_modules['modules']:
modules[answer].run()

View File

@ -1,14 +1,12 @@
from IModule import IModule from AbstractModule import AbstractModule
class SampleModule(IModule): class SampleModule(AbstractModule):
"""docstring for SampleModule.""" """docstring for SampleModule."""
def __init__(self): def __init__(self):
super(SampleModule, self).__init__() super(SampleModule, self).__init__()
def get_command(self): def run(self):
return [ print("Command 1")
"command 1", print("Command 2")
"command 2"
]

View File

@ -1 +1 @@
PyInquirer==1.0.3