Added audio media type interface and adapter
authorThierry Florac <thierry.florac@onf.fr>
Fri, 13 Oct 2017 09:08:48 +0200
changeset 58 05898b7a0a73
parent 57 7de6abf4258d
child 59 4d725464e237
Added audio media type interface and adapter
src/pyams_media/audio.py
src/pyams_media/interfaces/__init__.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_media/audio.py	Fri Oct 13 09:08:48 2017 +0200
@@ -0,0 +1,61 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_file.interfaces import IAudio
+from pyams_media.interfaces import IAudioType
+from pyams_utils.interfaces.tales import ITALESExtension
+
+# import packages
+from pyams_utils.adapter import adapter_config, ContextAdapter, ContextRequestViewAdapter
+from zope.interface import Interface
+
+
+#
+# Custom audio types
+#
+
+@adapter_config(context=IAudio, provides=IAudioType)
+class AudioTypeAdapter(ContextAdapter):
+    """Default audio content type adapter"""
+
+    @property
+    def audio_type(self):
+        return self.context.content_type
+
+
+@adapter_config(name='audio_type', context=(Interface, Interface, Interface), provides=ITALESExtension)
+class AudioTypeExtension(ContextRequestViewAdapter):
+    """extension:audio_type(media) TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        if not IAudio.providedBy(context):
+            return None
+        registry = self.request.registry
+        content_type = context.content_type
+        if isinstance(content_type, bytes):
+            content_type = content_type.decode()
+        adapter = registry.queryAdapter(context, IAudioType, name=content_type)
+        if adapter is None:
+            adapter = registry.queryAdapter(context, IAudioType)
+        if adapter is not None:
+            audio_type = adapter.audio_type
+            if isinstance(audio_type, bytes):
+                audio_type = audio_type.decode()
+            return audio_type
--- a/src/pyams_media/interfaces/__init__.py	Thu Sep 21 12:42:23 2017 +0200
+++ b/src/pyams_media/interfaces/__init__.py	Fri Oct 13 09:08:48 2017 +0200
@@ -62,14 +62,20 @@
     """Media video converter"""
 
 
+class IVideoType(Interface):
+    """Video content-type interface"""
+
+    video_type = Attribute("Video content type")
+
+
 class IMediaAudioConverter(IMediaConverter):
     """Media audio converter"""
 
 
-class IVideoType(Interface):
-    """Video content-type interface"""
+class IAudioType(Interface):
+    """Audio content-type interface"""
 
-    video_type = Attribute("Video content type")
+    audio_type = Attribute("Audio content type")
 
 
 #