Updated annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Mon, 28 May 2018 14:04:42 +0200
changeset 90 8f71dece6170
parent 89 1255acff2eb9
child 91 eec2392dacb5
Updated annotations adapters
src/pyams_media/media.py
--- a/src/pyams_media/media.py	Mon May 28 11:27:14 2018 +0200
+++ b/src/pyams_media/media.py	Mon May 28 14:04:42 2018 +0200
@@ -24,20 +24,18 @@
     IMediaConversion, IMediaConversionUtility, VIDEO_FRAME_SIZE, MEDIA_CONVERSIONS_KEY
 from pyams_utils.interfaces.tales import ITALESExtension
 from transaction.interfaces import ITransactionManager
-from zope.annotation.interfaces import IAnnotations
 from zope.lifecycleevent.interfaces import IObjectAddedEvent
 
 # import packages
 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, ContextRequestViewAdapter
+from pyams_utils.adapter import adapter_config, ContextAdapter, ContextRequestViewAdapter, get_annotation_adapter
 from pyams_utils.registry import query_utility, get_global_registry
 from pyramid.events import subscriber
 from zope.container.folder import Folder
 from zope.interface import implementer, alsoProvides, Interface
 from zope.lifecycleevent import ObjectCreatedEvent
-from zope.location import locate
 from zope.traversing.interfaces import ITraversable
 
 
@@ -51,6 +49,10 @@
 @adapter_config(context=IFile, provides=IMediaInfo)
 def media_info_factory(context):
     """Media info adapter"""
+
+    def media_info_adapter_factory():
+        return FFmpeg('avprobe').info(context)
+
     content_type = context.content_type
     if isinstance(content_type, bytes):
         content_type = content_type.decode()
@@ -58,11 +60,8 @@
             content_type.startswith('video/') or
             content_type in (CUSTOM_AUDIO_TYPES + CUSTOM_VIDEO_TYPES)):
         return None
-    annotations = IAnnotations(context)
-    info = annotations.get(MEDIA_INFO_KEY)
-    if info is None:
-        info = annotations[MEDIA_INFO_KEY] = FFmpeg('avprobe').info(context)
-    return info
+    return get_annotation_adapter(context, MEDIA_INFO_KEY, factory=media_info_adapter_factory,
+                                  notify=False, locate=False)
 
 
 #
@@ -123,12 +122,8 @@
 @adapter_config(context=IFile, provides=IMediaConversions)
 def media_conversions_factory(context):
     """Media conversions factory"""
-    annotations = IAnnotations(context)
-    conversions = annotations.get(MEDIA_CONVERSIONS_KEY)
-    if conversions is None:
-        conversions = annotations[MEDIA_CONVERSIONS_KEY] = MediaConversions()
-        locate(conversions, context, '++conversions++')
-    return conversions
+    return get_annotation_adapter(context, MEDIA_CONVERSIONS_KEY, MediaConversions,
+                                  name='++conversions++')
 
 
 @adapter_config(name='conversions', context=IFile, provides=ITraversable)