Rename get_all_posts_of_a_blog function.
[mediagoblin.git] / mediagoblin / media_types / __init__.py
index 745e05ef5a5590156c8996487e92a45b0331e0e7..134157dcb0ca7054fdf5043bef82548137ef7373 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import os
-import sys
 import logging
 import tempfile
 
-from mediagoblin import mg_globals
-from mediagoblin.tools.common import import_component
+from mediagoblin.tools.pluginapi import hook_handle
 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 
 _log = logging.getLogger(__name__)
@@ -32,27 +30,24 @@ class InvalidFileType(Exception):
     pass
 
 
-class CompatMediaManager(object):
-    def __init__(self, mm_dict, entry=None):
-        self.mm_dict = mm_dict
+class MediaManagerBase(object):
+    "Base class for all media managers"
+
+    # Please override in actual media managers
+    media_fetch_order = None
+
+    @staticmethod
+    def sniff_handler(*args, **kwargs):
+        return False
+
+    def __init__(self, entry):
         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]
+        return getattr(self, 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]
+        return hasattr(self, i)
 
 
 def sniff_media(media):
@@ -71,41 +66,18 @@ def sniff_media(media):
         media_file.write(media.stream.read())
         media.stream.seek(0)
 
-        for media_type, manager in get_media_managers():
-            _log.info('Sniffing {0}'.format(media_type))
-            if 'sniff_handler' in manager and \
-               manager['sniff_handler'](media_file, media=media):
-                _log.info('{0} accepts the file'.format(media_type))
-                return media_type, manager
-            else:
-                _log.debug('{0} did not accept the file'.format(media_type))
+        media_type = hook_handle('sniff_handler', media_file, media=media)
+        if media_type:
+            _log.info('{0} accepts the file'.format(media_type))
+            return media_type, hook_handle(('media_manager', media_type))
+        else:
+            _log.debug('{0} did not accept the file'.format(media_type))
 
     raise FileTypeNotSupported(
         # TODO: Provide information on which file types are supported
         _(u'Sorry, I don\'t support that file type :('))
 
 
-def get_media_types():
-    """
-    Generator, yields the available media types
-    """
-    for media_type in mg_globals.app_config['media_types']:
-        yield media_type
-
-
-def get_media_managers():
-    '''
-    Generator, yields all enabled media managers
-    '''
-    for media_type in get_media_types():
-        mm = import_component(media_type + ":MEDIA_MANAGER")
-
-        if isinstance(mm, dict):
-            mm = CompatMediaManager(mm)
-
-        yield media_type, mm
-
-
 def get_media_type_and_manager(filename):
     '''
     Try to find the media type based on the file name, extension
@@ -116,11 +88,10 @@ def get_media_type_and_manager(filename):
         # Get the file extension
         ext = os.path.splitext(filename)[1].lower()
 
-        for media_type, manager in get_media_managers():
-            # Omit the dot from the extension and match it against
-            # the media manager
-            if ext[1:] in manager['accepted_extensions']:
-                return media_type, manager
+        # Omit the dot from the extension and match it against
+        # the media manager
+        if hook_handle('get_media_type_and_manager', ext[1:]):
+            return hook_handle('get_media_type_and_manager', ext[1:])
     else:
         _log.info('File {0} has no file extension, let\'s hope the sniffers get it.'.format(
             filename))