From 440d72896fffa58e6c73325a66050681e5672ca2 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sun, 15 Dec 2019 06:31:18 +0100 Subject: [PATCH 1/5] Integrate single module runner in main file --- main.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index 3937e18..f5ba3d7 100644 --- a/main.py +++ b/main.py @@ -2,28 +2,27 @@ from CommandRunner import CommandRunnerDebug as Runner import importlib import os +print("Loading modules") +modules = {} +for folder in os.listdir("./modules"): + curr_module = importlib.import_module('.' + folder, package="modules") + modules[folder] = curr_module.get_module() +print("Modules loaded successfully.\n") -def get_modules(): - modules = [] - for folder in os.listdir("./modules"): - # skipping sample module - if folder == "sample": - continue +print("Available Modules:") +for mod in modules.keys(): + print(mod) - print("Module found: " + folder) - 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) - return modules +print() +while True: + chosen_module = input("Please specify your module: ") + if chosen_module in modules.keys(): + break + else: + print("Module was not found.") -print("Loading modules: \n") +print("\nChosen Module to run: " + chosen_module) -modules = get_modules() +Runner().run(modules[chosen_module]) -print("\nRunning all modules \n") - -cmdRunner = Runner() -for module in modules: - cmdRunner.run(module) From 1fbca4a6526215de64a1395b5bb9844e691e6eaf Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sun, 15 Dec 2019 07:32:28 +0100 Subject: [PATCH 2/5] Add exception handling for module discovery --- main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index f5ba3d7..c364f5a 100644 --- a/main.py +++ b/main.py @@ -5,8 +5,11 @@ import os print("Loading modules") modules = {} for folder in os.listdir("./modules"): - curr_module = importlib.import_module('.' + folder, package="modules") - modules[folder] = curr_module.get_module() + try: + curr_module = importlib.import_module('.' + folder, package="modules") + modules[folder] = curr_module.get_module() + except AttributeError: + pass print("Modules loaded successfully.\n") print("Available Modules:") From c47b25592486cfe82b31d0ecea4cdf80414202ea Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Mon, 16 Dec 2019 22:51:06 +0100 Subject: [PATCH 3/5] Update IModule to AbstractModule with ABC class --- AbstractModule.py | 16 ++++++++++++++++ IModule.py | 8 -------- modules/sample/sampleModule.py | 7 +++++-- 3 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 AbstractModule.py delete mode 100644 IModule.py diff --git a/AbstractModule.py b/AbstractModule.py new file mode 100644 index 0000000..fed48da --- /dev/null +++ b/AbstractModule.py @@ -0,0 +1,16 @@ +from abc import ABC, abstractmethod + + +class AbstractModule(ABC): + """docstring for Module.""" + + def __init__(self): + super(AbstractModule, self).__init__() + + @abstractmethod + def get_command(self): + pass + + @abstractmethod + def run(self): + pass diff --git a/IModule.py b/IModule.py deleted file mode 100644 index bdc3bbb..0000000 --- a/IModule.py +++ /dev/null @@ -1,8 +0,0 @@ -class IModule(object): - """docstring for Module.""" - - def __init__(self): - super(IModule, self).__init__() - - def get_command(self): - raise NotImplementedError diff --git a/modules/sample/sampleModule.py b/modules/sample/sampleModule.py index 5fdd363..34e81e3 100644 --- a/modules/sample/sampleModule.py +++ b/modules/sample/sampleModule.py @@ -1,12 +1,15 @@ -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 run(self): + pass + def get_command(self): return [ "command 1", From 79449c8ad32f66f74c59fa3d8137d5c0d6517807 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Mon, 16 Dec 2019 22:51:40 +0100 Subject: [PATCH 4/5] Add command line interface with PyInquirer Add update requirements.txt --- main.py | 60 +++++++++++++++++++++++++++++++----------------- requirements.txt | 2 +- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index c364f5a..2ffc019 100644 --- a/main.py +++ b/main.py @@ -1,31 +1,49 @@ from CommandRunner import CommandRunnerDebug as Runner +from PyInquirer import prompt import importlib import os -print("Loading modules") -modules = {} -for folder in os.listdir("./modules"): - try: - curr_module = importlib.import_module('.' + folder, package="modules") - modules[folder] = curr_module.get_module() - except AttributeError: - pass -print("Modules loaded successfully.\n") -print("Available Modules:") -for mod in modules.keys(): - print(mod) +def transform_list_into_choices(choices_list): + mapped_list = [] + for choice in choices_list: + mapped_list.append({ + "name": choice + }) + return mapped_list -print() -while True: - chosen_module = input("Please specify your module: ") - if chosen_module in modules.keys(): - break - else: - print("Module was not found.") +def load_modules(): + print("Loading modules") + modules = {} + for folder in os.listdir("./modules"): + try: + curr_module = importlib.import_module('.' + folder, package="modules") + modules[folder] = curr_module.get_module() + except AttributeError: + pass + print("Modules loaded successfully.\n") + return modules -print("\nChosen Module to run: " + chosen_module) -Runner().run(modules[chosen_module]) +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 = load_modules() + +selected_modules = open_module_selection(modules) + +for answer in selected_modules['modules']: + Runner().run(modules[answer]) diff --git a/requirements.txt b/requirements.txt index 8b13789..efa466d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ - +PyInquirer==1.0.3 From af169b95986c36f77a200b13f8f38cdcc3b923ae Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 21 Dec 2019 02:49:53 +0100 Subject: [PATCH 5/5] Replace IModule with AbstractModule Delete CommandRunner Set up API to delegate the module execution to the module itself Update SampleModule --- AbstractModule.py | 4 ---- CommandRunner.py | 18 ------------------ main.py | 3 +-- modules/sample/sampleModule.py | 9 ++------- 4 files changed, 3 insertions(+), 31 deletions(-) delete mode 100644 CommandRunner.py diff --git a/AbstractModule.py b/AbstractModule.py index fed48da..4dbdbf0 100644 --- a/AbstractModule.py +++ b/AbstractModule.py @@ -7,10 +7,6 @@ class AbstractModule(ABC): def __init__(self): super(AbstractModule, self).__init__() - @abstractmethod - def get_command(self): - pass - @abstractmethod def run(self): pass diff --git a/CommandRunner.py b/CommandRunner.py deleted file mode 100644 index 8249941..0000000 --- a/CommandRunner.py +++ /dev/null @@ -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()) diff --git a/main.py b/main.py index 2ffc019..9f8f1dd 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,3 @@ -from CommandRunner import CommandRunnerDebug as Runner from PyInquirer import prompt import importlib import os @@ -46,4 +45,4 @@ modules = load_modules() selected_modules = open_module_selection(modules) for answer in selected_modules['modules']: - Runner().run(modules[answer]) + modules[answer].run() diff --git a/modules/sample/sampleModule.py b/modules/sample/sampleModule.py index 34e81e3..6a5b385 100644 --- a/modules/sample/sampleModule.py +++ b/modules/sample/sampleModule.py @@ -8,10 +8,5 @@ class SampleModule(AbstractModule): super(SampleModule, self).__init__() def run(self): - pass - - def get_command(self): - return [ - "command 1", - "command 2" - ] + print("Command 1") + print("Command 2")