Commit a0a90919 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Speed up topydo a bit by only loading the necessary subcommand module.

topydo was a tad slow, because all modules of all subcommands were
loaded with each invocation of topydo. Particularly, the ical subcommand
was quite slow because it loads modules from the icalendar package.
parent 5bcf70e6
...@@ -19,51 +19,36 @@ This module is aware of all supported submodules and hands out a Command ...@@ -19,51 +19,36 @@ This module is aware of all supported submodules and hands out a Command
instance based on an argument list. instance based on an argument list.
""" """
from topydo.lib.Config import config import sys
from topydo.commands.AddCommand import AddCommand from topydo.lib.Config import config
from topydo.commands.AppendCommand import AppendCommand
from topydo.commands.DeleteCommand import DeleteCommand
from topydo.commands.DepCommand import DepCommand
from topydo.commands.DepriCommand import DepriCommand
from topydo.commands.DoCommand import DoCommand
from topydo.commands.EditCommand import EditCommand
from topydo.commands.ExitCommand import ExitCommand
from topydo.commands.IcalCommand import IcalCommand
from topydo.commands.ListCommand import ListCommand
from topydo.commands.ListContextCommand import ListContextCommand
from topydo.commands.ListProjectCommand import ListProjectCommand
from topydo.commands.PostponeCommand import PostponeCommand
from topydo.commands.PriorityCommand import PriorityCommand
from topydo.commands.SortCommand import SortCommand
from topydo.commands.TagCommand import TagCommand
_SUBCOMMAND_MAP = { _SUBCOMMAND_MAP = {
'add': AddCommand, 'add': 'AddCommand',
'app': AppendCommand, 'app': 'AppendCommand',
'append': AppendCommand, 'append': 'AppendCommand',
'del': DeleteCommand, 'del': 'DeleteCommand',
'dep': DepCommand, 'dep': 'DepCommand',
'depri': DepriCommand, 'depri': 'DepriCommand',
'do': DoCommand, 'do': 'DoCommand',
'edit': EditCommand, 'edit': 'EditCommand',
'exit': ExitCommand, # used for the prompt 'exit': 'ExitCommand', # used for the prompt
'ical': IcalCommand, 'ical': 'IcalCommand',
'ls': ListCommand, 'ls': 'ListCommand',
'lscon': ListContextCommand, 'lscon': 'ListContextCommand',
'listcon': ListContextCommand, 'listcon': 'ListContextCommand',
'lsprj': ListProjectCommand, 'lsprj': 'ListProjectCommand',
'lsproj': ListProjectCommand, 'lsproj': 'ListProjectCommand',
'listprj': ListProjectCommand, 'listprj': 'ListProjectCommand',
'listproj': ListProjectCommand, 'listproj': 'ListProjectCommand',
'listproject': ListProjectCommand, 'listproject': 'ListProjectCommand',
'listprojects': ListProjectCommand, 'listprojects': 'ListProjectCommand',
'postpone': PostponeCommand, 'postpone': 'PostponeCommand',
'pri': PriorityCommand, 'pri': 'PriorityCommand',
'quit': ExitCommand, 'quit': 'ExitCommand',
'rm': DeleteCommand, 'rm': 'DeleteCommand',
'sort': SortCommand, 'sort': 'SortCommand',
'tag': TagCommand, 'tag': 'TagCommand',
} }
def get_subcommand(p_args): def get_subcommand(p_args):
...@@ -81,6 +66,18 @@ def get_subcommand(p_args): ...@@ -81,6 +66,18 @@ def get_subcommand(p_args):
If no valid command could be found, the subcommand part of the tuple If no valid command could be found, the subcommand part of the tuple
is None. is None.
""" """
def import_subcommand(p_subcommand):
"""
Returns the class of the requested subcommand. An invalid p_subcommand
will result in an ImportError, since this is a programming mistake
(most likely an error in the _SUBCOMMAND_MAP).
"""
classname = _SUBCOMMAND_MAP[p_subcommand]
modulename = 'topydo.commands.{}'.format(classname)
__import__(modulename, globals(), locals(), [classname], -1)
return getattr(sys.modules[modulename], classname)
result = None result = None
args = p_args args = p_args
...@@ -88,7 +85,7 @@ def get_subcommand(p_args): ...@@ -88,7 +85,7 @@ def get_subcommand(p_args):
subcommand = p_args[0] subcommand = p_args[0]
if subcommand in _SUBCOMMAND_MAP: if subcommand in _SUBCOMMAND_MAP:
result = _SUBCOMMAND_MAP[subcommand] result = import_subcommand(subcommand)
args = args[1:] args = args[1:]
elif subcommand == 'help': elif subcommand == 'help':
try: try:
...@@ -103,12 +100,12 @@ def get_subcommand(p_args): ...@@ -103,12 +100,12 @@ def get_subcommand(p_args):
else: else:
p_command = config().default_command() p_command = config().default_command()
if p_command in _SUBCOMMAND_MAP: if p_command in _SUBCOMMAND_MAP:
result = _SUBCOMMAND_MAP[p_command] result = import_subcommand(p_command)
# leave args unchanged # leave args unchanged
except IndexError: except IndexError:
p_command = config().default_command() p_command = config().default_command()
if p_command in _SUBCOMMAND_MAP: if p_command in _SUBCOMMAND_MAP:
result = _SUBCOMMAND_MAP[p_command] result = import_subcommand(p_command)
return (result, args) return (result, args)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment