From: Elrond Date: Fri, 8 Mar 2013 13:37:33 +0000 (+0100) Subject: First step towards a MediaManager class: Compat one. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2077d6ed933773e0b44a7d1efa603453ea196fe3;p=mediagoblin.git First step towards a MediaManager class: Compat one. To get us moving towards a MediaManager class, the first idea is to create a class that wraps our current dict based manager and makes all users happy. --- diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index 0dc3bc85..db08dfd2 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -212,7 +212,7 @@ class MediaEntryMixin(GenerateSlugMixin): # than iterating through all media managers. for media_type, manager in get_media_managers(): if media_type == self.media_type: - return manager + return manager(self) # Not found? Then raise an error raise FileTypeNotSupported( "MediaManager not in enabled types. Check media_types in config?") diff --git a/mediagoblin/media_types/__init__.py b/mediagoblin/media_types/__init__.py index 0abb38d3..745e05ef 100644 --- a/mediagoblin/media_types/__init__.py +++ b/mediagoblin/media_types/__init__.py @@ -20,6 +20,7 @@ import logging import tempfile from mediagoblin import mg_globals +from mediagoblin.tools.common import import_component from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ _log = logging.getLogger(__name__) @@ -31,6 +32,29 @@ class InvalidFileType(Exception): pass +class CompatMediaManager(object): + def __init__(self, mm_dict, entry=None): + self.mm_dict = mm_dict + self.entry = entry + + def __call__(self, entry): + "So this object can look like a class too, somehow" + assert self.entry is None + return self.__class__(self.mm_dict, entry) + + def __getitem__(self, i): + return self.mm_dict[i] + + def __contains__(self, i): + return (i in self.mm_dict) + + def get(self, *args, **kwargs): + return self.mm_dict.get(*args, **kwargs) + + def __getattr__(self, i): + return self.mm_dict[i] + + def sniff_media(media): ''' Iterate through the enabled media types and find those suited @@ -74,9 +98,12 @@ def get_media_managers(): Generator, yields all enabled media managers ''' for media_type in get_media_types(): - __import__(media_type) + mm = import_component(media_type + ":MEDIA_MANAGER") + + if isinstance(mm, dict): + mm = CompatMediaManager(mm) - yield media_type, sys.modules[media_type].MEDIA_MANAGER + yield media_type, mm def get_media_type_and_manager(filename): diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index d9c7ca46..0d74848a 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -231,7 +231,8 @@ class TestSubmission: media = self.check_media(request, {'title': u'Balanced Goblin'}, 1) assert media.media_type == u'mediagoblin.media_types.image' - assert media.media_manager == img_MEDIA_MANAGER + assert media.media_manager.mm_dict == img_MEDIA_MANAGER + assert media.media_manager.entry == media def test_sniffing(self, test_app):