Mercurial > hg > documentViewer
annotate documentViewer.py @ 73:0f534c12cc9e
minor
author | dwinter |
---|---|
date | Tue, 04 Nov 2008 21:23:49 +0100 |
parents | 6412c45dff58 |
children | 5c9837484085 |
rev | line source |
---|---|
46 | 1 |
2 | |
0 | 3 from OFS.Folder import Folder |
4 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate | |
52 | 5 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
0 | 6 from AccessControl import ClassSecurityInfo |
32 | 7 from AccessControl import getSecurityManager |
0 | 8 from Globals import package_home |
9 | |
10 from Ft.Xml.Domlette import NonvalidatingReader | |
11 from Ft.Xml.Domlette import PrettyPrint, Print | |
38 | 12 from Ft.Xml import EMPTY_NAMESPACE, Parse |
0 | 13 |
14 import Ft.Xml.XPath | |
15 | |
16 import os.path | |
31 | 17 import sys |
0 | 18 import cgi |
19 import urllib | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
20 import logging |
61 | 21 import math |
52 | 22 |
46 | 23 import urlparse |
0 | 24 |
52 | 25 def logger(txt,method,txt2): |
26 """logging""" | |
27 logging.info(txt+ txt2) | |
28 | |
29 | |
25 | 30 def getInt(number, default=0): |
31 """returns always an int (0 in case of problems)""" | |
32 try: | |
33 return int(number) | |
34 except: | |
62 | 35 return int(default) |
25 | 36 |
0 | 37 def getTextFromNode(nodename): |
46 | 38 """get the cdata content of a node""" |
32 | 39 if nodename is None: |
40 return "" | |
0 | 41 nodelist=nodename.childNodes |
42 rc = "" | |
43 for node in nodelist: | |
44 if node.nodeType == node.TEXT_NODE: | |
45 rc = rc + node.data | |
46 return rc | |
47 | |
35 | 48 |
49 def getParentDir(path): | |
50 """returns pathname shortened by one""" | |
51 return '/'.join(path.split('/')[0:-1]) | |
52 | |
53 | |
0 | 54 import socket |
55 | |
32 | 56 def urlopen(url,timeout=2): |
0 | 57 """urlopen mit timeout""" |
32 | 58 socket.setdefaulttimeout(timeout) |
0 | 59 ret=urllib.urlopen(url) |
60 socket.setdefaulttimeout(5) | |
61 return ret | |
62 | |
63 | |
22 | 64 ## |
65 ## documentViewer class | |
66 ## | |
67 class documentViewer(Folder): | |
0 | 68 """document viewer""" |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
69 #textViewerUrl="http://127.0.0.1:8080/HFQP/testXSLT/getPage?" |
46 | 70 |
0 | 71 meta_type="Document viewer" |
72 | |
73 security=ClassSecurityInfo() | |
22 | 74 manage_options=Folder.manage_options+( |
0 | 75 {'label':'main config','action':'changeDocumentViewerForm'}, |
76 ) | |
77 | |
22 | 78 # templates and forms |
79 viewer_main = PageTemplateFile('zpt/viewer_main', globals()) | |
80 thumbs_main = PageTemplateFile('zpt/thumbs_main', globals()) | |
81 image_main = PageTemplateFile('zpt/image_main', globals()) | |
82 head_main = PageTemplateFile('zpt/head_main', globals()) | |
83 docuviewer_css = PageTemplateFile('css/docuviewer.css', globals()) | |
57 | 84 info_xml = PageTemplateFile('zpt/info_xml', globals()) |
22 | 85 |
68 | 86 thumbs_main_rss = PageTemplateFile('zpt/thumbs_main_rss', globals()) |
22 | 87 security.declareProtected('View management screens','changeDocumentViewerForm') |
88 changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals()) | |
89 | |
0 | 90 |
46 | 91 def __init__(self,id,imageViewerUrl,textViewerUrl=None,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10,authgroups="mpiwg"): |
0 | 92 """init document viewer""" |
93 self.id=id | |
94 self.title=title | |
95 self.imageViewerUrl=imageViewerUrl | |
46 | 96 self.textViewerUrl=textViewerUrl |
97 | |
25 | 98 if not digilibBaseUrl: |
22 | 99 self.digilibBaseUrl = self.findDigilibUrl() |
25 | 100 else: |
101 self.digilibBaseUrl = digilibBaseUrl | |
102 self.thumbcols = thumbcols | |
103 self.thumbrows = thumbrows | |
32 | 104 # authgroups is list of authorized groups (delimited by ,) |
105 self.authgroups = [s.strip().lower() for s in authgroups.split(',')] | |
22 | 106 # add template folder so we can always use template.something |
107 self.manage_addFolder('template') | |
108 | |
109 | |
68 | 110 security.declareProtected('View','thumbs_rss') |
111 def thumbs_rss(self,mode,url,viewMode="auto",start=None,pn=1): | |
112 ''' | |
113 view it | |
114 @param mode: defines how to access the document behind url | |
115 @param url: url which contains display information | |
116 @param viewMode: if images display images, if text display text, default is images (text,images or auto) | |
117 | |
118 ''' | |
119 logging.info("HHHHHHHHHHHHHH:load the rss") | |
120 logger("documentViewer (index)", logging.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) | |
121 | |
122 if not hasattr(self, 'template'): | |
123 # create template folder if it doesn't exist | |
124 self.manage_addFolder('template') | |
125 | |
126 if not self.digilibBaseUrl: | |
127 self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary" | |
128 | |
129 docinfo = self.getDocinfo(mode=mode,url=url) | |
130 pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo) | |
131 pt = getattr(self.template, 'thumbs_main_rss') | |
132 | |
133 if viewMode=="auto": # automodus gewaehlt | |
134 if docinfo.get("textURL",'') and self.textViewerUrl: #texturl gesetzt und textViewer konfiguriert | |
135 viewMode="text" | |
136 else: | |
137 viewMode="images" | |
138 | |
139 return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode) | |
140 | |
22 | 141 security.declareProtected('View','index_html') |
51
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
142 def index_html(self,mode,url,viewMode="auto",start=None,pn=1): |
22 | 143 ''' |
144 view it | |
57 | 145 @param mode: defines how to access the document behind url |
22 | 146 @param url: url which contains display information |
57 | 147 @param viewMode: if images display images, if text display text, default is images (text,images or auto) |
46 | 148 |
22 | 149 ''' |
0 | 150 |
52 | 151 logger("documentViewer (index)", logging.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) |
22 | 152 |
153 if not hasattr(self, 'template'): | |
154 # create template folder if it doesn't exist | |
155 self.manage_addFolder('template') | |
156 | |
157 if not self.digilibBaseUrl: | |
158 self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary" | |
159 | |
25 | 160 docinfo = self.getDocinfo(mode=mode,url=url) |
161 pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo) | |
22 | 162 pt = getattr(self.template, 'viewer_main') |
51
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
163 |
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
164 if viewMode=="auto": # automodus gewaehlt |
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
165 if docinfo.get("textURL",'') and self.textViewerUrl: #texturl gesetzt und textViewer konfiguriert |
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
166 viewMode="text" |
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
167 else: |
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
168 viewMode="images" |
52 | 169 |
46 | 170 return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode) |
0 | 171 |
172 | |
25 | 173 def getLink(self,param=None,val=None): |
174 """link to documentviewer with parameter param set to val""" | |
35 | 175 params=self.REQUEST.form.copy() |
25 | 176 if param is not None: |
31 | 177 if val is None: |
178 if params.has_key(param): | |
179 del params[param] | |
25 | 180 else: |
35 | 181 params[param] = str(val) |
31 | 182 |
35 | 183 # quote values and assemble into query string |
184 ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()]) | |
185 url=self.REQUEST['URL1']+"?"+ps | |
25 | 186 return url |
187 | |
68 | 188 def getLinkAmp(self,param=None,val=None): |
189 """link to documentviewer with parameter param set to val""" | |
190 params=self.REQUEST.form.copy() | |
191 if param is not None: | |
192 if val is None: | |
193 if params.has_key(param): | |
194 del params[param] | |
195 else: | |
196 params[param] = str(val) | |
197 | |
198 # quote values and assemble into query string | |
199 logging.info("XYXXXXX: %s"%repr(params.items())) | |
200 ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()]) | |
201 url=self.REQUEST['URL1']+"?"+ps | |
202 return url | |
57 | 203 def getInfo_xml(self,url,mode): |
204 """returns info about the document as XML""" | |
205 | |
206 if not self.digilibBaseUrl: | |
207 self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary" | |
208 | |
209 docinfo = self.getDocinfo(mode=mode,url=url) | |
210 pt = getattr(self.template, 'info_xml') | |
211 return pt(docinfo=docinfo) | |
212 | |
0 | 213 |
22 | 214 def getStyle(self, idx, selected, style=""): |
25 | 215 """returns a string with the given style and append 'sel' if path == selected.""" |
52 | 216 #logger("documentViewer (getstyle)", logging.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style)) |
22 | 217 if idx == selected: |
218 return style + 'sel' | |
219 else: | |
35 | 220 return style |
57 | 221 |
35 | 222 |
223 def isAccessible(self, docinfo): | |
32 | 224 """returns if access to the resource is granted""" |
225 access = docinfo.get('accessType', None) | |
52 | 226 logger("documentViewer (accessOK)", logging.INFO, "access type %s"%access) |
45 | 227 if access is not None and access == 'free': |
52 | 228 logger("documentViewer (accessOK)", logging.INFO, "access is free") |
32 | 229 return True |
45 | 230 elif access is None or access in self.authgroups: |
35 | 231 # only local access -- only logged in users |
232 user = getSecurityManager().getUser() | |
233 if user is not None: | |
234 #print "user: ", user | |
235 return (user.getUserName() != "Anonymous User") | |
236 else: | |
237 return False | |
32 | 238 |
52 | 239 logger("documentViewer (accessOK)", logging.INFO, "unknown access type %s"%access) |
32 | 240 return False |
35 | 241 |
32 | 242 |
73 | 243 def getDirinfoFromDigilib(self,path,docinfo=None,cut=0): |
29 | 244 """gibt param von dlInfo aus""" |
40 | 245 num_retries = 3 |
31 | 246 if docinfo is None: |
247 docinfo = {} | |
73 | 248 |
249 for x in range(cut): | |
250 path=getParentDir(path) | |
40 | 251 infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path |
29 | 252 |
52 | 253 logger("documentViewer (getparamfromdigilib)", logging.INFO, "dirInfo from %s"%(infoUrl)) |
29 | 254 |
40 | 255 for cnt in range(num_retries): |
35 | 256 try: |
40 | 257 # dom = NonvalidatingReader.parseUri(imageUrl) |
258 txt=urllib.urlopen(infoUrl).read() | |
259 dom = Parse(txt) | |
35 | 260 break |
261 except: | |
52 | 262 logger("documentViewer (getdirinfofromdigilib)", logging.ERROR, "error reading %s (try %d)"%(infoUrl,cnt)) |
35 | 263 else: |
40 | 264 raise IOError("Unable to get dir-info from %s"%(infoUrl)) |
29 | 265 |
37 | 266 sizes=dom.xpath("//dir/size") |
52 | 267 logger("documentViewer (getparamfromdigilib)", logging.INFO, "dirInfo:size"%sizes) |
29 | 268 |
37 | 269 if sizes: |
270 docinfo['numPages'] = int(getTextFromNode(sizes[0])) | |
31 | 271 else: |
272 docinfo['numPages'] = 0 | |
273 | |
274 return docinfo | |
32 | 275 |
29 | 276 |
35 | 277 def getIndexMeta(self, url): |
278 """returns dom of index.meta document at url""" | |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
279 num_retries = 3 |
35 | 280 dom = None |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
281 metaUrl = None |
35 | 282 if url.startswith("http://"): |
283 # real URL | |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
284 metaUrl = url |
35 | 285 else: |
286 # online path | |
287 server=self.digilibBaseUrl+"/servlet/Texter?fn=" | |
40 | 288 metaUrl=server+url.replace("/mpiwg/online","") |
35 | 289 if not metaUrl.endswith("index.meta"): |
290 metaUrl += "/index.meta" | |
46 | 291 print metaUrl |
40 | 292 for cnt in range(num_retries): |
35 | 293 try: |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
294 # patch dirk encoding fehler treten dann nicht mehr auf |
38 | 295 # dom = NonvalidatingReader.parseUri(metaUrl) |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
296 txt=urllib.urlopen(metaUrl).read() |
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
297 dom = Parse(txt) |
40 | 298 break |
35 | 299 except: |
52 | 300 logger("ERROR documentViewer (getIndexMata)", logging.INFO,"%s (%s)"%sys.exc_info()[0:2]) |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
301 |
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
302 if dom is None: |
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
303 raise IOError("Unable to read index meta from %s"%(url)) |
35 | 304 |
305 return dom | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
306 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
307 def getPresentationInfoXML(self, url): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
308 """returns dom of info.xml document at url""" |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
309 num_retries = 3 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
310 dom = None |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
311 metaUrl = None |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
312 if url.startswith("http://"): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
313 # real URL |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
314 metaUrl = url |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
315 else: |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
316 # online path |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
317 server=self.digilibBaseUrl+"/servlet/Texter?fn=" |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
318 metaUrl=server+url.replace("/mpiwg/online","") |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
319 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
320 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
321 for cnt in range(num_retries): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
322 try: |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
323 # patch dirk encoding fehler treten dann nicht mehr auf |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
324 # dom = NonvalidatingReader.parseUri(metaUrl) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
325 txt=urllib.urlopen(metaUrl).read() |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
326 dom = Parse(txt) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
327 break |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
328 except: |
52 | 329 logger("ERROR documentViewer (getPresentationInfoXML)", logging.INFO,"%s (%s)"%sys.exc_info()[0:2]) |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
330 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
331 if dom is None: |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
332 raise IOError("Unable to read infoXMLfrom %s"%(url)) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
333 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
334 return dom |
35 | 335 |
336 | |
70 | 337 def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0): |
35 | 338 """gets authorization info from the index.meta file at path or given by dom""" |
52 | 339 logger("documentViewer (getauthinfofromindexmeta)", logging.INFO,"path: %s"%(path)) |
32 | 340 |
341 access = None | |
342 | |
343 if docinfo is None: | |
344 docinfo = {} | |
345 | |
346 if dom is None: | |
70 | 347 for x in range(cut+1): |
348 path=getParentDir(path) | |
349 dom = self.getIndexMeta(path) | |
46 | 350 |
32 | 351 acctype = dom.xpath("//access-conditions/access/@type") |
352 if acctype and (len(acctype)>0): | |
353 access=acctype[0].value | |
35 | 354 if access in ['group', 'institution']: |
32 | 355 access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower() |
356 | |
357 docinfo['accessType'] = access | |
358 return docinfo | |
29 | 359 |
32 | 360 |
70 | 361 def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0): |
35 | 362 """gets bibliographical info from the index.meta file at path or given by dom""" |
59 | 363 logging.debug("documentViewer (getbibinfofromindexmeta) path: %s"%(path)) |
20 | 364 |
22 | 365 if docinfo is None: |
366 docinfo = {} | |
367 | |
368 if dom is None: | |
70 | 369 for x in range(cut+1): |
370 path=getParentDir(path) | |
371 dom = self.getIndexMeta(path) | |
35 | 372 |
59 | 373 # put in all raw bib fields as dict "bib" |
374 bib = dom.xpath("//bib/*") | |
375 if bib and len(bib)>0: | |
376 bibinfo = {} | |
377 for e in bib: | |
378 bibinfo[e.localName] = getTextFromNode(e) | |
379 docinfo['bib'] = bibinfo | |
380 | |
381 # extract some fields (author, title, year) according to their mapping | |
25 | 382 metaData=self.metadata.main.meta.bib |
383 bibtype=dom.xpath("//bib/@type") | |
384 if bibtype and (len(bibtype)>0): | |
385 bibtype=bibtype[0].value | |
20 | 386 else: |
25 | 387 bibtype="generic" |
59 | 388 |
25 | 389 bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC) |
59 | 390 docinfo['bib_type'] = bibtype |
25 | 391 bibmap=metaData.generateMappingForType(bibtype) |
32 | 392 # if there is no mapping bibmap is empty (mapping sometimes has empty fields) |
31 | 393 if len(bibmap) > 0 and len(bibmap['author'][0]) > 0: |
63 | 394 try: |
395 docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0]) | |
396 except: pass | |
397 try: | |
398 docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0]) | |
399 except: pass | |
400 try: | |
401 docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0]) | |
402 except: pass | |
59 | 403 logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype) |
52 | 404 try: |
405 docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0]) | |
406 except: | |
407 docinfo['lang']='' | |
59 | 408 |
22 | 409 return docinfo |
410 | |
411 | |
32 | 412 def getDocinfoFromTextTool(self,url,dom=None,docinfo=None): |
22 | 413 """parse texttool tag in index meta""" |
52 | 414 logger("documentViewer (getdocinfofromtexttool)", logging.INFO,"url: %s"%(url)) |
22 | 415 if docinfo is None: |
416 docinfo = {} | |
417 | |
52 | 418 if docinfo.get('lang',None) is None: |
419 docinfo['lang']='' # default keine Sprache gesetzt | |
32 | 420 if dom is None: |
35 | 421 dom = self.getIndexMeta(url) |
32 | 422 |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
423 archivePath = None |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
424 archiveName = None |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
425 |
32 | 426 archiveNames=dom.xpath("//resource/name") |
427 if archiveNames and (len(archiveNames)>0): | |
428 archiveName=getTextFromNode(archiveNames[0]) | |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
429 else: |
52 | 430 logger("documentViewer (getdocinfofromtexttool)", logging.WARNING,"resource/name missing in: %s"%(url)) |
22 | 431 |
432 archivePaths=dom.xpath("//resource/archive-path") | |
433 if archivePaths and (len(archivePaths)>0): | |
434 archivePath=getTextFromNode(archivePaths[0]) | |
32 | 435 # clean up archive path |
436 if archivePath[0] != '/': | |
437 archivePath = '/' + archivePath | |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
438 if archiveName and (not archivePath.endswith(archiveName)): |
32 | 439 archivePath += "/" + archiveName |
22 | 440 else: |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
441 # try to get archive-path from url |
52 | 442 logger("documentViewer (getdocinfofromtexttool)", logging.WARNING,"resource/archive-path missing in: %s"%(url)) |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
443 if (not url.startswith('http')): |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
444 archivePath = url.replace('index.meta', '') |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
445 |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
446 if archivePath is None: |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
447 # we balk without archive-path |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
448 raise IOError("Missing archive-path (for text-tool) in %s"%(url)) |
22 | 449 |
35 | 450 imageDirs=dom.xpath("//texttool/image") |
451 if imageDirs and (len(imageDirs)>0): | |
452 imageDir=getTextFromNode(imageDirs[0]) | |
22 | 453 else: |
52 | 454 # we balk with no image tag / not necessary anymore because textmode is now standard |
455 #raise IOError("No text-tool info in %s"%(url)) | |
456 imageDir="" | |
457 docinfo['numPages']=1 # im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht | |
458 | |
459 docinfo['imagePath'] = "" # keine Bilder | |
460 docinfo['imageURL'] = "" | |
461 | |
35 | 462 if imageDir and archivePath: |
463 #print "image: ", imageDir, " archivepath: ", archivePath | |
464 imageDir=os.path.join(archivePath,imageDir) | |
465 imageDir=imageDir.replace("/mpiwg/online",'') | |
466 docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo) | |
467 docinfo['imagePath'] = imageDir | |
468 docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir | |
22 | 469 |
470 viewerUrls=dom.xpath("//texttool/digiliburlprefix") | |
471 if viewerUrls and (len(viewerUrls)>0): | |
472 viewerUrl=getTextFromNode(viewerUrls[0]) | |
31 | 473 docinfo['viewerURL'] = viewerUrl |
22 | 474 |
475 textUrls=dom.xpath("//texttool/text") | |
476 if textUrls and (len(textUrls)>0): | |
477 textUrl=getTextFromNode(textUrls[0]) | |
49
a10fff6199b0
verbesserung f?r text unterst?tzung, text kann jetzt aus url kommen, sprache bisher nur deutsch
dwinter
parents:
46
diff
changeset
|
478 if urlparse.urlparse(textUrl)[0]=="": #keine url |
a10fff6199b0
verbesserung f?r text unterst?tzung, text kann jetzt aus url kommen, sprache bisher nur deutsch
dwinter
parents:
46
diff
changeset
|
479 textUrl=os.path.join(archivePath,textUrl) |
65
c048559460a3
fix problems with full text paths starting with /mpiwg/online
casties
parents:
63
diff
changeset
|
480 # fix URLs starting with /mpiwg/online |
c048559460a3
fix problems with full text paths starting with /mpiwg/online
casties
parents:
63
diff
changeset
|
481 if textUrl.startswith("/mpiwg/online"): |
c048559460a3
fix problems with full text paths starting with /mpiwg/online
casties
parents:
63
diff
changeset
|
482 textUrl = textUrl.replace("/mpiwg/online",'',1) |
c048559460a3
fix problems with full text paths starting with /mpiwg/online
casties
parents:
63
diff
changeset
|
483 |
31 | 484 docinfo['textURL'] = textUrl |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
485 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
486 presentationUrls=dom.xpath("//texttool/presentation") |
52 | 487 docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom) # get info von bib tag |
488 | |
489 if presentationUrls and (len(presentationUrls)>0): # ueberschreibe diese durch presentation informationen | |
63 | 490 # presentation url ergiebt sich ersetzen von index.meta in der url der fuer die Metadaten |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
491 # durch den relativen Pfad auf die presentation infos |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
492 presentationUrl=url.replace('index.meta',getTextFromNode(presentationUrls[0])) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
493 docinfo = self.getBibinfoFromTextToolPresentation(presentationUrl,docinfo=docinfo,dom=dom) |
52 | 494 |
55
ba8917b93c84
fixed bug in fix for missing getauthinfo in getdocinfofromtexttool
casties
parents:
54
diff
changeset
|
495 docinfo = self.getAuthinfoFromIndexMeta(url,docinfo=docinfo,dom=dom) # get access info |
22 | 496 return docinfo |
497 | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
498 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
499 def getBibinfoFromTextToolPresentation(self,url,docinfo=None,dom=None): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
500 """gets the bibliographical information from the preseantion entry in texttools |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
501 """ |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
502 dom=self.getPresentationInfoXML(url) |
62 | 503 try: |
504 docinfo['author']=getTextFromNode(dom.xpath("//author")[0]) | |
505 except: | |
506 pass | |
507 try: | |
508 docinfo['title']=getTextFromNode(dom.xpath("//title")[0]) | |
509 except: | |
510 pass | |
511 try: | |
512 docinfo['year']=getTextFromNode(dom.xpath("//date")[0]) | |
513 except: | |
514 pass | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
515 return docinfo |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
516 |
70 | 517 def getDocinfoFromImagePath(self,path,docinfo=None,cut=0): |
22 | 518 """path ist the path to the images it assumes that the index.meta file is one level higher.""" |
52 | 519 logger("documentViewer (getdocinfofromimagepath)", logging.INFO,"path: %s"%(path)) |
22 | 520 if docinfo is None: |
521 docinfo = {} | |
29 | 522 path=path.replace("/mpiwg/online","") |
22 | 523 docinfo['imagePath'] = path |
73 | 524 docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo,cut=cut) |
31 | 525 imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path |
22 | 526 docinfo['imageURL'] = imageUrl |
527 | |
70 | 528 docinfo = self.getBibinfoFromIndexMeta(path,docinfo=docinfo,cut=cut) |
529 docinfo = self.getAuthinfoFromIndexMeta(path,docinfo=docinfo,cut=cut) | |
22 | 530 return docinfo |
20 | 531 |
22 | 532 |
533 def getDocinfo(self, mode, url): | |
534 """returns docinfo depending on mode""" | |
52 | 535 logger("documentViewer (getdocinfo)", logging.INFO,"mode: %s, url: %s"%(mode,url)) |
22 | 536 # look for cached docinfo in session |
51
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
537 if self.REQUEST.SESSION.has_key('docinfo'): |
22 | 538 docinfo = self.REQUEST.SESSION['docinfo'] |
539 # check if its still current | |
540 if docinfo is not None and docinfo.get('mode') == mode and docinfo.get('url') == url: | |
52 | 541 logger("documentViewer (getdocinfo)", logging.INFO,"docinfo in session: %s"%docinfo) |
22 | 542 return docinfo |
543 # new docinfo | |
544 docinfo = {'mode': mode, 'url': url} | |
545 if mode=="texttool": #index.meta with texttool information | |
546 docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo) | |
547 elif mode=="imagepath": | |
548 docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo) | |
70 | 549 elif mode=="filepath": |
71
6412c45dff58
minorCVS: ----------------------------------------------------------------------
dwinter
parents:
70
diff
changeset
|
550 docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo,cut=2) |
22 | 551 else: |
52 | 552 logger("documentViewer (getdocinfo)", logging.ERROR,"unknown mode!") |
37 | 553 raise ValueError("Unknown mode %s"%(mode)) |
554 | |
52 | 555 logger("documentViewer (getdocinfo)", logging.INFO,"docinfo: %s"%docinfo) |
22 | 556 self.REQUEST.SESSION['docinfo'] = docinfo |
557 return docinfo | |
20 | 558 |
559 | |
25 | 560 def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None): |
22 | 561 """returns pageinfo with the given parameters""" |
562 pageinfo = {} | |
25 | 563 current = getInt(current) |
564 pageinfo['current'] = current | |
565 rows = int(rows or self.thumbrows) | |
566 pageinfo['rows'] = rows | |
567 cols = int(cols or self.thumbcols) | |
568 pageinfo['cols'] = cols | |
569 grpsize = cols * rows | |
570 pageinfo['groupsize'] = grpsize | |
61 | 571 start = getInt(start, default=(math.ceil(float(current)/float(grpsize))*grpsize-(grpsize-1))) |
572 # int(current / grpsize) * grpsize +1)) | |
22 | 573 pageinfo['start'] = start |
25 | 574 pageinfo['end'] = start + grpsize |
575 if docinfo is not None: | |
576 np = int(docinfo['numPages']) | |
577 pageinfo['end'] = min(pageinfo['end'], np) | |
578 pageinfo['numgroups'] = int(np / grpsize) | |
579 if np % grpsize > 0: | |
580 pageinfo['numgroups'] += 1 | |
581 | |
22 | 582 return pageinfo |
583 | |
0 | 584 def text(self,mode,url,pn): |
585 """give text""" | |
586 if mode=="texttool": #index.meta with texttool information | |
587 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | |
588 | |
35 | 589 #print textpath |
0 | 590 try: |
591 dom = NonvalidatingReader.parseUri(textpath) | |
592 except: | |
593 return None | |
594 | |
595 list=[] | |
596 nodes=dom.xpath("//pb") | |
597 | |
598 node=nodes[int(pn)-1] | |
599 | |
600 p=node | |
601 | |
602 while p.tagName!="p": | |
603 p=p.parentNode | |
604 | |
605 | |
606 endNode=nodes[int(pn)] | |
607 | |
608 | |
609 e=endNode | |
610 | |
611 while e.tagName!="p": | |
612 e=e.parentNode | |
613 | |
614 | |
615 next=node.parentNode | |
616 | |
617 #sammle s | |
618 while next and (next!=endNode.parentNode): | |
619 list.append(next) | |
620 next=next.nextSibling | |
621 list.append(endNode.parentNode) | |
622 | |
623 if p==e:# beide im selben paragraphen | |
20 | 624 pass |
625 # else: | |
626 # next=p | |
627 # while next!=e: | |
628 # print next,e | |
629 # list.append(next) | |
630 # next=next.nextSibling | |
631 # | |
632 # for x in list: | |
633 # PrettyPrint(x) | |
634 # | |
635 # return list | |
22 | 636 # |
637 | |
638 def findDigilibUrl(self): | |
639 """try to get the digilib URL from zogilib""" | |
640 url = self.imageViewerUrl[:-1] + "/getScalerUrl" | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
641 #print urlparse.urlparse(url)[0] |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
642 #print urlparse.urljoin(self.absolute_url(),url) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
643 logging.info("finddigiliburl: %s"%urlparse.urlparse(url)[0]) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
644 logging.info("finddigiliburl: %s"%urlparse.urljoin(self.absolute_url(),url)) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
645 |
22 | 646 try: |
46 | 647 if urlparse.urlparse(url)[0]=='': #relative path |
648 url=urlparse.urljoin(self.absolute_url()+"/",url) | |
649 | |
22 | 650 scaler = urlopen(url).read() |
651 return scaler.replace("/servlet/Scaler?", "") | |
652 except: | |
653 return None | |
654 | |
46 | 655 def changeDocumentViewer(self,imageViewerUrl,textViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None): |
22 | 656 """init document viewer""" |
657 self.title=title | |
658 self.imageViewerUrl=imageViewerUrl | |
46 | 659 self.textViewerUrl=textViewerUrl |
22 | 660 self.digilibBaseUrl = digilibBaseUrl |
25 | 661 self.thumbrows = thumbrows |
662 self.thumbcols = thumbcols | |
32 | 663 self.authgroups = [s.strip().lower() for s in authgroups.split(',')] |
22 | 664 if RESPONSE is not None: |
665 RESPONSE.redirect('manage_main') | |
0 | 666 |
667 | |
668 | |
669 | |
670 # security.declareProtected('View management screens','renameImageForm') | |
671 | |
672 def manage_AddDocumentViewerForm(self): | |
673 """add the viewer form""" | |
22 | 674 pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self) |
0 | 675 return pt() |
676 | |
46 | 677 def manage_AddDocumentViewer(self,id,imageViewerUrl="",textViewerUrl="",title="",RESPONSE=None): |
0 | 678 """add the viewer""" |
46 | 679 newObj=documentViewer(id,imageViewerUrl,title=title,textViewerUrl=textViewerUrl) |
0 | 680 self._setObject(id,newObj) |
681 | |
682 if RESPONSE is not None: | |
683 RESPONSE.redirect('manage_main') | |
22 | 684 |
685 | |
686 ## | |
687 ## DocumentViewerTemplate class | |
688 ## | |
689 class DocumentViewerTemplate(ZopePageTemplate): | |
690 """Template for document viewer""" | |
691 meta_type="DocumentViewer Template" | |
692 | |
693 | |
694 def manage_addDocumentViewerTemplateForm(self): | |
695 """Form for adding""" | |
696 pt=PageTemplateFile('zpt/addDocumentViewerTemplate', globals()).__of__(self) | |
697 return pt() | |
698 | |
699 def manage_addDocumentViewerTemplate(self, id='viewer_main', title=None, text=None, | |
700 REQUEST=None, submit=None): | |
701 "Add a Page Template with optional file content." | |
702 | |
703 self._setObject(id, DocumentViewerTemplate(id)) | |
704 ob = getattr(self, id) | |
53
f4e0af8c281d
NEW - # 44: ECHO - vollst?ndige bibliographische Angabe
dwinter
parents:
52
diff
changeset
|
705 txt=file(os.path.join(package_home(globals()),'zpt/viewer_main.zpt'),'r').read() |
f4e0af8c281d
NEW - # 44: ECHO - vollst?ndige bibliographische Angabe
dwinter
parents:
52
diff
changeset
|
706 logging.info("txt %s:"%txt) |
f4e0af8c281d
NEW - # 44: ECHO - vollst?ndige bibliographische Angabe
dwinter
parents:
52
diff
changeset
|
707 ob.pt_edit(txt,"text/html") |
22 | 708 if title: |
709 ob.pt_setTitle(title) | |
710 try: | |
711 u = self.DestinationURL() | |
712 except AttributeError: | |
713 u = REQUEST['URL1'] | |
714 | |
715 u = "%s/%s" % (u, urllib.quote(id)) | |
716 REQUEST.RESPONSE.redirect(u+'/manage_main') | |
717 return '' | |
718 | |
719 | |
41 | 720 |