Attachment 'Gallery2-1.3.5-11.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         If you click on a thumbnail you get the webnails shown. By a menue you are able to toggle between the slides.
  10 
  11     CALLING SEQUENCE:
  12       {{{
  13       #!Gallery2 [columns=columns],[filter=filter],[mode=mode],
  14                  [show_text=show_text],[show_date=show_date], [show_tools=show_tools],
  15                  [sort_by_name=sort_by_name],[sort_by_date=sort_by_date], [sort_by_alias=sort_by_alias],
  16                  [reverse_sort=reverse_sort],
  17                  [only_items=only_items],[template_itemlist=template_itemlist],
  18                  [album=album],[album_name=album_name],[front_image=front_image],
  19                  [thumbnail_width=thumbnail_width],[webnail_width=webnail_width],[text_width=text_width],
  20                  [image_for_webnail=image_for_webnail],
  21                  [border_thick=border_thick],[renew=renew],[help=help]
  22       * [image1.jpg alias]
  23       * [image2.jpg alias]
  24       }}}
  25 
  26     KEYWORD PARAMETERS:
  27         columns:           number of columns for thumbnails
  28         filter:            regex to select images
  29         show_text:         default is 1 description is shown
  30                            any other means no description
  31         show_date:         default is 1 date info from exif header if available is shown
  32         show_tools:        default is 1 icon toolbar is show any other disables this
  33         sort_by_name:      default is 1, the images are sorted by name, but not if only_items is 1
  34         sort_by_date:      default is 0, if set to 1 the images are sorted to the modification time
  35         sort_by_alias      default is 0, if set to 1 and only_items set to 1 it is used to order the images by the alias name
  36         reverse_sort:      default is 0, if set to 1 the file list is reversed
  37                            any other means no description
  38         mode:              default is 1 this means description below the image
  39                            any other number means description right of image
  40         only_items:        default is 0 if it is set to 1 only images which are described in listitem are shown
  41                            dependend on the order of the items
  42         template_itemlist: default is 0, if set to 1 an item list is shown which could be copied into the script. 
  43         album:             default is 0 if set to 1 only the first image of a series is shown but slideshow over all images 
  44         album_name:        useful for album. default is 'album' use it as short name for the album. 
  45         front_image:       Useful for album.  default is ''. The first image is shown in front of the album and slideshow.
  46                            If set to an existing image name this is shown in front of album and slideshow. 
  47                            The slide show could start by this somewhere.
  48         border_thick:      default is 1 this is the thickness in pixeln of the outer frame
  49         renew:             default is 0 if set to 1 then all selected thumbnails_* and webnails_* removed.
  50                            Afterwards they are new created.
  51         thumbnail_width:   default is 128
  52         webnail_width:     default is 640
  53         text_width:        default is 140		   
  54         image_for_webnail  default is 0 if set to 1 then the image is shown as preview and not the webnail
  55         help:              default is 0 if set a copy of the CALLING SEQUENCE is shown, 
  56                            (there are some new ideas around to show help to an user so this will be later replaced)
  57 
  58     OPTIONAL INPUTS:
  59         itemlist : if it is used and only_items is 1 then only the images in this list are ahown.
  60                    The alias text is used as description of the image instead of the file name
  61 
  62 
  63     EXAMPLE:
  64 = GalleryTest =
  65 
  66 == all images shown, one is decribed ==
  67 {{{
  68 { { {
  69 #!Gallery2
  70 * [100_1185.JPG Bremen, SpaceCenter]
  71 } } }
  72 }}}
  73 
  74 Result: [[BR]]
  75  {{{
  76 #!Gallery2
  77 * [100_1185.JPG Bremen, SpaceCenter]
  78 }}}
  79 
  80 == only thumbnails and only_items ==
  81 {{{
  82 { { {
  83 #!Gallery2 show_text=0,show_tools=0,show_date=0,columns=2,only_items=1
  84  * [100_1185.JPG Bremen, SpaceCenter]
  85  * [100_1194.JPG Bremen]
  86 } } }
  87 }}}
  88 
  89 Result: [[BR]]
  90  {{{
  91 #!Gallery2 show_text=0,show_tools=0,show_date=0,columns=2,only_items=1
  92  * [100_1185.JPG Bremen, SpaceCenter]
  93  * [100_1194.JPG Bremen]
  94 }}}
  95 
  96 == only_items by two columns and text right ==
  97 
  98 {{{
  99 { { {
 100 #!Gallery2 mode=2,columns=2,only_items=1
 101  * [100_1185.JPG Bremen, SpaceCenter]
 102  * [100_1194.JPG Bremen]
 103 } } }
 104 }}}
 105 
 106 Result: [[BR]]
 107  {{{
 108 #!Gallery2 mode=2,columns=2,only_items=1
 109  * [100_1185.JPG Bremen, SpaceCenter]
 110  * [100_1194.JPG Bremen, behind SpaceCenter]
 111 }}}
 112 
 113 ----
 114 
 115 == only_items by two columns, date supressed ==
 116 
 117 {{{
 118 { { {
 119 #!Gallery2 columns=2,only_items=1,show_date=0
 120  * [100_1185.JPG Bremen, SpaceCenter]
 121  * [100_1194.JPG Bremen, behind SpaceCenter]
 122 } } }
 123 }}}
 124 
 125 Result: [[BR]]
 126  {{{
 127 #!Gallery2 columns=2,only_items=1,show_date=0
 128  * [100_1185.JPG Bremen, SpaceCenter]
 129  * [100_1194.JPG Bremen, behind SpaceCenter]
 130 }}}
 131 
 132 
 133 == filter regex used, mode 2, icons and date supressed, one column and border_thick=5 ==
 134 {{{
 135 { { {
 136 #!Gallery2 columns=1,filter=100_118[0-5],mode=2,show_date=0,show_tools=0,border_thick=5
 137 } } }
 138 }}}
 139 
 140 Result: [[BR]]
 141  {{{
 142 #!Gallery2 columns=1,filter=100_118[0-7],mode=2,show_date=0,show_tools=0,border_thick=5
 143 }}}
 144 
 145 == other macro calls ==
 146 {{{
 147 { { {
 148 #!Gallery2 only_items=1,show_date=0
 149  * [100_1189.JPG [[MiniPage(||Bremen||SpaceCenter||\n|| ||SpaceJump||)]]]
 150 } } }
 151 }}}
 152 
 153 Result: [[BR]]
 154  {{{
 155 #!Gallery2 only_items=1,show_date=0
 156  * [100_1189.JPG [[MiniPage(||Bremen||SpaceCenter||\n|| ||SpaceJump||)]]]
 157 }}}
 158 
 159 == renew means always new thumbnails and webnails of selection ==
 160 {{{
 161 { { {
 162 #!Gallery2 only_items=1,show_date=0,show_tools=0,renew=1
 163  * [100_1189.JPG [[MiniPage(||["Bremen"]||SpaceCenter||\n|| ||SpaceJump||)]]]
 164 } } }
 165 }}}
 166 
 167 Result: [[BR]]
 168  {{{
 169 #!Gallery2 only_items=1,show_date=0,renew=1
 170  * [100_1189.JPG [[MiniPage(||["Bremen"]||SpaceCenter||\n|| ||SpaceJump||)]]]
 171 }}}
 172 
 173 == template_itemlist ==
 174 {{{
 175 { { {
 176 #!Gallery2 template_itemlist=1
 177 * [100_1185.JPG Bremen, SpaceCenter]
 178 } } }
 179 }}}
 180 
 181 Result: [[BR]]
 182  {{{
 183 #!Gallery2 template_itemlist=1
 184 * [100_1185.JPG Bremen, SpaceCenter]
 185 }}}
 186 
 187 == help to show Calling Sequence ==
 188  {{{
 189 { { {
 190 #!Gallery2 help=1
 191 } } }
 192 }}}
 193 
 194 Result: [[BR]]
 195 {{{
 196 #!Gallery2 help=1
 197 }}}
 198 
 199 
 200     PROCEDURE:
 201       Download some images to a page and start with the examples.
 202       Aliasing of the filenames are done by adding an itemlist, see example.
 203 
 204       This routine requires the PIL (Python Imaging Library).
 205       And the EXIF routine from http://home.cfl.rr.com/genecash/digital_camera.html
 206 
 207 
 208       At the moment I have added the EXIF routine to the parsers dir.
 209       It's not the best place but during developing it is nice to have it there
 210       If you put it to another place you have to change the line
 211       from MoinMoin.parser import EXIF too.
 212 
 213       This routine requires the Action macro gallery2Image which is used to rotate or delete a selected image.
 214       Only users which have the rights to delete are able to execute this action macro.
 215       The icons of these are only shown if you have enough rights.
 216 
 217       The gallery2image macro does not take care on the EXIF header. This is lost by rotating.
 218       If a file is deleted by this macro it is moved to a bak file.
 219 
 220       Please remove the Version number from the code!
 221 
 222     RESTRICTIONS:
 223       The movie mode is not implemented at the moment. The implementation will be done in the action macro.
 224       
 225       
 226       If you rotate an image at the moment the exif is destroyed. PIL ignores the exif header.
 227       This is not a quite big problem normally files with an EXIF header are right rotated.
 228 
 229     Required Images:
 230       I have put them to wiki/modern/img/ dir. The icons were created by me. License: GPL
 231 
 232     attachment:to_bak.png
 233     attachment:to_left.png
 234     attachment:to_right.png
 235     attachment:to_slide.png
 236     attachment:to_full.png
 237 
 238     HISTORY:
 239     While recognizing how to write MiniPage I got the idea to write a Gallery Parser.
 240     We have used in our wikis in the past the Gallery macro of SimonRyan.
 241     I have tried to modify it a bit to change it for 1.3 but my python skills weren't enough
 242     or it was easier to write it completly new.
 243     So this one shows now a way how a Gallery could be used by the parser and an action Macro.
 244     Probably it is a good example for others who like to know how to do this
 245 
 246     MODIFICATION HISTORY:
 247         Version 1.3.3.-1
 248         @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
 249         @license: GNU GPL, see COPYING for details.
 250         2005-03-26: Version 1.3.3-2 keyword renew added
 251                     creation of thumbnails and webnails in two calls splitted
 252                     Version 1.3.3-3 bug fixed if itemlist is given to describe only some of the images
 253                                     but only_items is not set to 1
 254                                     Example code changed
 255         2005-03-27: Version 1.3.3-4 Action macro added and the form to call it. User which have rights to delete
 256                                     could use the functions of gallery2Image.
 257         2005-08-03: Version 1.3.3-5 theme path for icons corrected and a platform independent path joining
 258                                     os.unlink removed as suggested by CraigJohnson
 259                                     sort_by_name is default if not only_items is 1
 260                                     optional sort_by_date could be used
 261                                     keyword template_itemlist added 
 262                                     keyword help added
 263                                     extra frame by mode=2 removed 
 264         2005-08-06: Version 1.3.5-6 slideshow mode added
 265                                     keyword image_for_webnail added
 266         2005-08-13: Version 1.3.5-7 syntax changed from GET to POST
 267                                     forms instead of links
 268                                     filenames from images submitted to gallery2image too
 269                                     new keyword sort_by_alias
 270                                     internal code clean up
 271                                     this version needs: gallery2image-1.3.5-5.py
 272         2005-08-14: Version 1.3.5-8 (TW) cleanup                                    
 273         2005-08-14: Version 1.3.5-9 html code for tables changed
 274                                     because of the ugly extra space of form elements
 275                                     </div> tag removed so now we use the page style
 276                                     slide show action goes to right webnail now
 277                                     this version needs: gallery2image-1.3.5-5.py
 278        2005-08-17: Version 1.3.5-10 html code separated in functions     
 279                                     structure of code changed, now you see the thumbnails after creation
 280                                     bug removed if quote is given but file does not exist
 281        2005-09-02: Version 1.3.5-11 keyword album, album_name and front_image added          
 282                                     image urls changed to complete server url                          
 283                                     
 284 
 285 """
 286 Dependencies = []
 287 from MoinMoin.action import AttachFile
 288 from MoinMoin import wikiutil, config
 289 from MoinMoin.Page import Page
 290 
 291 import os,string,re,Image,StringIO
 292 
 293 from MoinMoin.parser import EXIF
 294 
 295 from MoinMoin.parser import wiki
 296 
 297 def show_tools_restricted(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):
 298     if request.user.may.delete(pagename):
 299         return tools_restricted_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request)
 300     else:
 301         return ''    
 302 
 303 def tools_restricted_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):
 304     text='''
 305                    <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 306                         <td>
 307                             <input type="hidden" name="action" value="gallery2image">
 308                             <input type="hidden" name="do" value="RL">
 309                             <input type="hidden" name="target" value="%(this_target)s">
 310                             <input type="image" value="submit" src="%(server)s/wiki/modern/img/to_left.png" title="rotate to left">
 311                         </td>
 312                     </form>
 313                     <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 314                         <td>
 315                             <input type="hidden" name="action" value="gallery2image">
 316                             <input type="hidden" name="do" value="RR">
 317                             <input type="hidden" name="target" value="%(this_target)s">
 318                             <input type="image"  value="submit" src="%(server)s/wiki/modern/img/to_right.png" title="rotate to right" >
 319                         </td>
 320                     </form>
 321                     <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 322                         <td>
 323                             <input type="hidden" name="action" value="gallery2image">
 324                             <input type="hidden" name="do" value="RM">
 325                             <input type="hidden" name="target" value="%(this_target)s">
 326                             <input type="image" value="submit" src="%(server)s/wiki/modern/img/to_bak.png" title="move to bak" >
 327                         </td>
 328                     </form>''' %  {
 329             "server" : 'http://'+request.server_name+':' + str(request.server_port), 
 330             'baseurl': request.getScriptname(),
 331             "pagename":pagename,
 332             "this_target":this_target}                   
 333     return text                
 334     
 335 def tools_html(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request):
 336     text='''
 337             <TABLE align="center" width="%(thumbnail_width)s">
 338                 <TR>
 339                     <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 340                         <td>
 341                             <input type="hidden" name="action" value="AttachFile">
 342                             <input type="hidden" name="do" value="get">
 343                             <input type="hidden" name="target" value="%(this_target)s">
 344                             <input type="image" value="submit" src="%(server)s/wiki/modern/img/to_full.png" title="load image">
 345                         </td>
 346                     </form>
 347                     <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 348                         <td>
 349                             <input type="hidden" name="action" value="gallery2image">
 350                             <input type="hidden" name="do" value="VS">
 351                             <input type="hidden" name="full" value="%(full)s">
 352                             <input type="hidden" name="alias" value="%(alias)s">
 353                             <input type="hidden" name="target" value="%(target)s">
 354                             <input type="hidden" name="exif_date" value="%(exif_date)s">
 355                             <input type="image" value="submit" src="%(server)s/wiki/modern/img/to_slide.png" title="slide_show" >
 356                        </td>
 357                     </form>
 358                     %(show_tools_restricted)s
 359                 </TR>
 360             </TABLE>'''   %  {
 361             "server" : 'http://'+request.server_name+':' + str(request.server_port), 
 362             'baseurl': request.getScriptname(),
 363             "pagename":pagename,
 364             "thumbnail_width":thumbnail_width,
 365             "full":full,
 366             "alias":alias,
 367             "exif_date":exif_date,
 368             "target":target,
 369             "this_target":this_image,
 370             "show_tools_restricted":show_tools_restricted(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request)
 371             } 
 372    
 373     return text
 374 
 375 def show_alias_mode2(show_alias,thumbnail_width,this_alias,text_width):
 376    if show_alias == '1': 
 377        return '''
 378         <td valign="top" width="%(text_width)s">
 379            %(this_alias)s
 380         </td>''' % {
 381         "this_alias":this_alias,
 382         "text_width":text_width}
 383    else:
 384       return ''     
 385         
 386 def show_date_mode2(show_date,this_exif_date):    
 387    if show_date == '1': 
 388        return '''
 389    <td>
 390      <p>%(this_exif_date)s</p>
 391    </td>''' % {
 392    "this_exif_date":this_exif_date }
 393    else:
 394         return '';
 395                
 396 def show_tools_mode2(show_tools,pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):  
 397     if show_tools == '1' :
 398         return "<td align=""center""> %s </td>" % tools_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request)
 399     else:
 400         return ''
 401       
 402 
 403 
 404 def mode2_html(pagename,border_thick,width,thumbnail_width,text_width,full,this_image,alias,this_alias,exif_date,this_exif_date,target,this_target,submit,show_tools,show_date,show_alias,request):
 405     text='''
 406     <tr valign="center">
 407         <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 408             <td align="center" valign="center" width="%(thumbnail_width)s">
 409                 <input type="hidden" name="action" value="gallery2image">
 410                 <input type="hidden" name="do" value="VS">
 411                 <input type="hidden" name="full" value="%(full)s">
 412                 <input type="hidden" name="alias" value="%(alias)s">
 413                 <input type="hidden" name="exif_date" value="%(exif_date)s">
 414                 <input type="hidden" name="target" value="%(target)s">
 415                 <input type="image" value="submit" src="%(server)s%(submit)s">
 416             </td>
 417         </form>
 418             %(alias_html)s 
 419     </tr>
 420     <tr>%(tools_html)s%(date_html)s</tr>'''%       {   
 421      "server" : 'http://'+request.server_name+':' + str(request.server_port), 
 422      "baseurl": request.getScriptname(),
 423      "pagename":pagename,
 424      "thumbnail_width":thumbnail_width,
 425      "full":full,
 426      "alias":alias,
 427      "exif_date":exif_date,
 428      "target":target,
 429      "submit":submit,
 430      "tools_html":show_tools_mode2(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request),
 431      "date_html": show_date_mode2(show_date,this_exif_date),
 432      "alias_html": show_alias_mode2(show_alias,thumbnail_width,this_alias,text_width)
 433      }
 434     
 435     return text
 436 
 437 def show_tools_mode1(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request):  
 438     if show_tools == '1' :
 439         text="<tr><td align=""center"">%s </td></tr>" % tools_html(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request)
 440     else:
 441         text=''
 442     return text        
 443 
 444 def show_date_mode1(show_date,this_exif_date):    
 445    if show_date == '1': 
 446        return '''
 447     <TR>
 448         <td>%(this_exif_date)s</td>
 449     </TR>''' % {
 450     "this_exif_date":this_exif_date}
 451    else:
 452        return ''
 453 
 454 def show_alias_mode1(show_alias,thumbnail_width,this_alias,text_width):
 455    if show_alias == '1': 
 456        return '''
 457     <TR>
 458         <td align="left" width="%(thumbnail_width)s"> %(this_alias)s</td>
 459     </TR>''' % {
 460     "thumbnail_width": thumbnail_width,
 461     "this_alias":this_alias}
 462    else:
 463        return ''     
 464        
 465 def mode1_html(pagename,border_thick,width,thumbnail_width,text_width,full,this_image,alias,this_alias,exif_date,this_exif_date,target,this_target,submit,
 466                show_tools,show_date,show_alias,request):
 467    text='''
 468 <table width="%(thumbnail_width)s" align="center" valign="center">
 469     <TR align="center" valign="center">
 470         <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 471             <td align="center" valign="middle" width="%(thumbnail_width)s">
 472                 <input type="hidden" name="action" value="gallery2image">
 473                 <input type="hidden" name="do" value="VS">
 474                 <input type="hidden" name="full" value="%(full)s">
 475                 <input type="hidden" name="alias" value="%(alias)s">
 476                 <input type="hidden" name="exif_date" value="%(exif_date)s">
 477                 <input type="hidden" name="target" value="%(target)s">
 478                 <input type="image" value="submit" src="%(server)s%(submit)s" >
 479             </td>
 480         </form>
 481     </TR>
 482       %(alias_html)s
 483       %(date_html)s
 484       %(tools_html)s
 485 </table>'''%       {  
 486      "server" : 'http://'+request.server_name+':' + str(request.server_port), 
 487      "baseurl": request.getScriptname() ,
 488      "pagename":pagename,
 489      "full":full,
 490      "alias":alias,
 491      "exif_date":exif_date,
 492      "target":target,
 493      "submit":submit,
 494      "thumbnail_width":thumbnail_width,
 495      "tools_html": show_tools_mode1(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request),  
 496      "date_html":show_date_mode1(show_date,this_exif_date),
 497      "alias_html": show_alias_mode1(show_alias,thumbnail_width,this_alias,text_width)
 498      }
 499 
 500    return text
 501 
 502 def get_files(kw,path,files,quotes,request):
 503     web=[]
 504     full=[]
 505     thumb=[]
 506     exif_date=[]
 507     img_type=[]
 508     description=[]
 509     
 510     
 511     ddict={}
 512     n=len(quotes['image'])
 513     if n > 0 :
 514         i = 0
 515         for txt in quotes['image']:
 516             ddict[txt]=quotes['alias'][i]
 517             i += 1    
 518     
 519     for attfile in files:
 520         # only files not thumb or webnails
 521         if attfile.find('thumbnail_') == -1 and attfile.find('webnail_') == -1:
 522             # only images
 523             if wikiutil.isPicture(attfile):
 524                 description.append(ddict.get(attfile, attfile))
 525                 full.append(attfile)
 526                 
 527                 if kw['image_for_webnail'] == '1':
 528                     webnail = attfile
 529                 else:
 530                     fname, ext = os.path.splitext(attfile)
 531                     if ext in ('.gif', '.png'):
 532                         img_type.append('PNG')
 533                         webnail = 'webnail_%s.png' % fname
 534                         thumbfile = 'thumbnail_%s.png' % fname
 535                     else:
 536                         img_type.append("JPEG")
 537                         webnail = 'webnail_%s.jpg' % fname
 538                         thumbfile = 'thumbnail_%s.jpg' % fname
 539   
 540                 infile = os.path.join(path, attfile)
 541                 if os.path.exists(infile):
 542                     web.append(webnail)
 543                     thumb.append(thumbfile)
 544                      
 545                 f = open(infile, 'rb')
 546                 tags = EXIF.process_file(f)
 547                 if tags.has_key('EXIF DateTimeOriginal'):
 548                     date = str(tags['EXIF DateTimeOriginal'])
 549                     date = date.replace(':', '-', 2)
 550                 else:
 551                     date = '--'
 552                 exif_date.append(date)
 553                 f.close() 
 554     
 555     return thumb,web,full,exif_date,img_type,description  
 556     
 557 def to_htmltext(text):        
 558     text = text.split(' ')
 559     text = '&nbsp;'.join(text)
 560     return text
 561 
 562 def to_wikiname(request,formatter,text):
 563   ##taken from MiniPage
 564     out=StringIO.StringIO()
 565     request.redirect(out)
 566     wikiizer = wiki.Parser(text.strip(),request)
 567     wikiizer.format(formatter)
 568     result=out.getvalue()
 569     request.redirect()
 570     del out
 571     
 572     return result.strip()
 573     
 574         
 575 def get_quotes(self,formatter):
 576     quotes = self.raw.split('\n')
 577     quotes = [quote.strip() for quote in quotes]
 578     quotes = [quote[2:] for quote in quotes if quote.startswith('* ')]
 579 
 580     
 581     image=[]
 582     text=[]
 583     
 584     for line in quotes:
 585         im, na=line[1:-1].split(' ',1)
 586         text.append(na.strip())
 587         image.append(im.strip())
 588 
 589     return {
 590         'alias': text,
 591         'image': image,
 592     }
 593 
 594 
 595 
 596 class Parser:
 597         
 598     def __init__(self, raw, request, **kw):
 599         self.raw = raw
 600         self.request = request
 601         self.form = request.form
 602         self._ = request.getText
 603         self.kw = {
 604             'sort_by_date': '0',
 605             'sort_by_name': '1',
 606             'sort_by_alias': '0',
 607             'album': '0',
 608             'album_name': 'album',
 609             'front_image':'',
 610             'template_itemlist': '0', 
 611             'reverse_sort': '0',
 612             'border_thick': '1',
 613             'columns': '4',
 614             'filter': '.',
 615             'mode': '1',
 616             'help': '0',
 617             'show_text': '1',
 618             'show_date': '1',
 619             'show_tools': '1',
 620             'only_items': '0',
 621             'image_for_webnail': '0',
 622             'renew': '0',
 623             'thumbnail_width': '128',
 624             'webnail_width': '640',
 625             'text_width': '140',
 626         }
 627        
 628 
 629         for arg in kw.get('format_args','').split(','):
 630 
 631             if arg.find('=') > -1:
 632                 key, value=arg.split('=')
 633                 self.kw[key]=wikiutil.escape(value, quote=1)
 634 
 635 
 636         self.kw['width']=str((int(self.kw['thumbnail_width'])+int(self.kw['text_width'])))
 637 
 638 
 639     def format(self, formatter):
 640         kw=self.kw
 641         Dict = {}
 642         quotes=get_quotes(self,formatter)
 643         current_pagename=formatter.page.page_name
 644         attachment_path = AttachFile.getAttachDir(self.request, current_pagename, create=1)
 645  
 646         if kw['help'] == '1':
 647             self.request.write('''
 648 <br>
 649 {{{<br>
 650 #!Gallery2 [columns=columns],[filter=filter],[mode=mode],<br>
 651            [show_text=show_text],[show_date=show_date], [show_tools=show_tools],<br>
 652            [sort_by_name=sort_by_name],[sort_by_date=sort_by_date],[sort_by_alias=sort_by_alias]<br>
 653            [reverse_sort=reverse_sort],<br>
 654            [only_items=only_items],[template_itemlist=template_itemlist],<br>
 655            [album=album],[album_name=album_name],[front_image=front_image],<br>
 656            [thumbnail_width=thumbnail_width],[webnail_width=webnail_width],[text_width=text_width],<br>
 657            [image_for_webnail=image_for_webnail],<br>
 658            [border_thick=border_thick],[renew=renew],[help=help]<br>
 659  * [image1.jpg alias]<br>
 660  * [image2.jpg alias]<br>
 661 }}}<br>''')
 662             return    
 663             
 664         
 665         if kw['only_items'] == '1':
 666             all_files=quotes['image']
 667             result=[]
 668             for attfile in all_files:
 669                 infile=os.path.join(attachment_path,attfile)
 670                 if os.path.exists(infile):
 671                    result.append(attfile) 
 672             all_files = result        
 673             
 674             if kw['sort_by_alias'] == '1':
 675                 new_ordered_files=[]
 676                 alias_text=quotes['alias']
 677                 
 678                 i=0
 679                 for attfile in all_files:
 680                     infile=os.path.join(attachment_path,attfile)
 681                     ft_file=str(os.path.getmtime(infile))+os.tmpnam()
 682                     Dict[alias_text[i]]=attfile
 683                     i += 1
 684        
 685                 keys = Dict.keys()   
 686                 keys.sort()
 687                 for txt in keys:
 688                     new_ordered_files.append(Dict[txt])
 689                    
 690 
 691                 all_files=new_ordered_files
 692                 Dict.clear()
 693            
 694         else:
 695             all_files=os.listdir(attachment_path)
 696 
 697         result = []
 698 
 699         for test in all_files:
 700            if re.match(kw['filter'], test):
 701               result.append(test)
 702         all_files=result
 703         
 704         if not all_files:
 705             self.request.write("<br><br><h1>No matching image file found!</h1>")
 706             return
 707             
 708         
 709 
 710         if kw['sort_by_name'] == '1' and kw['only_items'] == '0': 
 711             all_files.sort()
 712           
 713         if kw['sort_by_date']=='1': 
 714            for attfile in all_files:
 715                infile=os.path.join(attachment_path,attfile)
 716                ft_file=str(os.path.getmtime(infile))+os.tmpnam()
 717                Dict[ft_file]=attfile
 718                
 719            keys = Dict.keys()
 720            keys.sort()
 721            file_mdate=[]
 722            for txt in keys:
 723                file_mdate.append(Dict[txt])
 724            all_files=file_mdate
 725            Dict.clear()
 726          
 727         if kw['reverse_sort']=='1': 
 728              all_files.reverse()   
 729 
 730  
 731         cells=[]
 732         cell_name=[]
 733         img=[]
 734       
 735         thumb, web, full, exif_date, imgtype, description = get_files(kw, attachment_path, all_files, quotes, self.request)
 736         
 737         if kw['template_itemlist'] == '1':
 738             self.request.write('Copy the following listitems into the script. Replace alias with the label you want. Afterwards disable template_itemlist by setting it to 0:<BR>')
 739             for attfile in full : 
 740                 self.request.write(' * [%(attfile)s %(date)s]<br>' % {
 741                                    'attfile' : attfile,
 742                                    'date'    : 'alias'
 743                                     })
 744                                     
 745         
 746         i = 0  
 747         z = 1
 748         cols = int(kw['columns'])
 749         
 750            
 751         n = len(full)
 752         if  kw['album'] == '0' :
 753             self.request.write("<table align='center' border='%s' >" % self.kw['border_thick'])
 754             if kw['mode'] == '1' or cols > 1   :
 755                 self.request.write('<TR valign="bottom">')      
 756                 self.request.write('<TD>') 
 757  
 758                            
 759         if kw['album'] == '1' :
 760             if kw['front_image'] == '' :
 761                 front_image = full[0]  
 762             else:
 763                 front_image =  kw['front_image']   
 764             ii = 0
 765             for tst in full : 
 766                 if tst == front_image :
 767                     break 
 768                 ii += 1   
 769             
 770         
 771         for attfile in full :
 772             if  kw['album'] == '1' :
 773                 if tst == front_image :
 774                    i = ii
 775              
 776                                  
 777             this_description=description[i]
 778             this_exif_date=exif_date[i]
 779             this_webnail=web[i]
 780             this_imgtype=imgtype[i]
 781             this_thumbfile=thumb[i]
 782             
 783            
 784             thumbf=os.path.join(attachment_path,this_thumbfile)
 785             webf=os.path.join(attachment_path,this_webnail)            
 786             
 787             
 788             if kw['renew'] == '1':
 789                 if os.path.exists(thumbf):
 790                    os.unlink(thumbf)
 791                 if os.path.exists(webf):
 792                    os.unlink(webf)
 793                    
 794             if not os.path.exists(webf) or not os.path.exists(thumbf):
 795                 infile=os.path.join(attachment_path,attfile)
 796                 im = Image.open(infile)
 797                 if not os.path.exists(webf):
 798                     im.thumbnail(((int(kw['webnail_width'])),((int(kw['webnail_width'])))), Image.ANTIALIAS)
 799                     im.save(webf, this_imgtype)
 800                 if not os.path.exists(thumbf):
 801                     im.thumbnail(((int(kw['thumbnail_width'])),((int(kw['thumbnail_width'])))),
 802                                    Image.ANTIALIAS)
 803                     im.save(thumbf, this_imgtype)       
 804                     
 805             
 806             if kw['mode'] == '1':
 807                 text=mode1_html(current_pagename,
 808                         kw['border_thick'],
 809                         kw['width'],
 810                         kw['thumbnail_width'],
 811                         kw['text_width'],
 812                         attfile + "," + ','.join(full),
 813                         attfile,
 814                         to_htmltext(this_description + "!,!" + '!,!'.join(description)),
 815                         to_wikiname(self.request,formatter,this_description),
 816                         to_htmltext(this_exif_date + "," + ','.join(exif_date)),
 817                         this_exif_date,
 818                         this_webnail + "," + ','.join(web),
 819                         this_webnail,
 820                         AttachFile.getAttachUrl(current_pagename, this_thumbfile, self.request),
 821                         kw['show_tools'],
 822                         kw['show_date'],
 823                         kw['show_text'],
 824                         self.request
 825                         )
 826                 self.request.write(''.join(text))    
 827                         
 828             if kw['mode'] == '2':
 829                 text=mode2_html(current_pagename,
 830                         kw['border_thick'],
 831                         kw['width'],
 832                         kw['thumbnail_width'],
 833                         kw['text_width'],
 834                         attfile + "," + ','.join(full),
 835                         attfile,
 836                         to_htmltext(this_description + "!,!" + '!,!'.join(description)),
 837                         to_wikiname(self.request,formatter,this_description),
 838                         to_htmltext(this_exif_date + "," + ','.join(exif_date)),
 839                         this_exif_date,
 840                         this_webnail + "," + ','.join(web),
 841                         this_webnail,
 842                         AttachFile.getAttachUrl(current_pagename, this_thumbfile, self.request),
 843                         kw['show_tools'],
 844                         kw['show_date'],
 845                         kw['show_text'],
 846                         self.request
 847                         )
 848             
 849                 if cols > 1 : self.request.write('<table valign="bottom">')
 850                 self.request.write(''.join(text))
 851                 if cols > 1 : self.request.write('</table>')
 852             
 853             if kw['mode'] == '1' or cols > 1:    
 854                 if kw['album'] == '0' :
 855                     if  z < cols:
 856                         self.request.write('</TD>')
 857                         if z <  n :
 858                             self.request.write('<TD>')
 859                     else: 
 860                         self.request.write('</TD>')
 861                         self.request.write('</TR>')
 862                         if i < n-1 :
 863                             self.request.write('<TR valign="bottom">')
 864                             self.request.write('<TD>')
 865                 
 866             i += 1         
 867             z += 1
 868             if z > cols :
 869                 z = 1
 870             
 871             if kw['album'] == '1' :
 872                 self.request.write("%(n)s images (%(album_name)s)" % {"n": str(n), "album_name":kw['album_name']})
 873                 break
 874         if kw['album'] == '0' :        
 875             if i < n :
 876                 self.request.write('</TD>')
 877                 self.request.write('</TR>')
 878             
 879         
 880             self.request.write('</table>')       
 881             
 882         ############################################
 883         ##TODO: syntax change to formatter - later #
 884         ############################################
 885         
 886        
 887         
 888         
 889        

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.