This commit is contained in:
Alex Orid 2021-04-22 13:43:41 -04:00
parent 9c876aeee9
commit 294306896f
2 changed files with 6 additions and 36 deletions

View File

@ -8,32 +8,14 @@ from typing import Dict
from commands.command_base import AbstractCommand from commands.command_base import AbstractCommand
def load_commands() -> Dict[str, AbstractCommand]:
commands = compile_and_load()
return commands
#New #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") print(" -Loading ", commandType ," Commands...\n")
commands = compile_and_load_new(commandType) commands = compile_and_load(commandType)
return commands 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 #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", "") module_name = os.path.split(path)[1].replace(".py", "")
spec = importlib.util.spec_from_file_location(module_name, path) spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec) 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())) print(" -%s CommandType did not match: %s for: %s" % (command_inst.get_commandType(), commandType, command_inst.get_command()))
return "", None 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 #New
def compile_and_load_new(commandType: AbstractCommand.CommandType) -> Dict[str, AbstractCommand]: def compile_and_load(commandType: AbstractCommand.CommandType) -> Dict[str, AbstractCommand]:
dic = {} dic = {}
implementations = get_implementations_dir() implementations = get_implementations_dir()
for dirName, subdirList, fileList in os.walk(implementations): for dirName, subdirList, fileList in os.walk(implementations):
for file in fileList: for file in fileList:
name = os.path.join(dirName, file) name = os.path.join(dirName, file)
print("compiling: %s" % name) 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: if command is not None and command.command_type is commandType:
dic[name] = command dic[name] = command
break break

View File

@ -17,7 +17,7 @@ def init():
def load_commands(): def load_commands():
global loadedCommands 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: def is_command(command: str) -> bool: