Attachment 'Gallery2-1.3.3-3.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],
  13                  [show_date=show_date], [show_tools=show_tools],
  14                  [only_items=only_items],[border_thick=border_thick],[renew=renew]
  15       * [image1.jpg alias]
  16       * [image2.jpg alias]
  17       }}}
  18 
  19     KEYWORD PARAMETERS:
  20         columns:      number of columns for thumbnails
  21 	filter:       regex to select images
  22         show_text:    default is 1 description is shown
  23 	              any other means no description
  24 	show_date:    default is 1 date info from exif header if available is shown
  25                       any other means no description
  26 	show_tools:   default is 1 icon toolbar is show any other disables this
  27 	mode:         default is 1 this means description below the image
  28 	              any other number means description right of image
  29 	only_items:   default is 0 if it is set to 1 only images which are described in listitem are shown
  30 	border_thick: default is 1 this is the thickness in pixeln of the outer frame
  31 	renew:        default is 0 if set to 1 then all selected thumbnails_* and webnails_* removed.
  32 	              Afterwards they are new created.
  33 
  34     INPUTS:
  35 	itemlist : if it is used and only_items is 1 then only the images in this list are ahown.
  36 	           The alias text is used as description of the image instead of the file name
  37 
  38 
  39     EXAMPLE:
  40 
  41     = GalleryTest =
  42 
  43     == all images shown, one is decribed ==
  44     {{{
  45     { { {
  46     #!Gallery2
  47     * [100_1185.JPG Bremen, SpaceCenter]
  48     } } }
  49     }}}
  50 
  51     Result: [[BR]]
  52     {{{
  53     #!Gallery2
  54     * [100_1185.JPG Bremen, SpaceCenter]
  55     }}}
  56 
  57     == only_items by two columns and text right ==
  58     {{{
  59     { { {
  60     #!Gallery2 mode=2,columns=2,only_items=1
  61     * [100_1185.JPG Bremen, SpaceCenter]
  62     * [100_1194.JPG Bremen]
  63     } } }
  64     }}}
  65 
  66     Result: [[BR]]
  67     {{{
  68     #!Gallery2 mode=2,columns=2,only_items=1
  69     * [100_1185.JPG Bremen, SpaceCenter]
  70     * [100_1194.JPG Bremen, behind SpaceCenter]
  71     }}}
  72 
  73     == only_items by two columns, date supressed ==
  74     {{{
  75     { { {
  76     #!Gallery2 columns=2,only_items=1,show_date=0
  77     * [100_1185.JPG Bremen, SpaceCenter]
  78     * [100_1194.JPG Bremen, behind SpaceCenter]
  79     } } }
  80     }}}
  81 
  82     Result: [[BR]]
  83     {{{
  84     #!Gallery2 columns=2,only_items=1,show_date=0
  85     * [100_1185.JPG Bremen, SpaceCenter]
  86     * [100_1194.JPG Bremen]
  87     }}}
  88 
  89 
  90     == filter regex used, mode 2, icons and date supressed, one column and border_thick=5 ==
  91     {{{
  92     { { {
  93     #!Gallery2 columns=1,filter=100_118[0-5],mode=2,show_date=0,show_tools=0,border_thick=5
  94     } } }
  95     }}}
  96 
  97     Result: [[BR]]
  98     {{{
  99     #!Gallery2 columns=1,filter=100_118[0-7],mode=2,show_date=0,show_tools=0,border_thick=5
 100     }}}
 101 
 102     == other macro calls ==
 103     {{{
 104     { { {
 105     #!Gallery2 only_items=1,show_date=0
 106     * [100_1189.JPG [[MiniPage(||Bremen||SpaceCenter||\n|| ||SpaceJump||)]]]
 107     } } }
 108     }}}
 109 
 110     Result: [[BR]]
 111     {{{
 112     #!Gallery2 only_items=1,show_date=0
 113     * [100_1189.JPG [[MiniPage(||Bremen||SpaceCenter||\n|| ||SpaceJump||)]]]
 114     }}}
 115 
 116     == renew means always new thumbnails and webnails of selection ==
 117     {{{
 118     { { {
 119     #!Gallery2 only_items=1,show_date=0,show_tools=0,renew=1
 120     * [100_1189.JPG [[MiniPage(||Bremen||SpaceCenter||\n|| ||SpaceJump||)]]]
 121     } } }
 122     }}}
 123 
 124     Result: [[BR]]
 125     {{{
 126     #!Gallery2 only_items=1,show_date=0,renew=1
 127     * [100_1189.JPG [[MiniPage(||Bremen||SpaceCenter||\n|| ||SpaceJump||)]]]
 128     }}}
 129 
 130     PROCEDURE:
 131       Download some images to a page and start with the examples.
 132       Aliasing of the filenames are done by adding an itemlist, see example.
 133 
 134       This routine requires the PIL (Python Imaging Library).
 135       And the EXIF routine from http://home.cfl.rr.com/genecash/
 136 
 137       At the moment I have added the EXIF routine to the parsers dir.
 138       It's not the best place but during developing it is nice to have it there
 139       If you put it to another place you have to change the line
 140       from MoinMoin.parser import EXIF too.
 141 
 142       Please remove the Version number from the code!
 143 
 144     MODIFICATION HISTORY:
 145         Version 1.3.3.-1
 146         @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
 147         @license: GNU GPL, see COPYING for details.
 148 	2005-03-26: Version 1.3.3-2 keyword renew added
 149 	            creation of thumnails and webnails in two calls splitted
 150                     Version 1.3.3-3 bug fixed if itemlist is given to describe only some of the images
 151 		                    but only_items is not set to 1
 152 				    Example code changed
 153 
 154 """
 155 
 156 from MoinMoin.action import AttachFile
 157 from MoinMoin import wikiutil, config
 158 from MoinMoin.Page import Page
 159 import os,string,re,Image,StringIO
 160 from MoinMoin.parser import EXIF
 161 
 162 from MoinMoin.parser import wiki
 163 
 164 
 165 
 166 def get_quotes(self,formatter):
 167     quotes = self.raw.split('\n')
 168     quotes = [quote.strip() for quote in quotes]
 169     quotes = [quote[2:] for quote in quotes if quote.startswith('* ')]
 170 
 171     image=[]
 172     alias=[]
 173     for line in quotes:
 174         im,na=line[1:-1].split(' ',1)
 175 
 176         ##taken from MiniPage
 177 
 178         out=StringIO.StringIO()
 179         self.request.redirect(out)
 180         wikiizer = wiki.Parser(na.strip(),self.request)
 181         wikiizer.format(formatter)
 182         na=out.getvalue()
 183         self.request.redirect()
 184         del out
 185 
 186         alias.append(na)
 187         image.append(im.strip())
 188 
 189     result={}
 190     result['alias']=alias
 191     result['image']=image
 192 
 193     return(result)
 194 
 195 
 196 
 197 class Parser:
 198     def __init__(self, raw, request, **kw):
 199         self.raw = raw
 200         self.request = request
 201         self.form = request.form
 202         self._ = request.getText
 203         self.kw = {}
 204         self.kw['border_thick']='1'
 205         self.kw['columns']='4'
 206         self.kw['filter']='.'
 207         self.kw['mode']='1'
 208         self.kw['show_text']='1'
 209         self.kw['show_date']='1'
 210         self.kw['show_tools']='1'
 211         self.kw['only_items']='0'
 212         self.kw['renew']='0'
 213 
 214         for arg in kw.get('format_args','').split(','):
 215 
 216             if (arg.find('=') > -1):
 217                key=arg.split('=')
 218                self.kw[str(key[0])]=wikiutil.escape(string.join(key[1],''), quote=1)
 219 
 220 
 221     def format(self, formatter):
 222 
 223         kw=self.kw
 224         quotes=get_quotes(self,formatter)
 225 
 226         current_pagename=formatter.page.page_name
 227         attachment_path = AttachFile.getAttachDir(self.request,current_pagename,create=1)
 228 
 229         if (kw['only_items'] == '1'):
 230             all_files=quotes['image']
 231         else:
 232             all_files=os.listdir(attachment_path)
 233 
 234         result=[]
 235 
 236         for test in all_files:
 237            if re.match(kw['filter'], test):
 238               result.append(test)
 239 
 240         all_files=result
 241 
 242         if (len(all_files) == 0):
 243            self.request.write("<BR><BR><H1>No matching image file found!</H1>")
 244            return
 245 
 246 
 247         cells=[]
 248         big={}
 249         medium={}
 250         small={}
 251         cell_name=[]
 252         exif_date=[]
 253         big_image_url=[]
 254 
 255         valid_img=0
 256 
 257         for attfile in all_files:
 258              # only files not thumb or webnails
 259             if ((string.find(string.join(attfile,''),'thumbnail_') == -1) and (string.find(string.join(attfile,''),'webnail_') == -1)) :
 260                 # only images
 261                 if wikiutil.isPicture(attfile):
 262 
 263                     file, ext = os.path.splitext(attfile)
 264 
 265                     if (ext == '.gif') or (ext == '.png'):
 266                         img_type='PNG'
 267                         thumbfile='thumbnail_'+file+".png"
 268                         webnail='webnail_'+file+".png"
 269                     else:
 270                         img_type="JPEG"
 271                         thumbfile='thumbnail_'+file+".jpg"
 272                         webnail='webnail_'+file+".jpg"
 273 
 274 
 275                     infile=attachment_path+'/'+attfile
 276                     f=open(infile, 'rb')
 277                     tags=EXIF.process_file(f)
 278 
 279                     if tags.has_key('EXIF DateTimeOriginal'):
 280                          date=str(tags['EXIF DateTimeOriginal'])
 281                          date=string.replace(date,':','-',2)
 282                          exif_date.append(date)
 283                     else:
 284                         exif_date.append('--')
 285 
 286                     f.close()
 287 
 288                     thumb=attachment_path+'/'+thumbfile
 289                     webf=attachment_path+'/'+webnail
 290 
 291                     if (kw['renew'] == '1'):
 292                        if os.path.exists(thumb):
 293                           os.unlink(thumb)
 294                        if os.path.exists(webf):
 295                           os.unlink(webf)
 296 
 297                     valid_img=valid_img+1
 298 
 299                     im = Image.open(infile)
 300                     if not os.path.exists(webf):
 301                         im.thumbnail((640,640), Image.ANTIALIAS)
 302                         im.save(webf, img_type)
 303 
 304                     if not os.path.exists(thumb):
 305                         im.thumbnail((128, 128), Image.ANTIALIAS)
 306                         im.save(thumb, img_type)
 307 
 308 
 309 
 310                     big_image_url.append(AttachFile.getAttachUrl(current_pagename,attfile,self.request))
 311                     medium['src']=AttachFile.getAttachUrl(current_pagename,webnail,self.request)
 312 
 313 
 314                     small['title']=attfile
 315                     small['alt']=attfile
 316                     small['src']=AttachFile.getAttachUrl(current_pagename,thumbfile,self.request)
 317 
 318 
 319                     image_link=formatter.image(**small)
 320                     # collect images
 321                     cells.append(formatter.url(1,medium['src'] )+image_link+formatter.url(0))
 322                     cell_name.append(attfile)
 323 
 324 
 325         ##over all images
 326         n=len(cells)
 327         cols=int(kw['columns'])
 328 
 329 
 330         rows=n/cols
 331         first=0
 332         result=[]
 333 
 334         if (valid_img > 1):
 335            result.append('<table border="'+kw['border_thick']+'">')
 336 
 337         icon={}
 338         icon['src']='/wiki/modern/img/idea.png'
 339         icon['title']='Load Image'
 340 
 341         z=1
 342         i=0
 343 
 344         ############################################
 345         ##TODO: syntax change to formatter - later #
 346         ############################################
 347 
 348         # fixed width because of better visualisation on different browsers
 349         for line in cells:
 350             if (z == 1):
 351                if (valid_img > 1):
 352                   if (kw['mode']=='1'):
 353                      result.append(formatter.table_cell(1))
 354                   else:
 355                       result.append('<td width="300" >')
 356             result.append('<table border="1">')
 357             result.append('<TR valign="top">')
 358             result.append('<td align="center" width="140">')
 359             result.append(line)
 360             result.append(formatter.table_cell(0))
 361             if (kw['show_text']=='1'):
 362                 if (kw['mode']=='1'):
 363                   result.append(formatter.table_row(0))
 364                   result.append(formatter.table_row(1))
 365 
 366                 if (kw['mode']=='1'):
 367                     result.append('<td width="140" >')
 368                 else:
 369                     result.append('<td width="140" >')
 370 
 371                 i_quote=0
 372                 found=0
 373                 for text in quotes['image'] :
 374                     if (text == cell_name[i]):
 375                         result.append(quotes['alias'][i_quote])
 376                         found=1
 377                     i_quote=i_quote+1
 378 
 379                 if (found == 0):
 380                     result.append(cell_name[i])
 381 
 382                 result.append(formatter.table_cell(0))
 383                 if (kw['mode']=='1'):
 384                     result.append(formatter.table_row(0))
 385 
 386 
 387             if (kw['show_date']=='1'):
 388                 if (kw['mode']=='1'):
 389                     result.append(formatter.table_row(1))
 390                     result.append(formatter.table_cell(1))
 391                     result.append(exif_date[i])
 392                     result.append(formatter.table_cell(0))
 393                     result.append(formatter.table_row(0))
 394             if (kw['show_tools'] == '1'):
 395                result.append(formatter.table_row(1))
 396                result.append('<td width="140">')
 397                small=formatter.image(**icon)
 398                result.append(formatter.url(1,big_image_url[i])+small+formatter.url(0))
 399 
 400             result.append(formatter.table_cell(0))
 401             if (kw['show_date']=='1'):
 402                 if not (kw['mode']=='1'):
 403                     result.append(formatter.table_cell(1))
 404                     result.append(exif_date[i])
 405                     result.append(formatter.table_cell(0))
 406             if (kw['show_tools'] == '1'):
 407                result.append(formatter.table_row(0))
 408             result.append(formatter.table(0))
 409             if ((z != cols) and (i < n-1)):
 410                 result.append(formatter.table_cell(1))
 411             if (z == cols):
 412                 result.append(formatter.table_cell(0))
 413                 result.append(formatter.table_row(0))
 414             z=z+1
 415             i=i+1
 416             if (z > cols):
 417                z=1
 418         if (valid_img > 1):
 419            result.append(formatter.table(0))
 420 
 421         ##Output
 422         self.request.write(string.join(result,''))
 423         return()

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.