ext: Move away from global registry to ease testing.

Extension's setup method are now passed the active registry allowing them to
"steal" a list of the registry items they care about. In the case of commands
the registry is passed via args.registry.
This commit is contained in:
Thomas Adamcik 2013-12-23 21:46:24 +01:00
parent 353782e2c8
commit 8a94d81c42
4 changed files with 15 additions and 18 deletions

View File

@ -40,11 +40,13 @@ def main():
signal.signal(signal.SIGUSR1, pykka.debug.log_thread_tracebacks)
try:
registry = ext.Registry()
root_cmd = commands.RootCommand()
config_cmd = commands.ConfigCommand()
deps_cmd = commands.DepsCommand()
root_cmd.set(extension=None)
root_cmd.set(extension=None, registry=registry)
root_cmd.add_child('config', config_cmd)
root_cmd.add_child('deps', deps_cmd)
@ -108,7 +110,7 @@ def main():
return 1
for extension in enabled_extensions:
extension.setup()
extension.setup(registry)
# Anything that wants to exit after this point must use
# mopidy.utils.process.exit_process as actors can have been started.

View File

@ -4,7 +4,7 @@ import logging
import os
import time
from mopidy import commands, exceptions, ext
from mopidy import commands, exceptions
from mopidy.audio import scan
from mopidy.utils import path
@ -30,7 +30,7 @@ class ScanCommand(commands.Command):
file_ext.lower() for file_ext in excluded_file_extensions)
# TODO: select updater / library to use by name
updaters = ext.registry['local:library']
updaters = args.registry['local:library']
if not updaters:
logger.error('No usable library updaters found.')
return 1

View File

@ -9,7 +9,7 @@ import sys
import glib
import gobject
from mopidy import config as config_lib, ext
from mopidy import config as config_lib
from mopidy.audio import Audio
from mopidy.core import Core
from mopidy.utils import deps, process, versioning
@ -260,8 +260,8 @@ class RootCommand(Command):
def run(self, args, config):
loop = gobject.MainLoop()
backend_classes = ext.registry['backend']
frontend_classes = ext.registry['frontend']
backend_classes = args.registry['backend']
frontend_classes = args.registry['frontend']
try:
audio = self.start_audio(config)

View File

@ -62,7 +62,7 @@ class Extension(object):
"""
pass
def setup(self):
def setup(self, registry):
for backend_class in self.get_backend_classes():
registry.add('backend', backend_class)
@ -125,15 +125,16 @@ class Extension(object):
pass
class _Registry(collections.Mapping):
# TODO: document
class Registry(collections.Mapping):
def __init__(self):
self._registry = collections.defaultdict(list)
self._registry = {}
def add(self, name, cls):
self._registry[name].append(cls)
self._registry.setdefault(name, []).append(cls)
def __getitem__(self, name):
return self._registry[name]
return self._registry.setdefault(name, [])
def __iter__(self):
return iter(self._registry)
@ -142,12 +143,6 @@ class _Registry(collections.Mapping):
return len(self._registry)
# TODO: document the registry
# TODO: consider if we should avoid having this as a global and pass an
# instance from __main__ around instead?
registry = _Registry()
def load_extensions():
"""Find all installed extensions.