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