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:
commit
058bdcd828
12
AbstractModule.py
Normal file
12
AbstractModule.py
Normal 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
|
@ -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())
|
@ -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
53
main.py
@ -1,29 +1,48 @@
|
||||
from CommandRunner import CommandRunnerDebug as Runner
|
||||
from PyInquirer import prompt
|
||||
import importlib
|
||||
import os
|
||||
|
||||
|
||||
def get_modules():
|
||||
modules = []
|
||||
for folder in os.listdir("./modules"):
|
||||
# skipping sample module
|
||||
if folder == "sample":
|
||||
continue
|
||||
def transform_list_into_choices(choices_list):
|
||||
mapped_list = []
|
||||
for choice in choices_list:
|
||||
mapped_list.append({
|
||||
"name": choice
|
||||
})
|
||||
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")
|
||||
print("Try to load module: " + folder)
|
||||
modules.append(curr_module.get_module())
|
||||
print("Module loaded successfully: " + folder)
|
||||
modules[folder] = curr_module.get_module()
|
||||
except AttributeError:
|
||||
pass
|
||||
print("Modules loaded successfully.\n")
|
||||
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()
|
||||
for module in modules:
|
||||
cmdRunner.run(module)
|
||||
selected_modules = open_module_selection(modules)
|
||||
|
||||
for answer in selected_modules['modules']:
|
||||
modules[answer].run()
|
||||
|
@ -1,14 +1,12 @@
|
||||
from IModule import IModule
|
||||
from AbstractModule import AbstractModule
|
||||
|
||||
|
||||
class SampleModule(IModule):
|
||||
class SampleModule(AbstractModule):
|
||||
"""docstring for SampleModule."""
|
||||
|
||||
def __init__(self):
|
||||
super(SampleModule, self).__init__()
|
||||
|
||||
def get_command(self):
|
||||
return [
|
||||
"command 1",
|
||||
"command 2"
|
||||
]
|
||||
def run(self):
|
||||
print("Command 1")
|
||||
print("Command 2")
|
||||
|
@ -1 +1 @@
|
||||
|
||||
PyInquirer==1.0.3
|
||||
|
Loading…
Reference in New Issue
Block a user