Skip to content

Commit

Permalink
Improve hook plugin design and configuration
Browse files Browse the repository at this point in the history
 - Remove `shell` option and split all commands using `shlex.split`
   before passing them to `subprocess.Popen`.
 - General refactor of hook plugin code - move hook creation function
   inside `HookPlugin`.
 - Add improved error handling for invalid (i.e. empty) commands or
   commands that do not exist.
  • Loading branch information
jackwilsdon committed Apr 18, 2016
1 parent dd949a9 commit 8b4f349
Showing 1 changed file with 25 additions and 40 deletions.
65 changes: 25 additions & 40 deletions beetsplug/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,13 @@
from __future__ import (division, absolute_import, print_function,
unicode_literals)

import shlex
import subprocess
import sys

from beets.ui import _arg_encoding
from beets.plugins import BeetsPlugin


def create_hook_function(log, event, command, shell, substitute_args):
def hook_function(**kwargs):
hook_command = command

for key in substitute_args:
if key in kwargs:
hook_command = hook_command.replace(substitute_args[key],
unicode(kwargs[key],
_arg_encoding()))

log.debug('Running command {0} for event {1}', hook_command, event)

subprocess.Popen(hook_command, shell=shell).wait()

return hook_function
from beets.ui import _arg_encoding
from beets.util.confit import ConfigValueError


class HookPlugin(BeetsPlugin):
Expand All @@ -45,39 +31,38 @@ def __init__(self):
super(HookPlugin, self).__init__()

self.config.add({
'hooks': [],
'substitute_event': '%EVENT%',
'shell': True
'hooks': []
})

hooks = self.config['hooks'].get(list)
global_substitute_event = self.config['substitute_event'].get()
global_shell = self.config['shell'].get(bool)

for hook_index in range(len(hooks)):
hook = self.config['hooks'][hook_index]

hook_event = hook['event'].get()
hook_command = hook['command'].get()

if 'substitute_event' in hook:
original = hook['substitute_event'].get()
else:
original = global_substitute_event
self.create_and_register_hook(hook_event, hook_command)

def create_and_register_hook(self, event, command):
def hook_function(**kwargs):
formatted_command = command.format(event=event, **kwargs)
encoded_command = formatted_command.decode(_arg_encoding())
command_pieces = shlex.split(encoded_command)

if len(command_pieces) == 0:
raise ConfigValueError('invalid command \"{0}\"'.format(
command))

if 'shell' in hook:
shell = hook['shell'].get(bool)
else:
shell = global_shell
self._log.debug('Running command \"{0}\" for event \"{1}\"',
encoded_command, event)

if 'substitute_args' in hook:
substitute_args = hook['substitute_args'].get(dict)
else:
substitute_args = {}
try:
subprocess.Popen(command_pieces).wait()
except OSError as e:
_, _, trace = sys.exc_info()
message = "{0}: {1}".format(e, command_pieces[0])

hook_command = hook_command.replace(original, hook_event)
hook_function = create_hook_function(self._log, hook_event,
hook_command, shell,
substitute_args)
raise OSError, message, trace

self.register_listener(hook_event, hook_function)
self.register_listener(event, hook_function)

0 comments on commit 8b4f349

Please sign in to comment.