# HG changeset patch # User Thierry Florac # Date 1507878528 -7200 # Node ID 05898b7a0a73ad9c56008ad9a62ecf4928535a1f # Parent 7de6abf4258d3704f6d1bdf722df77aa7b9599c8 Added audio media type interface and adapter diff -r 7de6abf4258d -r 05898b7a0a73 src/pyams_media/audio.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 +# 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 diff -r 7de6abf4258d -r 05898b7a0a73 src/pyams_media/interfaces/__init__.py --- 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") #