Commit 9088cd37 authored by Jacek Sowiński's avatar Jacek Sowiński

Introduce aliases functionality

Aliases can now be defined in 'aliases' section of topydo config file.

Examples:

[aliases]
showall = ls -x
purge = del -f
ical = ls -f ical
parent 6689308e
...@@ -46,3 +46,6 @@ append_parent_contexts = 0 ...@@ -46,3 +46,6 @@ append_parent_contexts = 0
; context_color = magenta ; context_color = magenta
; metadata_color = green ; metadata_color = green
; link_color = light-cyan ; link_color = light-cyan
[aliases]
;showall = ls -x
...@@ -53,7 +53,6 @@ _SUBCOMMAND_MAP = { ...@@ -53,7 +53,6 @@ _SUBCOMMAND_MAP = {
'tag': 'TagCommand', 'tag': 'TagCommand',
} }
def get_subcommand(p_args): def get_subcommand(p_args):
""" """
Retrieves the to-be executed Command and returns a tuple (Command, args). Retrieves the to-be executed Command and returns a tuple (Command, args).
...@@ -82,11 +81,19 @@ def get_subcommand(p_args): ...@@ -82,11 +81,19 @@ def get_subcommand(p_args):
result = None result = None
args = p_args args = p_args
alias_map = config().aliases()
try: try:
subcommand = p_args[0] subcommand = p_args[0]
if subcommand in _SUBCOMMAND_MAP: if subcommand in alias_map:
real_subcommand, alias_args = alias_map[subcommand]
try:
result = import_subcommand(real_subcommand)
args = alias_args + args[1:]
except ImportError:
pass
elif subcommand in _SUBCOMMAND_MAP:
result = import_subcommand(subcommand) result = import_subcommand(subcommand)
args = args[1:] args = args[1:]
elif subcommand == 'help': elif subcommand == 'help':
......
...@@ -42,6 +42,7 @@ class _Config: ...@@ -42,6 +42,7 @@ class _Config:
""" """
self.sections = [ self.sections = [
'add', 'add',
'aliases',
'colorscheme', 'colorscheme',
'dep', 'dep',
'ls', 'ls',
...@@ -279,6 +280,22 @@ class _Config: ...@@ -279,6 +280,22 @@ class _Config:
except ValueError: except ValueError:
return self.defaults['add']['auto_creation_date'] == '1' return self.defaults['add']['auto_creation_date'] == '1'
def aliases(self):
"""
Returns dict with aliases names as keys and pairs of actual
subcommand and alias args as values.
"""
aliases = self.cp.items('aliases')
alias_dict = dict()
for alias, meaning in aliases:
meaning = meaning.split()
real_subcommand = meaning[0]
alias_args = meaning[1:]
alias_dict[alias] = (real_subcommand, alias_args)
return alias_dict
def config(p_path=None, p_overrides=None): def config(p_path=None, p_overrides=None):
""" """
Retrieve the config instance. Retrieve the config instance.
......
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