Updated conversion adapters
authorThierry Florac <thierry.florac@onf.fr>
Tue, 08 Sep 2015 16:59:44 +0200
changeset 6 b78dab193043
parent 5 73e9b218db71
child 7 b930c820b6cd
Updated conversion adapters
src/pyams_media/media.py
--- a/src/pyams_media/media.py	Tue Sep 08 16:59:10 2015 +0200
+++ b/src/pyams_media/media.py	Tue Sep 08 16:59:44 2015 +0200
@@ -14,12 +14,15 @@
 
 
 # import standard library
+import re
+
 from mimetypes import guess_extension, guess_type
 
 # import interfaces
 from pyams_file.interfaces import IFile
 from pyams_media.interfaces import IMediaInfo, CUSTOM_AUDIO_TYPES, CUSTOM_VIDEO_TYPES, IMediaConversions, \
-    IMediaConversion, IMediaConversionUtility
+    IMediaConversion, IMediaConversionUtility, VIDEO_FRAME_SIZE
+from pyams_utils.interfaces.tales import ITALESExtension
 from transaction.interfaces import ITransactionManager
 from zope.annotation.interfaces import IAnnotations
 from zope.lifecycleevent.interfaces import IObjectAddedEvent
@@ -28,12 +31,12 @@
 from pyams_file.file import FileFactory
 from pyams_media.ffbase import FFmpeg
 from pyams_media.ffdocument import FFDocument
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.adapter import adapter_config, ContextAdapter, ContextRequestViewAdapter
 from pyams_utils.registry import query_utility
 from pyramid.events import subscriber
-from pyramid.threadlocal import get_current_registry
+from zope.component.globalregistry import getGlobalSiteManager
 from zope.container.folder import Folder
-from zope.interface import implementer, alsoProvides
+from zope.interface import implementer, alsoProvides, Interface
 from zope.lifecycleevent import ObjectCreatedEvent
 from zope.location import locate
 from zope.traversing.interfaces import ITraversable
@@ -66,6 +69,8 @@
 
 MEDIA_CONVERSIONS_KEY = 'pyams_media.media.conversions'
 
+MEDIA_FRAME_SIZE = re.compile(".*-(.*)\..*")
+
 
 @implementer(IMediaConversions)
 class MediaConversions(Folder):
@@ -73,7 +78,7 @@
 
     def add_conversion(self, conversion, format, extension=None, width=None):
         target = FileFactory(conversion)
-        registry = get_current_registry()
+        registry = getGlobalSiteManager()
         registry.notify(ObjectCreatedEvent(target))
         alsoProvides(target, IMediaConversion)
         if extension is None:
@@ -82,18 +87,25 @@
                                                              if target.content_type else 'media',
                                                          width='-{0}'.format(width) if width else '',
                                                          extension=extension)
+        target.filename = target_name
         self[target_name] = target
 
-    def get_conversions(self):
-        context = self.__parent__
-        return [context] + list(self.values())
+    def get_conversions(self, with_source=False, order=None):
+        result = []
+        if with_source:
+            result.append(self.__parent__)
+        result.extend(list(self.values()))
+        if order:
+            order = tuple(map(lambda x: x.encode(), order))
+            result.sort(key=lambda x: (order.index(x.content_type) if x.content_type in order else 999,
+                                       self.get_conversion_width(x.filename) or 9999))
+        return result
 
-    def get_conversion(self, name):
-        if '/' in name:
-            for conversion in self.get_conversions():
-                if conversion.content_type == name:
-                    return conversion
-        return self.get(name)
+    @staticmethod
+    def get_conversion_width(name):
+        match = MEDIA_FRAME_SIZE.match(name)
+        if match:
+            return VIDEO_FRAME_SIZE.get(match.groups()[0])
 
     def has_conversion(self, formats):
         for conversion in self.get_conversions():
@@ -121,6 +133,16 @@
         return IMediaConversions(self.context)
 
 
+@adapter_config(name='conversions', context=(IFile, Interface, Interface), provides=ITALESExtension)
+class ConversionsExtension(ContextRequestViewAdapter):
+    """extension:conversions(media) TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        return IMediaConversions(context)
+
+
 #
 # Media files events
 #