-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load only the module that executes the specified mepo command
- Loading branch information
1 parent
9065dd2
commit 572bd4c
Showing
1 changed file
with
6 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,10 @@ | ||
import os | ||
import glob | ||
from importlib import import_module | ||
|
||
THIS_DIR = os.path.dirname(__file__) | ||
|
||
def get_exclude_list(): | ||
patterns = ['command.py', '*~', '__pycache__'] | ||
exclude_list = list() | ||
for pat in patterns: | ||
tmp = glob.glob(os.path.join(THIS_DIR, pat)) | ||
exclude_list.extend([os.path.basename(x) for x in tmp]) | ||
return exclude_list | ||
|
||
def get_available_mepo_commands(): | ||
exclude_list = get_exclude_list() | ||
return [x for x in os.listdir(THIS_DIR) if x not in exclude_list] | ||
|
||
def run(args): | ||
# Dispatch table: d = {'list': <module 'command.list.list'>, ...} | ||
cmd_list = get_available_mepo_commands() | ||
d = {x: import_module('command.{}.{}'.format(x, x)) for x in cmd_list} | ||
mepo_cmd = args.mepo_cmd | ||
|
||
# Load the module containing the 'run' method of specified mepo command | ||
cmd_module = import_module('command.{}.{}'.format(mepo_cmd, mepo_cmd)) | ||
|
||
# Execute run method of specified mepo command, e.g. d['list'].run(args) | ||
d[args.mepo_cmd].run(args) | ||
# Execute 'run' method of the specified mepo command | ||
cmd_module.run(args) |