Attachment 'Gallery2-1.3.3-1.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Gallery2 parser
   4 
   5     PURPOSE:
   6         This parser is used to visualize a couple of images as a thumbnail gallery.
   7 	Optional a description of an image could be added including WikiName.
   8 	On default the image name and it's creation date is shown.
   9 
  10     CALLING SEQUENCE:
  11       {{{
  12       #!Gallery2 columns=columns,filter=filter,mode=mode,show_text=show_text,show_date=show_date, show_tools=show_tools, only_items=only_items,border_thick=border_thick
  13       * [image1.jpg alias]
  14       * [image2.jpg alias]
  15       }}}
  16 
  17     KEYWORD PARAMETERS:
  18         columns:      number of columns for thumbnails
  19 	filter:       regex to select images
  20         show_text:    default is 1 description is shown
  21 	              any other means no description
  22 	show_date:    default is 1 date info from exif header if available is shown
  23                       any other means no description
  24 	show_tools:   default is 1 icon toolbar is show any other disables this
  25 	mode:         default is 1 this means description below the image
  26 	              any other number means description right of image
  27 	only_items:   default is 0 if it is set to 1 only images which are described in listitem are shown
  28 	border_thick: default is 1 this is the thickness in pixeln of the outer frame
  29 
  30     INPUTS:
  31 	itemlist : if it is used and only_items is 1 then only the images in this list are ahown.
  32 	           The alias text is used as description of the image instead of the file name
  33 
  34 
  35     EXAMPLE:
  36      {{{
  37      #!Gallery2 columns=3,mode=2
  38      * [100_1183.JPG Hannover]
  39      * [100_1184.JPG Berlin]
  40      }}}
  41      -----
  42     {{{
  43     #!Gallery2 filter=100_118[0-8],mode=2)
  44     }}}
  45     -----
  46     {{{
  47     #!Gallery2 columns=6,filter=100_119[0-8],show_date=0
  48     }}}
  49     -----
  50     {{{
  51     #!Gallery2 columns=1,mode=1,show_date=0,only_items=1,show_tools=0,show_text=1,border_thick=5
  52     * [IMG1.JPG MainPage]
  53     * [IMG2.JPG StartSeite]
  54     }}}
  55 
  56     PROCEDURE:
  57       Aliasing of the filenames are done by adding an itemlist, see example.
  58 
  59       This routine requires the PIL (Python Imaging Library).
  60       And the EXIF routine from http://home.cfl.rr.com/genecash/
  61 
  62       At the moment I have added the EXIF routine to the parsers dir.
  63       It's not the best place but during developing it is nice to have it there
  64       If you put it to another place you have to change the line
  65       from MoinMoin.parser import EXIF too.
  66 
  67       Please remove the Version number from the code!
  68 
  69     MODIFICATION HISTORY:
  70         Version 1.3.3.-1
  71         @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
  72         @license: GNU GPL, see COPYING for details.
  73 
  74 """
  75 
  76 from MoinMoin.action import AttachFile
  77 from MoinMoin import wikiutil, config
  78 from MoinMoin.Page import Page
  79 import os,string,re,Image,StringIO
  80 from MoinMoin.parser import EXIF
  81 
  82 from MoinMoin.parser import wiki
  83 
  84 
  85 
  86 def get_quotes(self,formatter):
  87     quotes = self.raw.split('\n')
  88     quotes = [quote.strip() for quote in quotes]
  89     quotes = [quote[2:] for quote in quotes if quote.startswith('* ')]
  90 
  91     image=[]
  92     alias=[]
  93     for line in quotes:
  94         im,na=line[1:-1].split(' ',1)
  95 
  96         ##taken from MiniPage
  97 
  98         out=StringIO.StringIO()
  99         self.request.redirect(out)
 100         wikiizer = wiki.Parser(na.strip(),self.request)
 101         wikiizer.format(formatter)
 102         na=out.getvalue()
 103         self.request.redirect()
 104         del out
 105 
 106         alias.append(na)
 107         image.append(im.strip())
 108 
 109     result={}
 110     result['alias']=alias
 111     result['image']=image
 112 
 113     return(result)
 114 
 115 
 116 
 117 class Parser:
 118     def __init__(self, raw, request, **kw):
 119         self.raw = raw
 120         self.request = request
 121         self.form = request.form
 122         self._ = request.getText
 123         self.kw = {}
 124         self.kw['border_thick']='1'
 125         self.kw['columns']='4'
 126         self.kw['filter']='.'
 127         self.kw['mode']='1'
 128         self.kw['show_text']='1'
 129         self.kw['show_date']='1'
 130         self.kw['show_tools']='1'
 131         self.kw['only_items']='0'
 132 
 133         for arg in kw.get('format_args','').split(','):
 134 
 135             if (arg.find('=') > -1):
 136                key=arg.split('=')
 137                self.kw[str(key[0])]=wikiutil.escape(string.join(key[1],''), quote=1)
 138 
 139 
 140     def format(self, formatter):
 141 
 142         kw=self.kw
 143         quotes=get_quotes(self,formatter)
 144 
 145         current_pagename=formatter.page.page_name
 146         attachment_path = AttachFile.getAttachDir(self.request,current_pagename,create=1)
 147 
 148         if (kw['only_items'] == '1'):
 149             all_files=quotes['image']
 150         else:
 151             all_files=os.listdir(attachment_path)
 152 
 153 
 154         result=[]
 155 
 156         for test in all_files:
 157            if re.match(kw['filter'], test):
 158               result.append(test)
 159 
 160         all_files=result
 161 
 162         if (len(all_files) == 0):
 163            self.request.write("<BR><BR><H1>No matching image file found!</H1>")
 164            return
 165 
 166         cells=[]
 167         big={}
 168         medium={}
 169         small={}
 170         cell_name=[]
 171         exif_date=[]
 172         big_image_url=[]
 173 
 174         valid_img=0
 175 
 176         for attfile in all_files:
 177              # only files not thumb or webnails
 178             if ((string.find(string.join(attfile,''),'thumbnail_') == -1) and (string.find(string.join(attfile,''),'webnail_') == -1)) :
 179                 # only images
 180                 if wikiutil.isPicture(attfile):
 181 
 182                     file, ext = os.path.splitext(attfile)
 183 
 184                     if (ext == '.gif') or (ext == '.png'):
 185                         img_type='PNG'
 186                         thumbfile='thumbnail_'+file+".png"
 187                         webnail='webnail_'+file+".png"
 188                     else:
 189                         img_type="JPEG"
 190                         thumbfile='thumbnail_'+file+".jpg"
 191                         webnail='webnail_'+file+".jpg"
 192 
 193                     infile=attachment_path+'/'+attfile
 194                     f=open(infile, 'rb')
 195                     tags=EXIF.process_file(f)
 196 
 197 
 198                     if tags.has_key('EXIF DateTimeOriginal'):
 199                          date=str(tags['EXIF DateTimeOriginal'])
 200                          date=string.replace(date,':','-',2)
 201                          exif_date.append(date)
 202                     else:
 203                         exif_date.append('--')
 204 
 205 		    f.close()
 206 
 207                     thumb=attachment_path+'/'+thumbfile
 208                     webf=attachment_path+'/'+webnail
 209 
 210                     valid_img=valid_img+1
 211 
 212                     if not os.path.exists(thumb) :
 213                         im = Image.open(infile)
 214                         im.thumbnail((640,640), Image.ANTIALIAS)
 215 
 216                         im.save(webf, img_type)
 217 
 218                         im.thumbnail((128, 128), Image.ANTIALIAS)
 219                         im.save(thumb, img_type)
 220 
 221                     big_image_url.append(AttachFile.getAttachUrl(current_pagename,attfile,self.request))
 222                     medium['src']=AttachFile.getAttachUrl(current_pagename,webnail,self.request)
 223 
 224 
 225                     small['title']=attfile
 226                     small['alt']=attfile
 227                     small['src']=AttachFile.getAttachUrl(current_pagename,thumbfile,self.request)
 228 
 229 
 230                     image_link=formatter.image(**small)
 231                     # collect images
 232                     cells.append(formatter.url(1,medium['src'] )+image_link+formatter.url(0))
 233                     cell_name.append(attfile)
 234 
 235 
 236         ##over all images
 237         n=len(cells)
 238         cols=int(kw['columns'])
 239 
 240 
 241         rows=n/cols
 242         first=0
 243         result=[]
 244 
 245         if (valid_img > 1):
 246            result.append('<table border="'+kw['border_thick']+'">')
 247 
 248         icon={}
 249         icon['src']='/wiki/modern/img/idea.png'
 250         icon['title']='Load Image'
 251 
 252         z=1
 253         i=0
 254 
 255         ############################################
 256         ##TODO: syntax change to formatter - later #
 257         ############################################
 258 
 259         # fixed width because of better visualisation on different browsers
 260         for line in cells:
 261             if (z == 1):
 262                if (valid_img > 1):
 263                   if (kw['mode']=='1'):
 264                      result.append(formatter.table_cell(1))
 265                   else:
 266                       result.append('<td width="300" >')
 267             result.append('<table border="1">')
 268             result.append('<TR valign="top">')
 269             result.append('<td align="center" width="140">')
 270             result.append(line)
 271             result.append(formatter.table_cell(0))
 272             if (kw['show_text']=='1'):
 273                 if (kw['mode']=='1'):
 274                   result.append(formatter.table_row(0))
 275                   result.append(formatter.table_row(1))
 276 
 277                 if (kw['mode']=='1'):
 278                     result.append('<td width="140" >')
 279                 else:
 280                     result.append('<td width="140" >')
 281 
 282                 found=0
 283                 for text in quotes['image'] :
 284                     if (text == cell_name[i]):
 285                         result.append(quotes['alias'][i])
 286                         found=1
 287 
 288                 if (found == 0):
 289                     result.append(cell_name[i])
 290 
 291                 result.append(formatter.table_cell(0))
 292                 if (kw['mode']=='1'):
 293                     result.append(formatter.table_row(0))
 294 
 295 
 296             if (kw['show_date']=='1'):
 297                 if (kw['mode']=='1'):
 298                     result.append(formatter.table_row(1))
 299                     result.append(formatter.table_cell(1))
 300                     result.append(exif_date[i])
 301                     result.append(formatter.table_cell(0))
 302                     result.append(formatter.table_row(0))
 303             if (kw['show_tools'] == '1'):
 304                result.append(formatter.table_row(1))
 305                result.append('<td width="140">')
 306                small=formatter.image(**icon)
 307                result.append(formatter.url(1,big_image_url[i])+small+formatter.url(0))
 308 
 309             result.append(formatter.table_cell(0))
 310             if (kw['show_date']=='1'):
 311                 if not (kw['mode']=='1'):
 312                     result.append(formatter.table_cell(1))
 313                     result.append(exif_date[i])
 314                     result.append(formatter.table_cell(0))
 315             if (kw['show_tools'] == '1'):
 316                result.append(formatter.table_row(0))
 317             result.append(formatter.table(0))
 318             if ((z != cols) and (i < n-1)):
 319                 result.append(formatter.table_cell(1))
 320             if (z == cols):
 321                 result.append(formatter.table_cell(0))
 322                 result.append(formatter.table_row(0))
 323             z=z+1
 324             i=i+1
 325             if (z > cols):
 326                z=1
 327         if (valid_img > 1):
 328            result.append(formatter.table(0))
 329 
 330         ##Output
 331         self.request.write(string.join(result,''))

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-08-06 08:57:54, 41.8 KB) [[attachment:EXIF.py]]
  • [get | view] (2005-03-24 20:14:37, 10.6 KB) [[attachment:Gallery2-1.3.3-1.py]]
  • [get | view] (2005-03-26 08:39:13, 11.6 KB) [[attachment:Gallery2-1.3.3-2.py]]
  • [get | view] (2005-03-26 12:41:49, 13.1 KB) [[attachment:Gallery2-1.3.3-3.py]]
  • [get | view] (2005-03-27 20:23:01, 19.0 KB) [[attachment:Gallery2-1.3.3-4.py]]
  • [get | view] (2005-08-03 19:30:10, 23.2 KB) [[attachment:Gallery2-1.3.3-5.py]]
  • [get | view] (2005-08-18 07:58:38, 31.9 KB) [[attachment:Gallery2-1.3.5-10.py]]
  • [get | view] (2005-09-02 19:55:13, 34.1 KB) [[attachment:Gallery2-1.3.5-11.py]]
  • [get | view] (2005-11-13 18:09:11, 35.4 KB) [[attachment:Gallery2-1.3.5-12.py]]
  • [get | view] (2005-11-18 20:13:04, 46.2 KB) [[attachment:Gallery2-1.3.5-13.py]]
  • [get | view] (2005-12-03 15:33:06, 46.6 KB) [[attachment:Gallery2-1.3.5-14.py]]
  • [get | view] (2006-01-01 09:20:19, 43.3 KB) [[attachment:Gallery2-1.3.5-15.py]]
  • [get | view] (2005-08-07 15:46:28, 26.9 KB) [[attachment:Gallery2-1.3.5-6.py]]
  • [get | view] (2005-08-13 15:13:59, 28.7 KB) [[attachment:Gallery2-1.3.5-7.py]]
  • [get | view] (2005-08-14 13:02:00, 27.5 KB) [[attachment:Gallery2-1.3.5-8.py]]
  • [get | view] (2005-08-14 14:38:32, 28.7 KB) [[attachment:Gallery2-1.3.5-9.py]]
  • [get | view] (2006-08-06 08:45:47, 41.8 KB) [[attachment:Gallery2-1.5.4-16.py]]
  • [get | view] (2006-08-22 20:29:39, 42.0 KB) [[attachment:Gallery2-1.5.4-18.py]]
  • [get | view] (2006-08-06 08:57:36, 514.8 KB) [[attachment:example.swf]]
  • [get | view] (2005-08-17 18:10:27, 11.3 KB) [[attachment:gallery2image_test.py]]
  • [get | view] (2005-08-10 16:49:16, 1.3 KB) [[attachment:patchpullfromdir.diff]]
  • [get | view] (2006-08-17 16:32:50, 41.9 KB) [[attachment:text_x_gallery2-1.6.0-17.py]]
  • [get | view] (2006-08-22 20:23:06, 42.1 KB) [[attachment:text_x_gallery2-1.6.0-18.py]]
  • [get | view] (2008-02-06 10:08:05, 42.2 KB) [[attachment:text_x_gallery2-1.6.0-19.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.