Added IVideoType interface adapters and TALES extension
authorThierry Florac <thierry.florac@onf.fr>
Tue, 08 Sep 2015 17:02:16 +0200
changeset 10 9296741c1470
parent 9 7c73df1106b4
child 11 967f905e1de5
Added IVideoType interface adapters and TALES extension
src/pyams_media/video.py
--- a/src/pyams_media/video.py	Tue Sep 08 17:01:39 2015 +0200
+++ b/src/pyams_media/video.py	Tue Sep 08 17:02:16 2015 +0200
@@ -21,6 +21,8 @@
 
 # import interfaces
 from pyams_file.interfaces import IVideo, IThumbnail
+from pyams_media.interfaces import IVideoType
+from pyams_utils.interfaces.tales import ITALESExtension
 from zope.annotation.interfaces import IAnnotations
 from zope.traversing.interfaces import ITraversable
 
@@ -30,8 +32,9 @@
 from pyams_file.file import ImageFile, get_magic_content_type
 from pyams_file.image import ThumbnailGeometrry
 from pyams_media.ffbase import FFmpeg
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.adapter import adapter_config, ContextAdapter, ContextRequestViewAdapter
 from pyramid.threadlocal import get_current_registry
+from zope.interface import Interface
 from zope.lifecycleevent import ObjectCreatedEvent, ObjectAddedEvent
 from zope.location import locate
 
@@ -53,12 +56,11 @@
             return self.thumbnail.get_image_size()
         else:
             mpeg = FFmpeg('ffprobe')
-            streams = mpeg.info(self.video)
-            if streams:
-                for stream in streams:
-                    if stream.get('codec_type') != 'video':
-                        continue
-                    return stream.get('width'), stream.get('height')
+            streams = mpeg.info(self.video).get('streams', ())
+            for stream in streams:
+                if stream.get('codec_type') != 'video':
+                    continue
+                return stream.get('width'), stream.get('height')
 
     def get_thumbnail_size(self, thumbnail_name, forced=False):
         if self.thumbnail is not None:
@@ -161,3 +163,40 @@
         result = thumbnails.get_thumbnail(thumbnail_name, format)
         transaction.commit()
         return result
+
+
+#
+# Custom video types
+#
+
+@adapter_config(context=IVideo, provides=IVideoType)
+class VideoTypeAdapter(ContextAdapter):
+    """Default video content type adapter"""
+
+    @property
+    def video_type(self):
+        return self.context.content_type
+
+
+@adapter_config(name='video/x-flv', context=IVideo, provides=IVideoType)
+class FlashVideoTypeAdapter(ContextAdapter):
+    """Flash video content type adapter"""
+
+    @property
+    def video_type(self):
+        return b'video/flash'
+
+
+@adapter_config(name='video_type', context=(IVideo, Interface, Interface), provides=ITALESExtension)
+class VideoTypeExtension(ContextRequestViewAdapter):
+    """extension:video_type(media) TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        registry = self.request.registry
+        adapter = registry.queryAdapter(context, IVideoType, name=context.content_type.decode())
+        if adapter is None:
+            adapter = registry.queryAdapter(context, IVideoType)
+        if adapter is not None:
+            return adapter.video_type.decode()