diff --git a/commands/loader.py b/commands/loader.py index 369aa3e..a92b544 100644 --- a/commands/loader.py +++ b/commands/loader.py @@ -8,32 +8,14 @@ from typing import Dict from commands.command_base import AbstractCommand -def load_commands() -> Dict[str, AbstractCommand]: - commands = compile_and_load() - return commands - #New -def load_commands_new(commandType: AbstractCommand.CommandType) -> Dict[str, AbstractCommand]: +def load_commands(commandType: AbstractCommand.CommandType) -> Dict[str, AbstractCommand]: print(" -Loading ", commandType ," Commands...\n") - commands = compile_and_load_new(commandType) + commands = compile_and_load(commandType) return commands -def compile_and_load_file(path: str): - module_name = os.path.split(path)[1].replace(".py", "") - spec = importlib.util.spec_from_file_location(module_name, path) - module = importlib.util.module_from_spec(spec) - sys.modules[module_name] = module - spec.loader.load_module(module_name) - - for name, obj in inspect.getmembers(module): - if inspect.isclass(obj) and name.startswith("Command"): - command_inst = obj() - print("Successfully loaded %s: %s" % (name, command_inst.get_command())) - return command_inst.get_command(), command_inst - return "", None - #New -def compile_and_load_file_new(path: str, commandType: AbstractCommand.CommandType): +def compile_and_load_file(path: str, commandType: AbstractCommand.CommandType): module_name = os.path.split(path)[1].replace(".py", "") spec = importlib.util.spec_from_file_location(module_name, path) module = importlib.util.module_from_spec(spec) @@ -50,28 +32,16 @@ def compile_and_load_file_new(path: str, commandType: AbstractCommand.CommandTyp print(" -%s CommandType did not match: %s for: %s" % (command_inst.get_commandType(), commandType, command_inst.get_command())) return "", None -def compile_and_load() -> Dict[str, AbstractCommand]: - dic = {} - implementations = get_implementations_dir() - for dirName, subdirList, fileList in os.walk(implementations): - for file in fileList: - name = os.path.join(dirName, file) - print("compiling: %s" % name) - name, command = compile_and_load_file(name) - if command is not None: - dic[name] = command - break - return dic #New -def compile_and_load_new(commandType: AbstractCommand.CommandType) -> Dict[str, AbstractCommand]: +def compile_and_load(commandType: AbstractCommand.CommandType) -> Dict[str, AbstractCommand]: dic = {} implementations = get_implementations_dir() for dirName, subdirList, fileList in os.walk(implementations): for file in fileList: name = os.path.join(dirName, file) print("compiling: %s" % name) - name, command = compile_and_load_file_new(name, commandType) + name, command = compile_and_load_file(name, commandType) if command is not None and command.command_type is commandType: dic[name] = command break diff --git a/standalone_command.py b/standalone_command.py index cf905e2..53a6e28 100644 --- a/standalone_command.py +++ b/standalone_command.py @@ -17,7 +17,7 @@ def init(): def load_commands(): global loadedCommands - loadedCommands = command_loader.load_commands_new(AbstractCommand.CommandType.Ver2) + loadedCommands = command_loader.load_commands(AbstractCommand.CommandType.Ver2) def is_command(command: str) -> bool: