begin adding convenient plugin defaults

We need plugins to set their config values at run time instead of module import
time. That is, defaults should be put in the __init__ method. This is easy
enough, but to make it even more convenient, I added a BeetsPlugin.config
field, which is a Confit view into a subsection of the configuration named
after the plugin.
This commit is contained in:
Adrian Sampson 2012-12-18 20:42:42 -08:00
parent 2c45251db9
commit 4a5594bec6
4 changed files with 28 additions and 26 deletions

View file

@ -1,5 +1,5 @@
# This file is part of beets.
# Copyright 2011, Adrian Sampson.
# Copyright 2012, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@ -15,10 +15,10 @@
"""Support for beets plugins."""
import logging
import itertools
import traceback
from collections import defaultdict
import beets
from beets import mediafile
PLUGIN_NAMESPACE = 'beetsplug'
@ -37,12 +37,13 @@ class BeetsPlugin(object):
functionality by defining a subclass of BeetsPlugin and overriding
the abstract methods defined here.
"""
def __init__(self):
"""Perform one-time plugin setup. There is probably no reason to
override this method.
def __init__(self, name=None):
"""Perform one-time plugin setup.
"""
_add_media_fields(self.item_fields())
self.import_stages = []
self.name = name or self.__module__.split('.')[-1]
self.config = beets.config[self.name]
def commands(self):
"""Should return a list of beets.ui.Subcommand objects for

View file

@ -935,10 +935,7 @@ default_commands.append(stats_cmd)
def show_version(lib, opts, args):
print_('beets version %s' % beets.__version__)
# Show plugins.
names = []
for plugin in plugins.find_plugins():
modname = plugin.__module__
names.append(modname.split('.')[-1])
names = [p.name for p in plugins.find_plugins()]
if names:
print_('plugins:', ', '.join(names))
else:

View file

@ -139,6 +139,13 @@ class ConfigView(object):
return value
def add(self, value):
"""Add a value as a source for configuration data. The object as
added as the lowest-priority source. This can be used to
dynamically extend the defaults.
"""
raise NotImplementedError
def set(self, value):
"""Create an overlay source to set the value at this view.
"""
@ -324,12 +331,6 @@ class RootView(ConfigView):
self.name = ROOT_NAME
def add(self, obj):
"""Add the object (probably a dict) as a source for
configuration data. The object as added as the lowest-priority
source. This can be used to dynamically extend the defaults
(i.e., when loading a plugin that shares the main application's
config file).
"""
self.sources.append(obj)
def set(self, value):
@ -382,6 +383,9 @@ class Subview(ConfigView):
def set(self, value):
self.parent.set({self.key: value})
def add(self, value):
self.parent.add({self.key: value})
# Config file paths, including platform-specific paths and in-package
# defaults.

View file

@ -32,14 +32,6 @@ from beets import vfs
from beets import config
from beets.util import bluelet
config.add({
'bpd': {
'host': u'',
'port': 6600,
'password': u'',
}
})
PROTOCOL_VERSION = '0.13.0'
BUFSIZE = 1024
@ -1129,6 +1121,14 @@ class BPDPlugin(BeetsPlugin):
"""Provides the "beet bpd" command for running a music player
server.
"""
def __init__(self):
super(BPDPlugin, self).__init__()
self.config.add({
'host': u'',
'port': 6600,
'password': u'',
})
def start_bpd(self, lib, host, port, password, debug):
"""Starts a BPD server."""
if debug:
@ -1149,11 +1149,11 @@ class BPDPlugin(BeetsPlugin):
help='dump all MPD traffic to stdout')
def func(lib, opts, args):
host = args.pop(0) if args else config['bpd']['host'].get(unicode)
port = args.pop(0) if args else config['bpd']['port'].get(int)
host = args.pop(0) if args else self.config['host'].get(unicode)
port = args.pop(0) if args else self.config['port'].get(int)
if args:
raise beets.ui.UserError('too many arguments')
password = config['bpd']['password'].get(unicode)
password = self.config['password'].get(unicode)
debug = opts.debug or False
self.start_bpd(lib, host, int(port), password, debug)