2012-05-19 19:56:13 +00:00
|
|
|
|
"""
|
2013-04-13 20:33:06 +00:00
|
|
|
|
Usage
|
|
|
|
|
-----
|
|
|
|
|
|
|
|
|
|
This plugin defines two new global commands: :term:`/alias` and :term:`/unalias`.
|
|
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
|
|
/alias
|
|
|
|
|
**Usage:** ``/alias <name> <command> [args]``
|
|
|
|
|
|
2014-03-27 22:10:07 +00:00
|
|
|
|
This command will create a new command, named ``<name>`` (and callable
|
|
|
|
|
with ``/name``), that runs ``/command``, with ``[args]`` as fixed
|
|
|
|
|
args for the command.
|
|
|
|
|
When you run the alias, you can also pass parameters to it, that will be
|
|
|
|
|
given to the original command.
|
2013-04-13 20:33:06 +00:00
|
|
|
|
|
|
|
|
|
Example: ::
|
|
|
|
|
|
2014-03-27 22:10:07 +00:00
|
|
|
|
/alias toto say koin
|
2013-04-13 20:33:06 +00:00
|
|
|
|
|
2014-03-27 22:10:07 +00:00
|
|
|
|
Will bind ``/say koin`` to ``/toto``, so this alias will work in any
|
2013-04-13 20:33:06 +00:00
|
|
|
|
Chat tab. If someone calls it with ::
|
|
|
|
|
|
|
|
|
|
/toto koin
|
|
|
|
|
|
|
|
|
|
Poezio will then execute ``/say koin koin``.
|
|
|
|
|
|
2014-03-27 22:10:07 +00:00
|
|
|
|
Also, you can rebind arguments arbitrarily, with the ``{}`` placeholder.
|
|
|
|
|
For example, ::
|
|
|
|
|
|
|
|
|
|
/alias toto say {} le {}
|
|
|
|
|
/toto loulou coucou
|
|
|
|
|
|
|
|
|
|
Will execute ``/say loulou le coucou``, because the ``{}`` are
|
|
|
|
|
replaced with the command args, in the order they are given.
|
|
|
|
|
|
|
|
|
|
Extra args are still added at the end of the command if provided
|
|
|
|
|
(args used for the formatting are only used for the formatting).
|
|
|
|
|
|
2013-04-13 20:33:06 +00:00
|
|
|
|
/unalias
|
|
|
|
|
**Usage:** ``/unalias <name>``
|
|
|
|
|
|
|
|
|
|
This command removes a defined alias.
|
2012-05-19 19:56:13 +00:00
|
|
|
|
|
2014-03-27 22:10:07 +00:00
|
|
|
|
|
|
|
|
|
Config
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
The aliases are stored inside the configuration file for the plugin.
|
|
|
|
|
You can either use the above commands or write it manually, and it
|
|
|
|
|
will be read when the plugin is loaded.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Example of the syntax:
|
|
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
|
|
[alias]
|
|
|
|
|
toto = say {} le {}
|
|
|
|
|
j = join {}@conference.jabber.org/nick
|
|
|
|
|
jp = say je proteste
|
|
|
|
|
|
|
|
|
|
|
2012-05-19 19:56:13 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2014-03-27 22:10:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-02 11:22:13 +00:00
|
|
|
|
from plugin import BasePlugin
|
2013-03-08 18:39:34 +00:00
|
|
|
|
from common import shell_split
|
2014-04-03 22:24:16 +00:00
|
|
|
|
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
|
def init(self):
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.add_command('alias', self.command_alias,
|
2013-03-01 18:25:31 +00:00
|
|
|
|
usage='<alias> <command> [args]',
|
|
|
|
|
short='Create an alias command',
|
|
|
|
|
help='Create an alias for <command> with [args].')
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.add_command('unalias', self.command_unalias,
|
2013-03-01 18:25:31 +00:00
|
|
|
|
usage='<alias>',
|
|
|
|
|
help='Remove a previously created alias',
|
|
|
|
|
short='Remove an alias',
|
|
|
|
|
completion=self.completion_unalias)
|
2011-10-02 11:22:13 +00:00
|
|
|
|
self.commands = {}
|
2014-03-27 22:10:07 +00:00
|
|
|
|
self.load_conf()
|
|
|
|
|
|
|
|
|
|
def load_conf(self):
|
|
|
|
|
"""
|
|
|
|
|
load stored aliases on startup
|
|
|
|
|
"""
|
|
|
|
|
for alias in self.config.options():
|
|
|
|
|
full = self.config.get(alias, '')
|
|
|
|
|
self.command_alias(alias + ' ' + full)
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
|
|
|
|
def command_alias(self, line):
|
2012-05-19 19:56:13 +00:00
|
|
|
|
"""
|
|
|
|
|
/alias <alias> <command> [args]
|
|
|
|
|
"""
|
2014-03-27 22:10:07 +00:00
|
|
|
|
arg = split_args(line)
|
|
|
|
|
if not arg:
|
2013-03-08 18:39:34 +00:00
|
|
|
|
self.api.information('Alias: Not enough parameters', 'Error')
|
2011-10-02 11:22:13 +00:00
|
|
|
|
return
|
2014-03-27 22:10:07 +00:00
|
|
|
|
alias, command, args = arg
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
2014-03-27 22:10:07 +00:00
|
|
|
|
if alias in self.commands:
|
|
|
|
|
update = True
|
|
|
|
|
elif alias in self.core.commands:
|
2013-03-08 18:39:34 +00:00
|
|
|
|
self.api.information('Alias: command already exists', 'Error')
|
2011-10-02 11:22:13 +00:00
|
|
|
|
return
|
2014-03-27 22:10:07 +00:00
|
|
|
|
else:
|
|
|
|
|
update = False
|
|
|
|
|
|
|
|
|
|
self.config.set(alias, command + ' ' + args)
|
|
|
|
|
self.commands[alias] = command_wrapper(generic_command, lambda: self.get_command(command), args)
|
|
|
|
|
self.api.del_command(alias)
|
|
|
|
|
self.api.add_command(alias, self.commands[alias], 'This command is an alias for /%s %s' %(alias, command))
|
2013-03-08 21:53:35 +00:00
|
|
|
|
|
2014-03-27 22:10:07 +00:00
|
|
|
|
if update:
|
|
|
|
|
self.api.information('Alias /%s updated' % alias, 'Info')
|
|
|
|
|
else:
|
|
|
|
|
self.api.information('Alias /%s successfuly created' % alias, 'Info')
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
|
|
|
|
def command_unalias(self, alias):
|
2012-05-19 19:56:13 +00:00
|
|
|
|
"""
|
|
|
|
|
/unalias <existing alias>
|
|
|
|
|
"""
|
2011-10-02 11:22:13 +00:00
|
|
|
|
if alias in self.commands:
|
|
|
|
|
del self.commands[alias]
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.del_command(alias)
|
2013-03-08 18:39:34 +00:00
|
|
|
|
self.api.information('Alias /%s successfuly deleted' % alias, 'Info')
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
2013-03-01 18:25:31 +00:00
|
|
|
|
def completion_unalias(self, the_input):
|
2014-03-27 22:10:07 +00:00
|
|
|
|
"Completion for /unalias"
|
2013-03-01 18:25:31 +00:00
|
|
|
|
aliases = [alias for alias in self.commands]
|
|
|
|
|
aliases.sort()
|
|
|
|
|
return the_input.auto_completion(aliases, '', quotify=False)
|
|
|
|
|
|
2011-10-02 11:22:13 +00:00
|
|
|
|
def get_command(self, name):
|
|
|
|
|
"""Returns the function associated with a command"""
|
|
|
|
|
def dummy(args):
|
|
|
|
|
"""Dummy function called if the command doesn’t exist"""
|
|
|
|
|
pass
|
|
|
|
|
if name in self.core.commands:
|
|
|
|
|
return self.core.commands[name][0]
|
2013-03-08 18:39:34 +00:00
|
|
|
|
elif name in self.api.current_tab().commands:
|
|
|
|
|
return self.api.current_tab().commands[name][0]
|
2011-10-02 11:22:13 +00:00
|
|
|
|
return dummy
|
2014-03-27 22:10:07 +00:00
|
|
|
|
|
|
|
|
|
def split_args(line):
|
|
|
|
|
"""
|
|
|
|
|
Extract the relevant vars from the command line
|
|
|
|
|
"""
|
|
|
|
|
arg = line.split()
|
|
|
|
|
if len(arg) < 2:
|
|
|
|
|
return None
|
|
|
|
|
alias_pos = line.find(' ')
|
|
|
|
|
alias = line[:alias_pos]
|
|
|
|
|
end = line[alias_pos+1:]
|
|
|
|
|
args_pos = end.find(' ')
|
|
|
|
|
command = end[:args_pos]
|
|
|
|
|
args = end[args_pos+1:]
|
|
|
|
|
return (alias, command, args)
|
|
|
|
|
|
|
|
|
|
def generic_command(command, extra_args, args):
|
|
|
|
|
"""
|
|
|
|
|
Function that will execute the command and set the relevant
|
|
|
|
|
parameters (format string, etc).
|
|
|
|
|
"""
|
|
|
|
|
args = shell_split(args)
|
|
|
|
|
new_extra_args = extra_args.format(*args)
|
|
|
|
|
count = extra_args.count('{}')
|
|
|
|
|
args = args[count:]
|
|
|
|
|
new_extra_args += ' '.join(args)
|
|
|
|
|
return command()(new_extra_args)
|
|
|
|
|
|
|
|
|
|
def command_wrapper(func, command, extra_args):
|
|
|
|
|
"set the predefined arguments"
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
return func(command, extra_args, *args, **kwargs)
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|