diff data/plugin/macro/MoinImage.py @ 263:448a086d840c

MoinImage: simplify API
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 13 Sep 2008 20:21:20 +0200
parents c605998e1123
children b7e7be4340b0
line wrap: on
line diff
--- a/data/plugin/macro/MoinImage.py	Sat Sep 13 19:28:31 2008 +0200
+++ b/data/plugin/macro/MoinImage.py	Sat Sep 13 20:21:20 2008 +0200
@@ -7,7 +7,7 @@
     * gives ready-to-use image urls
     * auto-rotation based on EXIF information
     * determines creation time from EXIF or file system
-    * access to some EXIF data (cached)
+    * easy access to some EXIF data (cached)
 
     Requires PIL and ExifTags libs.
 
@@ -16,13 +16,6 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-# default width, height
-WEBNAIL_SIZE = (640, 480)
-THUMBNAIL_SIZE = (128, 128)
-
-# we don't process and cache all EXIF infos, but just these:
-EXIF_CACHED = set(['DateTimeOriginal', 'TimeZoneOffset', 'Orientation', ])
-
 import os, time
 import StringIO
 
@@ -40,17 +33,20 @@
     ExifTags = None
 
 class MoinImage(object):
+    # predefined sizes (width, height) - use them if you like:
+    WEBNAIL_SIZE = (640, 480)
+    THUMBNAIL_SIZE = (128, 128)
+
+    # we don't process and cache all EXIF infos, but just these:
+    EXIF_CACHED = set(['DateTimeOriginal', 'TimeZoneOffset', 'Orientation', ])
+
     def __init__(self, request,
-                 name, # PageName/AttachName for now, later this is just the item name
+                 item_name, # PageName/AttachName for now, later this is just the item name
                  description=u'', # we just store this, but we do not use it in MoinImage
-                 webnail_size=WEBNAIL_SIZE,
-                 thumbnail_size=THUMBNAIL_SIZE,
         ):
         self.request = request
-        self.pagename, self.attachname = name.rsplit('/', 1)
+        self.pagename, self.attachname = item_name.rsplit('/', 1)
         self.description = description
-        self.webnail_size = webnail_size
-        self.thumbnail_size = thumbnail_size
 
         # cached property values:
         self.__filename = None
@@ -76,7 +72,7 @@
         if self.__image is None and Image is not None:
             self.__image = Image.open(self._filename)
         return self.__image
-    image = property(_get_image)
+    image = property(_get_image) # the original image (PIL Image object) or None
 
     def _get_cache_key(self):
         if self.__cache_key is None:
@@ -97,7 +93,7 @@
                     if self.image is not None: # we have PIL
                         for tag, value in self.image._getexif().items():
                             tag_name = ExifTags.TAGS.get(tag)
-                            if tag_name in EXIF_CACHED:
+                            if tag_name in self.EXIF_CACHED:
                                 exif_data[tag_name] = value
                         try:
                             time_str = exif_data['DateTimeOriginal']
@@ -120,7 +116,7 @@
             else:
                 self.__exif = exif_cache.content()
         return self.__exif
-    exif = property(_get_exif_data)
+    exif = property(_get_exif_data) # dict of preprocessed EXIF data (string -> value)
 
     def _get_ctime(self):
         """ return creation time of image (either from EXIF or file date) as UNIX timestamp """
@@ -181,9 +177,14 @@
             data = ''
         return content_type, data
 
-    def _cache_url(self, size=None):
-        """ return a cache url for a rendering of this image with specified size,
-            make sure the cache contains that rendering.
+    def url(self, size=None):
+        """ return a cache url for a rendering of this image with specified size -
+            the code automatically makes sure that the cache contains that rendering.
+            If size is None, it gives a url for the full image size rendering.
+            Otherwise, size has to be a tuple (w, h) - if you like, you can use
+            these class level constant sizes:
+                WEBNAIL_SIZE - medium size, one of those likely to fit in a browser window
+                THUMBNAIL_SIZE - small size, for showing collections
         """
         request = self.request
         content_type = self.content_type
@@ -195,19 +196,7 @@
         if not cache.exists(request, key):
             content_type, data = self._transform(size=size, content_type=content_type)
             cache.put(request, key, data, content_type=content_type)
-        return cache.url(self.request, key)
-
-    def webnail_url(self):
-        """ return url of webnail of this image """
-        return self._cache_url(self.webnail_size)
-
-    def thumbnail_url(self):
-        """ return url of thumbnail of this image """
-        return self._cache_url(self.thumbnail_size)
-
-    def fullsize_url(self):
-        """ return url of non-resized, but maybe transposed original image """
-        return self._cache_url() # keep size as is
+        return cache.url(request, key)
 
 
 def macro_MoinImage(macro, itemname=wikiutil.required_arg(unicode), desc=u''):
@@ -221,10 +210,7 @@
     thispagename = fmt.page.page_name
     pagename, fname = AttachFile.absoluteName(itemname, thispagename)
     img = MoinImage(request, itemname, desc)
-
-    src = img.fullsize_url()
-    alt = fmt.text(img.description)
-    return (fmt.image(src=src, alt=alt) +
+    return (fmt.image(src=img.url(img.THUMBNAIL_SIZE), alt=img.description) +
             fmt.text(time.asctime(time.gmtime(img.ctime))) +
             ' ' +
             fmt.text(img.description) +