mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 16:42:42 +01:00
Load plugins from cli config
This commit is contained in:
parent
5f79c54cb3
commit
a363d9672f
3 changed files with 40 additions and 20 deletions
|
|
@ -668,6 +668,16 @@ class SubcommandsOptionParser(optparse.OptionParser):
|
||||||
"""
|
"""
|
||||||
options, args = optparse.OptionParser.parse_args(self, a, v)
|
options, args = optparse.OptionParser.parse_args(self, a, v)
|
||||||
|
|
||||||
|
if getattr(options, 'config', None) is not None:
|
||||||
|
config_path = options.config
|
||||||
|
del options.config
|
||||||
|
config.set_file(config_path)
|
||||||
|
config.set_args(options)
|
||||||
|
load_plugins()
|
||||||
|
|
||||||
|
for cmd in plugins.commands():
|
||||||
|
self.add_subcommand(cmd)
|
||||||
|
|
||||||
if not args:
|
if not args:
|
||||||
# No command given.
|
# No command given.
|
||||||
self.print_help()
|
self.print_help()
|
||||||
|
|
@ -735,17 +745,7 @@ def vararg_callback(option, opt_str, value, parser):
|
||||||
|
|
||||||
# The root parser and its main function.
|
# The root parser and its main function.
|
||||||
|
|
||||||
def _raw_main(args):
|
def load_plugins():
|
||||||
"""A helper function for `main` without top-level exception
|
|
||||||
handling.
|
|
||||||
"""
|
|
||||||
# Temporary: Migrate from 1.0-style configuration.
|
|
||||||
from beets.ui import migrate
|
|
||||||
migrate.automigrate()
|
|
||||||
|
|
||||||
# Get the default subcommands.
|
|
||||||
from beets.ui.commands import default_commands
|
|
||||||
|
|
||||||
# Add plugin paths.
|
# Add plugin paths.
|
||||||
import beetsplug
|
import beetsplug
|
||||||
beetsplug.__path__ = get_plugin_paths() + beetsplug.__path__
|
beetsplug.__path__ = get_plugin_paths() + beetsplug.__path__
|
||||||
|
|
@ -757,9 +757,19 @@ def _raw_main(args):
|
||||||
plugins.load_plugins(config['plugins'].as_str_seq())
|
plugins.load_plugins(config['plugins'].as_str_seq())
|
||||||
plugins.send("pluginload")
|
plugins.send("pluginload")
|
||||||
|
|
||||||
|
def _raw_main(args):
|
||||||
|
"""A helper function for `main` without top-level exception
|
||||||
|
handling.
|
||||||
|
"""
|
||||||
|
# Temporary: Migrate from 1.0-style configuration.
|
||||||
|
from beets.ui import migrate
|
||||||
|
migrate.automigrate()
|
||||||
|
|
||||||
|
# Get the default subcommands.
|
||||||
|
from beets.ui.commands import default_commands
|
||||||
|
|
||||||
# Construct the root parser.
|
# Construct the root parser.
|
||||||
commands = list(default_commands)
|
commands = list(default_commands)
|
||||||
commands += plugins.commands()
|
|
||||||
commands.append(migrate.migrate_cmd) # Temporary.
|
commands.append(migrate.migrate_cmd) # Temporary.
|
||||||
parser = SubcommandsOptionParser(subcommands=commands)
|
parser = SubcommandsOptionParser(subcommands=commands)
|
||||||
parser.add_option('-l', '--library', dest='library',
|
parser.add_option('-l', '--library', dest='library',
|
||||||
|
|
@ -773,11 +783,6 @@ def _raw_main(args):
|
||||||
|
|
||||||
# Parse the command-line!
|
# Parse the command-line!
|
||||||
options, subcommand, suboptions, subargs = parser.parse_args(args)
|
options, subcommand, suboptions, subargs = parser.parse_args(args)
|
||||||
if getattr(options, 'config', None) is not None:
|
|
||||||
config_path = options.config
|
|
||||||
del options.config
|
|
||||||
config.set_file(config_path)
|
|
||||||
config.set_args(options)
|
|
||||||
|
|
||||||
# Open library file.
|
# Open library file.
|
||||||
dbpath = config['library'].as_filename()
|
dbpath = config['library'].as_filename()
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,11 @@ from beets import ui
|
||||||
class TestPlugin(BeetsPlugin):
|
class TestPlugin(BeetsPlugin):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(TestPlugin, self).__init__()
|
super(TestPlugin, self).__init__()
|
||||||
|
self.is_test_plugin = True
|
||||||
|
|
||||||
def commands(self):
|
def commands(self):
|
||||||
cmd = ui.Subcommand('test')
|
test = ui.Subcommand('test')
|
||||||
cmd.func = lambda *args: None
|
test.func = lambda *args: None
|
||||||
return [cmd]
|
plugin = ui.Subcommand('plugin')
|
||||||
|
plugin.func = lambda *args: None
|
||||||
|
return [test, plugin]
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ from beets import importer
|
||||||
from beets.mediafile import MediaFile
|
from beets.mediafile import MediaFile
|
||||||
from beets import config
|
from beets import config
|
||||||
from beets.util import confit
|
from beets.util import confit
|
||||||
|
from beets import plugins
|
||||||
|
|
||||||
class ListTest(_common.TestCase):
|
class ListTest(_common.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
@ -597,6 +598,17 @@ class ConfigTest(_common.TestCase):
|
||||||
ui._raw_main(['--config', cli_config_path, 'test'])
|
ui._raw_main(['--config', cli_config_path, 'test'])
|
||||||
self.assertEqual(config['anoption'].get(), 'cli overwrite')
|
self.assertEqual(config['anoption'].get(), 'cli overwrite')
|
||||||
|
|
||||||
|
def test_cli_config_file_loads_plugin_commands(self):
|
||||||
|
plugin_path = os.path.join(_common.RSRC, 'beetsplug')
|
||||||
|
|
||||||
|
cli_config_path = os.path.join(self.temp_dir, 'config.yaml')
|
||||||
|
with open(cli_config_path, 'w') as file:
|
||||||
|
file.write('pluginpath: %s\n' % plugin_path)
|
||||||
|
file.write('plugins: test')
|
||||||
|
|
||||||
|
ui._raw_main(['--config', cli_config_path, 'plugin'])
|
||||||
|
self.assertTrue(plugins.find_plugins()[0].is_test_plugin)
|
||||||
|
|
||||||
|
|
||||||
class ShowdiffTest(_common.TestCase):
|
class ShowdiffTest(_common.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue