prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  hook: Crash when using non-ASCII paths in command template
  Using the config
  
  ```
  hook:
    hooks:
      - event: album_imported
        command: beet convert -ay path:{album.path}
  ```
  
  and this command
  
  ```
  beet import -a -L path:/path/to/lib/Ария
  ```
  
  I get an UnicodeDecodeError from `hook.py` line 50.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `beetsplug/hook.py`
  Content:
  ```
  1 # This file is part of beets.
  2 # Copyright 2015, Adrian Sampson.
  3 #
  4 # Permission is hereby granted, free of charge, to any person obtaining
  5 # a copy of this software and associated documentation files (the
  6 # "Software"), to deal in the Software without restriction, including
  7 # without limitation the rights to use, copy, modify, merge, publish,
  8 # distribute, sublicense, and/or sell copies of the Software, and to
  9 # permit persons to whom the Software is furnished to do so, subject to
  10 # the following conditions:
  11 #
  12 # The above copyright notice and this permission notice shall be
  13 # included in all copies or substantial portions of the Software.
  14 
  15 """Allows custom commands to be run when an event is emitted by beets"""
  16 from __future__ import division, absolute_import, print_function
  17 
  18 import shlex
  19 import subprocess
  20 
  21 from beets.plugins import BeetsPlugin
  22 from beets.ui import _arg_encoding
  23 
  24 
  25 class HookPlugin(BeetsPlugin):
  26     """Allows custom commands to be run when an event is emitted by beets"""
  27     def __init__(self):
  28         super(HookPlugin, self).__init__()
  29 
  30         self.config.add({
  31             'hooks': []
  32         })
  33 
  34         hooks = self.config['hooks'].get(list)
  35 
  36         for hook_index in range(len(hooks)):
  37             hook = self.config['hooks'][hook_index]
  38 
  39             hook_event = hook['event'].get()
  40             hook_command = hook['command'].get()
  41 
  42             self.create_and_register_hook(hook_event, hook_command)
  43 
  44     def create_and_register_hook(self, event, command):
  45         def hook_function(**kwargs):
  46                 if command is None or len(command) == 0:
  47                     self._log.error('invalid command "{0}"', command)
  48                     return
  49 
  50                 formatted_command = command.format(event=event, **kwargs)
  51                 encoded_command = formatted_command.decode(_arg_encoding())
  52                 command_pieces = shlex.split(encoded_command)
  53 
  54                 self._log.debug('Running command "{0}" for event {1}',
  55                                 encoded_command, event)
  56 
  57                 try:
  58                     subprocess.Popen(command_pieces).wait()
  59                 except OSError as exc:
  60                     self._log.error('hook for {0} failed: {1}', event, exc)
  61 
  62         self.register_listener(event, hook_function)
  63
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/beetsplug/hook.py b/beetsplug/hook.py
  --- a/beetsplug/hook.py
  +++ b/beetsplug/hook.py
  @@ -47,7 +47,9 @@
                       self._log.error('invalid command "{0}"', command)
                       return
   
  -                formatted_command = command.format(event=event, **kwargs)
  +                unicode_command = command.decode('utf-8')
  +                formatted_command = unicode_command.format(event=event,
  +                                                           **kwargs)
                   encoded_command = formatted_command.decode(_arg_encoding())
                   command_pieces = shlex.split(encoded_command)
  
verification_info: {"golden_diff": "diff --git a/beetsplug/hook.py b/beetsplug/hook.py\n--- a/beetsplug/hook.py\n+++ b/beetsplug/hook.py\n@@ -47,7 +47,9 @@\n                     self._log.error('invalid command \"{0}\"', command)\n                     return\n \n-                formatted_command = command.format(event=event, **kwargs)\n+                unicode_command = command.decode('utf-8')\n+                formatted_command = unicode_command.format(event=event,\n+                                                           **kwargs)\n                 encoded_command = formatted_command.decode(_arg_encoding())\n                 command_pieces = shlex.split(encoded_command)\n", "issue": "hook: Crash when using non-ASCII paths in command template\nUsing the config\n\n```\nhook:\n  hooks:\n    - event: album_imported\n      command: beet convert -ay path:{album.path}\n```\n\nand this command\n\n```\nbeet import -a -L path:/path/to/lib/\u0410\u0440\u0438\u044f\n```\n\nI get an UnicodeDecodeError from `hook.py` line 50.\n\n", "before_files": [{"content": "# This file is part of beets.\n# Copyright 2015, Adrian Sampson.\n#\n# Permission is hereby granted, free of charge, to any person obtaining\n# a copy of this software and associated documentation files (the\n# \"Software\"), to deal in the Software without restriction, including\n# without limitation the rights to use, copy, modify, merge, publish,\n# distribute, sublicense, and/or sell copies of the Software, and to\n# permit persons to whom the Software is furnished to do so, subject to\n# the following conditions:\n#\n# The above copyright notice and this permission notice shall be\n# included in all copies or substantial portions of the Software.\n\n\"\"\"Allows custom commands to be run when an event is emitted by beets\"\"\"\nfrom __future__ import division, absolute_import, print_function\n\nimport shlex\nimport subprocess\n\nfrom beets.plugins import BeetsPlugin\nfrom beets.ui import _arg_encoding\n\n\nclass HookPlugin(BeetsPlugin):\n    \"\"\"Allows custom commands to be run when an event is emitted by beets\"\"\"\n    def __init__(self):\n        super(HookPlugin, self).__init__()\n\n        self.config.add({\n            'hooks': []\n        })\n\n        hooks = self.config['hooks'].get(list)\n\n        for hook_index in range(len(hooks)):\n            hook = self.config['hooks'][hook_index]\n\n            hook_event = hook['event'].get()\n            hook_command = hook['command'].get()\n\n            self.create_and_register_hook(hook_event, hook_command)\n\n    def create_and_register_hook(self, event, command):\n        def hook_function(**kwargs):\n                if command is None or len(command) == 0:\n                    self._log.error('invalid command \"{0}\"', command)\n                    return\n\n                formatted_command = command.format(event=event, **kwargs)\n                encoded_command = formatted_command.decode(_arg_encoding())\n                command_pieces = shlex.split(encoded_command)\n\n                self._log.debug('Running command \"{0}\" for event {1}',\n                                encoded_command, event)\n\n                try:\n                    subprocess.Popen(command_pieces).wait()\n                except OSError as exc:\n                    self._log.error('hook for {0} failed: {1}', event, exc)\n\n        self.register_listener(event, hook_function)\n", "path": "beetsplug/hook.py"}], "after_files": [{"content": "# This file is part of beets.\n# Copyright 2015, Adrian Sampson.\n#\n# Permission is hereby granted, free of charge, to any person obtaining\n# a copy of this software and associated documentation files (the\n# \"Software\"), to deal in the Software without restriction, including\n# without limitation the rights to use, copy, modify, merge, publish,\n# distribute, sublicense, and/or sell copies of the Software, and to\n# permit persons to whom the Software is furnished to do so, subject to\n# the following conditions:\n#\n# The above copyright notice and this permission notice shall be\n# included in all copies or substantial portions of the Software.\n\n\"\"\"Allows custom commands to be run when an event is emitted by beets\"\"\"\nfrom __future__ import division, absolute_import, print_function\n\nimport shlex\nimport subprocess\n\nfrom beets.plugins import BeetsPlugin\nfrom beets.ui import _arg_encoding\n\n\nclass HookPlugin(BeetsPlugin):\n    \"\"\"Allows custom commands to be run when an event is emitted by beets\"\"\"\n    def __init__(self):\n        super(HookPlugin, self).__init__()\n\n        self.config.add({\n            'hooks': []\n        })\n\n        hooks = self.config['hooks'].get(list)\n\n        for hook_index in range(len(hooks)):\n            hook = self.config['hooks'][hook_index]\n\n            hook_event = hook['event'].get()\n            hook_command = hook['command'].get()\n\n            self.create_and_register_hook(hook_event, hook_command)\n\n    def create_and_register_hook(self, event, command):\n        def hook_function(**kwargs):\n                if command is None or len(command) == 0:\n                    self._log.error('invalid command \"{0}\"', command)\n                    return\n\n                unicode_command = command.decode('utf-8')\n                formatted_command = unicode_command.format(event=event,\n                                                           **kwargs)\n                encoded_command = formatted_command.decode(_arg_encoding())\n                command_pieces = shlex.split(encoded_command)\n\n                self._log.debug('Running command \"{0}\" for event {1}',\n                                encoded_command, event)\n\n                try:\n                    subprocess.Popen(command_pieces).wait()\n                except OSError as exc:\n                    self._log.error('hook for {0} failed: {1}', event, exc)\n\n        self.register_listener(event, hook_function)\n", "path": "beetsplug/hook.py"}]}