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