src/pyams_media/audio.py
changeset 58 05898b7a0a73
--- /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