Mercurial > hg > documentViewer
comparison documentViewer.py @ 494:ecdb238aa595 roc_1
added retry when reading index meta from texter applet
author | casties |
---|---|
date | Tue, 13 Jun 2006 16:57:46 +0200 |
parents | 96f74b2bab24 |
children |
comparison
equal
deleted
inserted
replaced
1:312446f900da | 494:ecdb238aa595 |
---|---|
1 | |
2 genericDigilib="http://nausikaa2.rz-berlin.mpg.de/digitallibrary/" | |
3 | |
4 from OFS.Folder import Folder | 1 from OFS.Folder import Folder |
5 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate | 2 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate |
6 from Products.PageTemplates.PageTemplateFile import PageTemplateFile | 3 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
7 from AccessControl import ClassSecurityInfo | 4 from AccessControl import ClassSecurityInfo |
5 from AccessControl import getSecurityManager | |
8 from Globals import package_home | 6 from Globals import package_home |
9 | 7 |
10 from Ft.Xml.Domlette import NonvalidatingReader | 8 from Ft.Xml.Domlette import NonvalidatingReader |
11 from Ft.Xml.Domlette import PrettyPrint, Print | 9 from Ft.Xml.Domlette import PrettyPrint, Print |
12 from Ft.Xml import EMPTY_NAMESPACE | 10 from Ft.Xml import EMPTY_NAMESPACE |
13 | 11 |
14 import Ft.Xml.XPath | 12 import Ft.Xml.XPath |
15 | 13 |
16 import os.path | 14 import os.path |
15 import sys | |
17 import cgi | 16 import cgi |
18 import urllib | 17 import urllib |
18 import zLOG | |
19 | |
20 def getInt(number, default=0): | |
21 """returns always an int (0 in case of problems)""" | |
22 try: | |
23 return int(number) | |
24 except: | |
25 return default | |
19 | 26 |
20 def getTextFromNode(nodename): | 27 def getTextFromNode(nodename): |
28 if nodename is None: | |
29 return "" | |
21 nodelist=nodename.childNodes | 30 nodelist=nodename.childNodes |
22 rc = "" | 31 rc = "" |
23 for node in nodelist: | 32 for node in nodelist: |
24 if node.nodeType == node.TEXT_NODE: | 33 if node.nodeType == node.TEXT_NODE: |
25 rc = rc + node.data | 34 rc = rc + node.data |
26 return rc | 35 return rc |
27 | 36 |
37 | |
38 def getParentDir(path): | |
39 """returns pathname shortened by one""" | |
40 return '/'.join(path.split('/')[0:-1]) | |
41 | |
42 | |
28 import socket | 43 import socket |
29 | 44 |
30 def urlopen(url): | 45 def urlopen(url,timeout=2): |
31 """urlopen mit timeout""" | 46 """urlopen mit timeout""" |
32 socket.setdefaulttimeout(2) | 47 socket.setdefaulttimeout(timeout) |
33 ret=urllib.urlopen(url) | 48 ret=urllib.urlopen(url) |
34 socket.setdefaulttimeout(5) | 49 socket.setdefaulttimeout(5) |
35 return ret | 50 return ret |
36 | 51 |
37 def getParamFromDigilib(path,param): | 52 |
38 """gibt param von dlInfo aus""" | 53 ## |
39 imageUrl=genericDigilib+"/dlInfo-xml.jsp?fn="+path | 54 ## documentViewer class |
40 | 55 ## |
41 try: | 56 class documentViewer(Folder): |
42 dom = NonvalidatingReader.parseUri(imageUrl) | |
43 except: | |
44 return None | |
45 | |
46 | |
47 params=dom.xpath("//document-parameters/parameter[@name='%s']/@value"%param) | |
48 | |
49 if params: | |
50 return params[0].value | |
51 | |
52 def parseUrlTextTool(url): | |
53 """parse index meta""" | |
54 | |
55 try: | |
56 dom = NonvalidatingReader.parseUri(url) | |
57 except: | |
58 zLOG.LOG("documentViewer (parseUrlTexttool)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2]) | |
59 return (None,None,None) | |
60 | |
61 archivePaths=dom.xpath("//resource/archive-path") | |
62 | |
63 if archivePaths and (len(archivePaths)>0): | |
64 archivePath=getTextFromNode(archivePaths[0]) | |
65 else: | |
66 archivePath=None | |
67 | |
68 | |
69 images=dom.xpath("//texttool/image") | |
70 | |
71 if images and (len(images)>0): | |
72 image=getTextFromNode(images[0]) | |
73 else: | |
74 image=None | |
75 | |
76 if image and archivePath: | |
77 image=os.path.join(archivePath,image) | |
78 image=image.replace("/mpiwg/online",'') | |
79 pt=getParamFromDigilib(image,'pt') | |
80 | |
81 else: | |
82 image=None | |
83 | |
84 viewerUrls=dom.xpath("//texttool/digiliburlprefix") | |
85 | |
86 if viewerUrls and (len(viewerUrls)>0): | |
87 viewerUrl=getTextFromNode(viewerUrls[0]) | |
88 else: | |
89 viewerUrl=None | |
90 | |
91 | |
92 textUrls=dom.xpath("//texttool/text") | |
93 | |
94 if textUrls and (len(textUrls)>0): | |
95 textUrl=getTextFromNode(textUrls[0]) | |
96 else: | |
97 textUrl=None | |
98 return viewerUrl,(image,pt),textUrl | |
99 | |
100 | |
101 class documentViewer(ZopePageTemplate): | |
102 """document viewer""" | 57 """document viewer""" |
103 | 58 |
104 meta_type="Document viewer" | 59 meta_type="Document viewer" |
105 | 60 |
106 security=ClassSecurityInfo() | 61 security=ClassSecurityInfo() |
107 manage_options=ZopePageTemplate.manage_options+( | 62 manage_options=Folder.manage_options+( |
108 {'label':'main config','action':'changeDocumentViewerForm'}, | 63 {'label':'main config','action':'changeDocumentViewerForm'}, |
109 ) | 64 ) |
110 | 65 |
111 _default_content_fn = os.path.join(package_home(globals()),'zpt','documentViewer_template.zpt') | 66 # templates and forms |
112 | 67 viewer_main = PageTemplateFile('zpt/viewer_main', globals()) |
113 def __init__(self,id,imageViewerUrl,title=""): | 68 thumbs_main = PageTemplateFile('zpt/thumbs_main', globals()) |
69 image_main = PageTemplateFile('zpt/image_main', globals()) | |
70 head_main = PageTemplateFile('zpt/head_main', globals()) | |
71 docuviewer_css = PageTemplateFile('css/docuviewer.css', globals()) | |
72 | |
73 security.declareProtected('View management screens','changeDocumentViewerForm') | |
74 changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals()) | |
75 | |
76 | |
77 def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10,authgroups="mpiwg"): | |
114 """init document viewer""" | 78 """init document viewer""" |
115 self.id=id | 79 self.id=id |
116 self.title=title | 80 self.title=title |
117 self.imageViewerUrl=imageViewerUrl | 81 self.imageViewerUrl=imageViewerUrl |
118 | 82 if not digilibBaseUrl: |
119 security.declareProtected('View management screens','changeDocumentViewerForm') | 83 self.digilibBaseUrl = self.findDigilibUrl() |
120 def changeDocumentViewerForm(self): | 84 else: |
121 """change it""" | 85 self.digilibBaseUrl = digilibBaseUrl |
122 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeDocumentViewer.zpt')).__of__(self) | 86 self.thumbcols = thumbcols |
123 return pt() | 87 self.thumbrows = thumbrows |
88 # authgroups is list of authorized groups (delimited by ,) | |
89 self.authgroups = [s.strip().lower() for s in authgroups.split(',')] | |
90 # add template folder so we can always use template.something | |
91 self.manage_addFolder('template') | |
92 | |
93 | |
94 security.declareProtected('View','index_html') | |
95 def index_html(self,mode,url,start=None,pn=1): | |
96 ''' | |
97 view it | |
98 @param mode: defines which type of document is behind url | |
99 @param url: url which contains display information | |
100 ''' | |
101 | |
102 zLOG.LOG("documentViewer (index)", zLOG.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) | |
103 | |
104 if not hasattr(self, 'template'): | |
105 # create template folder if it doesn't exist | |
106 self.manage_addFolder('template') | |
107 | |
108 if not self.digilibBaseUrl: | |
109 self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary" | |
110 | |
111 docinfo = self.getDocinfo(mode=mode,url=url) | |
112 pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo) | |
113 pt = getattr(self.template, 'viewer_main') | |
114 return pt(docinfo=docinfo,pageinfo=pageinfo) | |
124 | 115 |
125 | 116 |
126 def changeDocumentViewer(self,imageViewerUrl,title="",RESPONSE=None): | 117 def getLink(self,param=None,val=None): |
127 """init document viewer""" | 118 """link to documentviewer with parameter param set to val""" |
128 self.title=title | 119 params=self.REQUEST.form.copy() |
129 self.imageViewerUrl=imageViewerUrl | 120 if param is not None: |
130 | 121 if val is None: |
131 if RESPONSE is not None: | 122 if params.has_key(param): |
132 RESPONSE.redirect('manage_main') | 123 del params[param] |
133 | 124 else: |
134 | 125 params[param] = str(val) |
135 def imageLink(self,nr): | 126 |
136 """link hinter den images""" | 127 # quote values and assemble into query string |
137 paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING']) | 128 ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()]) |
138 params={} | 129 url=self.REQUEST['URL1']+"?"+ps |
139 for x in paramsTmp.iteritems(): | 130 return url |
140 params[x[0]]=x[1][0] | 131 |
141 | 132 |
142 params['pn']=nr | 133 def getStyle(self, idx, selected, style=""): |
143 newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) | 134 """returns a string with the given style and append 'sel' if path == selected.""" |
144 return newUrl | 135 #zLOG.LOG("documentViewer (getstyle)", zLOG.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style)) |
145 | 136 if idx == selected: |
146 | 137 return style + 'sel' |
147 def thumbruler(self,cols,rows,start,maximum): | 138 else: |
148 """ruler for thumbs""" | 139 return style |
149 ret="" | 140 |
150 paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING']) | 141 |
151 params={} | 142 def isAccessible(self, docinfo): |
152 for x in paramsTmp.iteritems(): | 143 """returns if access to the resource is granted""" |
153 | 144 access = docinfo.get('accessType', None) |
154 if not x[0]=="start": | 145 if access is None: |
155 params[x[0]]=x[1][0] | 146 # no information - no access |
156 | 147 #TODO: check |
157 newUrlSelect=self.REQUEST['URL']+"?"+urllib.urlencode(params) | 148 return True |
158 if start>0: | 149 elif access == 'free': |
159 newStart=max(start-cols*rows,0) | 150 return True |
160 params['start']=newStart | 151 elif access in self.authgroups: |
161 newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) | 152 # only local access -- only logged in users |
162 ret+="""<a href="%s">prev</a>"""%newUrl | 153 user = getSecurityManager().getUser() |
163 | 154 if user is not None: |
164 | 155 #print "user: ", user |
165 ret+="""<select onChange="location.href='%s&start='+this.options[this.selectedIndex].value" """%newUrlSelect | 156 return (user.getUserName() != "Anonymous User") |
166 nr,rest=divmod(maximum,cols*rows) | 157 else: |
167 if rest > 0: | 158 return False |
168 nr+=1 | 159 |
169 for i in range(nr): | 160 zLOG.LOG("documentViewer (accessOK)", zLOG.INFO, "unknown access type %s"%access) |
170 nr=i*cols*rows | 161 return False |
162 | |
163 | |
164 def getDirinfoFromDigilib(self,path,docinfo=None): | |
165 """gibt param von dlInfo aus""" | |
166 if docinfo is None: | |
167 docinfo = {} | |
168 | |
169 imageUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path | |
170 | |
171 zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo from %s"%(imageUrl)) | |
172 | |
173 for cnt in (1,2,3): | |
174 try: | |
175 dom = NonvalidatingReader.parseUri(imageUrl) | |
176 break | |
177 except: | |
178 zLOG.LOG("documentViewer (getdirinfofromdigilib)", zLOG.ERROR, "error reading %s (try %d)"%(imageUrl,cnt)) | |
179 else: | |
180 raise IOError("Unable to get dir-info from %s"%(imageUrl)) | |
181 | |
182 sizes=dom.xpath("//dir/size") | |
183 zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo:size"%sizes) | |
184 | |
185 if sizes: | |
186 docinfo['numPages'] = int(getTextFromNode(sizes[0])) | |
187 else: | |
188 docinfo['numPages'] = 0 | |
189 | |
190 return docinfo | |
191 | |
192 | |
193 def getIndexMeta(self, url): | |
194 """returns dom of index.meta document at url""" | |
195 num_retries = 3 | |
196 dom = None | |
197 metaUrl = None | |
198 if url.startswith("http://"): | |
199 # real URL | |
200 metaUrl = url | |
201 try: | |
202 dom = NonvalidatingReader.parseUri(url) | |
203 except: | |
204 zLOG.LOG("documentViewer (getIndexMata)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2]) | |
205 raise IOError("Unable to read index.meta from %s"%(url)) | |
206 else: | |
207 # online path | |
208 server=self.digilibBaseUrl+"/servlet/Texter?fn=" | |
209 metaUrl=server+url | |
210 if not metaUrl.endswith("index.meta"): | |
211 metaUrl += "/index.meta" | |
212 | |
213 for n in range(num_retries): | |
214 try: | |
215 dom = NonvalidatingReader.parseUri(metaUrl) | |
216 zLOG.LOG("documentViewer (getIndexMata)", zLOG.INFO,metaUrl) | |
217 break | |
218 except: | |
219 zLOG.LOG("ERROR documentViewer (getIndexMata)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2]) | |
220 | |
221 if dom is None: | |
222 raise IOError("Unable to read index meta from %s"%(url)) | |
223 | |
224 return dom | |
225 | |
226 | |
227 def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None): | |
228 """gets authorization info from the index.meta file at path or given by dom""" | |
229 zLOG.LOG("documentViewer (getauthinfofromindexmeta)", zLOG.INFO,"path: %s"%(path)) | |
230 | |
231 access = None | |
232 | |
233 if docinfo is None: | |
234 docinfo = {} | |
235 | |
236 if dom is None: | |
237 dom = self.getIndexMeta(getParentDir(path)) | |
238 | |
239 acctype = dom.xpath("//access-conditions/access/@type") | |
240 if acctype and (len(acctype)>0): | |
241 access=acctype[0].value | |
242 if access in ['group', 'institution']: | |
243 access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower() | |
244 | |
245 docinfo['accessType'] = access | |
246 return docinfo | |
247 | |
248 | |
249 def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None): | |
250 """gets bibliographical info from the index.meta file at path or given by dom""" | |
251 zLOG.LOG("documentViewer (getbibinfofromindexmeta)", zLOG.INFO,"path: %s"%(path)) | |
252 | |
253 if docinfo is None: | |
254 docinfo = {} | |
255 | |
256 if dom is None: | |
257 dom = self.getIndexMeta(getParentDir(path)) | |
258 | |
259 metaData=self.metadata.main.meta.bib | |
260 bibtype=dom.xpath("//bib/@type") | |
261 if bibtype and (len(bibtype)>0): | |
262 bibtype=bibtype[0].value | |
263 else: | |
264 bibtype="generic" | |
265 bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC) | |
266 bibmap=metaData.generateMappingForType(bibtype) | |
267 #print "bibmap: ", bibmap, " for: ", bibtype | |
268 # if there is no mapping bibmap is empty (mapping sometimes has empty fields) | |
269 if len(bibmap) > 0 and len(bibmap['author'][0]) > 0: | |
270 docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0]) | |
271 docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0]) | |
272 docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0]) | |
273 | |
274 return docinfo | |
275 | |
276 | |
277 def getDocinfoFromTextTool(self,url,dom=None,docinfo=None): | |
278 """parse texttool tag in index meta""" | |
279 zLOG.LOG("documentViewer (getdocinfofromtexttool)", zLOG.INFO,"url: %s"%(url)) | |
280 if docinfo is None: | |
281 docinfo = {} | |
171 | 282 |
172 if (start >= nr) and (start < nr+cols*rows): | 283 if dom is None: |
173 ret+="""<option value="%s" selected>%s</option>"""%(nr,nr) | 284 dom = self.getIndexMeta(url) |
174 else: | 285 |
175 ret+="""<option value="%s">%s</option>"""%(nr,nr) | 286 archiveNames=dom.xpath("//resource/name") |
176 ret+="</select>" | 287 if archiveNames and (len(archiveNames)>0): |
177 | 288 archiveName=getTextFromNode(archiveNames[0]) |
178 if start<maximum: | 289 |
179 newStart=min(start+cols*rows,maximum) | 290 archivePaths=dom.xpath("//resource/archive-path") |
180 params['start']=newStart | 291 if archivePaths and (len(archivePaths)>0): |
181 newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) | 292 archivePath=getTextFromNode(archivePaths[0]) |
182 ret+="""<a href="%s">next</a>"""%newUrl | 293 # clean up archive path |
183 | 294 if archivePath[0] != '/': |
184 return ret | 295 archivePath = '/' + archivePath |
185 | 296 if not archivePath.endswith(archiveName): |
186 def textToolThumb(self,url,start=0): | 297 archivePath += "/" + archiveName |
187 """understands the texttool format | 298 else: |
188 @param url: url to index.meta with texttool tag | 299 archivePath=None |
189 """ | 300 |
190 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | 301 imageDirs=dom.xpath("//texttool/image") |
191 | 302 if imageDirs and (len(imageDirs)>0): |
192 imageUrl=genericDigilib+"/servlet/Scaler?fn=%s"%imagepath[0] | 303 imageDir=getTextFromNode(imageDirs[0]) |
193 | 304 else: |
194 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','thumbs.zpt')).__of__(self) | 305 # we balk with no image tag |
195 return pt(imageUrl=imageUrl,pt=imagepath[1],start=start) | 306 raise IOError("No text-tool info in %s"%(url)) |
196 | 307 |
308 if imageDir and archivePath: | |
309 #print "image: ", imageDir, " archivepath: ", archivePath | |
310 imageDir=os.path.join(archivePath,imageDir) | |
311 imageDir=imageDir.replace("/mpiwg/online",'') | |
312 docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo) | |
313 docinfo['imagePath'] = imageDir | |
314 docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir | |
315 | |
316 viewerUrls=dom.xpath("//texttool/digiliburlprefix") | |
317 if viewerUrls and (len(viewerUrls)>0): | |
318 viewerUrl=getTextFromNode(viewerUrls[0]) | |
319 docinfo['viewerURL'] = viewerUrl | |
320 | |
321 textUrls=dom.xpath("//texttool/text") | |
322 if textUrls and (len(textUrls)>0): | |
323 textUrl=getTextFromNode(textUrls[0]) | |
324 docinfo['textURL'] = textUrl | |
325 | |
326 docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom) | |
327 docinfo = self.getAuthinfoFromIndexMeta(url,docinfo=docinfo,dom=dom) | |
328 return docinfo | |
329 | |
330 | |
331 def getDocinfoFromImagePath(self,path,docinfo=None): | |
332 """path ist the path to the images it assumes that the index.meta file is one level higher.""" | |
333 zLOG.LOG("documentViewer (getdocinfofromimagepath)", zLOG.INFO,"path: %s"%(path)) | |
334 if docinfo is None: | |
335 docinfo = {} | |
336 path=path.replace("/mpiwg/online","") | |
337 docinfo['imagePath'] = path | |
338 docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo) | |
339 imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path | |
340 docinfo['imageURL'] = imageUrl | |
341 | |
342 docinfo = self.getBibinfoFromIndexMeta(path,docinfo=docinfo) | |
343 docinfo = self.getAuthinfoFromIndexMeta(path,docinfo=docinfo) | |
344 return docinfo | |
345 | |
346 | |
347 def getDocinfo(self, mode, url): | |
348 """returns docinfo depending on mode""" | |
349 zLOG.LOG("documentViewer (getdocinfo)", zLOG.INFO,"mode: %s, url: %s"%(mode,url)) | |
350 # look for cached docinfo in session | |
351 if self.REQUEST.SESSION.has_key('docinfo'): | |
352 docinfo = self.REQUEST.SESSION['docinfo'] | |
353 # check if its still current | |
354 if docinfo is not None and docinfo.get('mode') == mode and docinfo.get('url') == url: | |
355 zLOG.LOG("documentViewer (getdocinfo)", zLOG.INFO,"docinfo in session: %s"%docinfo) | |
356 return docinfo | |
357 # new docinfo | |
358 docinfo = {'mode': mode, 'url': url} | |
359 if mode=="texttool": #index.meta with texttool information | |
360 docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo) | |
361 elif mode=="imagepath": | |
362 docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo) | |
363 else: | |
364 zLOG.LOG("documentViewer (getdocinfo)", zLOG.ERROR,"unknown mode!") | |
365 raise ValueError("Unknown mode %s"%(mode)) | |
366 | |
367 zLOG.LOG("documentViewer (getdocinfo)", zLOG.INFO,"docinfo: %s"%docinfo) | |
368 self.REQUEST.SESSION['docinfo'] = docinfo | |
369 return docinfo | |
370 | |
371 | |
372 def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None): | |
373 """returns pageinfo with the given parameters""" | |
374 pageinfo = {} | |
375 current = getInt(current) | |
376 pageinfo['current'] = current | |
377 rows = int(rows or self.thumbrows) | |
378 pageinfo['rows'] = rows | |
379 cols = int(cols or self.thumbcols) | |
380 pageinfo['cols'] = cols | |
381 grpsize = cols * rows | |
382 pageinfo['groupsize'] = grpsize | |
383 start = getInt(start, default=(int(current / grpsize) * grpsize +1)) | |
384 pageinfo['start'] = start | |
385 pageinfo['end'] = start + grpsize | |
386 if docinfo is not None: | |
387 np = int(docinfo['numPages']) | |
388 pageinfo['end'] = min(pageinfo['end'], np) | |
389 pageinfo['numgroups'] = int(np / grpsize) | |
390 if np % grpsize > 0: | |
391 pageinfo['numgroups'] += 1 | |
392 | |
393 return pageinfo | |
394 | |
197 def text(self,mode,url,pn): | 395 def text(self,mode,url,pn): |
198 """give text""" | 396 """give text""" |
199 if mode=="texttool": #index.meta with texttool information | 397 if mode=="texttool": #index.meta with texttool information |
200 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | 398 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) |
201 | 399 |
202 print textpath | 400 #print textpath |
203 try: | 401 try: |
204 dom = NonvalidatingReader.parseUri(textpath) | 402 dom = NonvalidatingReader.parseUri(textpath) |
205 except: | 403 except: |
206 return None | 404 return None |
207 | 405 |
232 list.append(next) | 430 list.append(next) |
233 next=next.nextSibling | 431 next=next.nextSibling |
234 list.append(endNode.parentNode) | 432 list.append(endNode.parentNode) |
235 | 433 |
236 if p==e:# beide im selben paragraphen | 434 if p==e:# beide im selben paragraphen |
237 | 435 pass |
238 else: | 436 # else: |
239 next=p | 437 # next=p |
240 while next!=e: | 438 # while next!=e: |
241 print next,e | 439 # print next,e |
242 list.append(next) | 440 # list.append(next) |
243 next=next.nextSibling | 441 # next=next.nextSibling |
244 | 442 # |
245 for x in list: | 443 # for x in list: |
246 PrettyPrint(x) | 444 # PrettyPrint(x) |
247 | 445 # |
248 return list | 446 # return list |
249 | 447 # |
250 def image(self,mode,url,pn): | 448 |
251 """give image out""" | 449 def findDigilibUrl(self): |
252 if mode=="texttool": #index.meta with texttool information | 450 """try to get the digilib URL from zogilib""" |
253 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | 451 url = self.imageViewerUrl[:-1] + "/getScalerUrl" |
254 url=viewerUrl+"pn=%s&fn=%s"%(pn,imagepath[0]) | 452 try: |
255 ret="""<iframe height="100%%" width="100%%" src="%s"/>"""%url | 453 scaler = urlopen(url).read() |
256 return url | 454 return scaler.replace("/servlet/Scaler?", "") |
257 | 455 except: |
258 def thumbs(self,mode,url,start): | 456 return None |
259 """give thumbs out""" | 457 |
260 if mode=="texttool": #index.meta with texttool information | 458 def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None): |
261 return self.textToolThumb(url,int(start)) | 459 """init document viewer""" |
262 | 460 self.title=title |
263 security.declareProtected('View','index_html') | 461 self.imageViewerUrl=imageViewerUrl |
264 | 462 self.digilibBaseUrl = digilibBaseUrl |
265 | 463 self.thumbrows = thumbrows |
266 def index_html(self,mode,url,start=0,pn=0): | 464 self.thumbcols = thumbcols |
267 ''' | 465 self.authgroups = [s.strip().lower() for s in authgroups.split(',')] |
268 view it | 466 if RESPONSE is not None: |
269 @param mode: defines which type of document is behind url | 467 RESPONSE.redirect('manage_main') |
270 @param url: url which contains display information | 468 |
271 ''' | 469 |
272 | |
273 | |
274 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','documentViewer_template.zpt')).__of__(self) | |
275 return pt(mode=mode,url=url,start=start,pn=pn) | |
276 | |
277 | 470 |
278 | 471 |
279 # security.declareProtected('View management screens','renameImageForm') | 472 # security.declareProtected('View management screens','renameImageForm') |
280 | 473 |
281 def manage_AddDocumentViewerForm(self): | 474 def manage_AddDocumentViewerForm(self): |
282 """add the viewer form""" | 475 """add the viewer form""" |
283 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addDocumentViewer.zpt')).__of__(self) | 476 pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self) |
284 return pt() | 477 return pt() |
285 | 478 |
286 def manage_AddDocumentViewer(self,id,imageViewerUrl="",title="",RESPONSE=None): | 479 def manage_AddDocumentViewer(self,id,imageViewerUrl="",title="",RESPONSE=None): |
287 """add the viewer""" | 480 """add the viewer""" |
288 newObj=documentViewer(id,imageViewerUrl,title) | 481 newObj=documentViewer(id,imageViewerUrl,title) |
289 self._setObject(id,newObj) | 482 self._setObject(id,newObj) |
290 | 483 |
291 if RESPONSE is not None: | 484 if RESPONSE is not None: |
292 RESPONSE.redirect('manage_main') | 485 RESPONSE.redirect('manage_main') |
293 | 486 |
487 | |
488 ## | |
489 ## DocumentViewerTemplate class | |
490 ## | |
491 class DocumentViewerTemplate(ZopePageTemplate): | |
492 """Template for document viewer""" | |
493 meta_type="DocumentViewer Template" | |
494 | |
495 | |
496 def manage_addDocumentViewerTemplateForm(self): | |
497 """Form for adding""" | |
498 pt=PageTemplateFile('zpt/addDocumentViewerTemplate', globals()).__of__(self) | |
499 return pt() | |
500 | |
501 def manage_addDocumentViewerTemplate(self, id='viewer_main', title=None, text=None, | |
502 REQUEST=None, submit=None): | |
503 "Add a Page Template with optional file content." | |
504 | |
505 self._setObject(id, DocumentViewerTemplate(id)) | |
506 ob = getattr(self, id) | |
507 ob.pt_edit(open(os.path.join(package_home(globals()),'zpt/viewer_main.zpt')).read(),None) | |
508 if title: | |
509 ob.pt_setTitle(title) | |
510 try: | |
511 u = self.DestinationURL() | |
512 except AttributeError: | |
513 u = REQUEST['URL1'] | |
514 | |
515 u = "%s/%s" % (u, urllib.quote(id)) | |
516 REQUEST.RESPONSE.redirect(u+'/manage_main') | |
517 return '' | |
518 | |
519 | |
520 |