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.
This commit is contained in:
Elrond 2013-03-08 14:37:33 +01:00
parent 827f91e603
commit 2077d6ed93
3 changed files with 32 additions and 4 deletions

View File

@ -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?")

View File

@ -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")
yield media_type, sys.modules[media_type].MEDIA_MANAGER
if isinstance(mm, dict):
mm = CompatMediaManager(mm)
yield media_type, mm
def get_media_type_and_manager(filename):

View File

@ -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):