# 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?")
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__)
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
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):
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):