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

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.