Skip to content

Commit

Permalink
Load only the module that executes the specified mepo command
Browse files Browse the repository at this point in the history
  • Loading branch information
pchakraborty committed Jan 9, 2020
1 parent 9065dd2 commit 572bd4c
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions mepo.d/command/command.py
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)

0 comments on commit 572bd4c

Please sign in to comment.