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