Mercurial > hg > documentViewer
annotate documentViewer.py @ 59:996b61d71351
added all fields from bib tag to docinfo
author | casties |
---|---|
date | Fri, 08 Jun 2007 20:10:22 +0200 |
parents | 7cdb0fc34a92 |
children | f3d2f240692c |
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""" |
59 | 312 logging.debug("documentViewer (getbibinfofromindexmeta) 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 | |
59 | 320 # put in all raw bib fields as dict "bib" |
321 bib = dom.xpath("//bib/*") | |
322 if bib and len(bib)>0: | |
323 bibinfo = {} | |
324 for e in bib: | |
325 bibinfo[e.localName] = getTextFromNode(e) | |
326 docinfo['bib'] = bibinfo | |
327 | |
328 # extract some fields (author, title, year) according to their mapping | |
25 | 329 metaData=self.metadata.main.meta.bib |
330 bibtype=dom.xpath("//bib/@type") | |
331 if bibtype and (len(bibtype)>0): | |
332 bibtype=bibtype[0].value | |
20 | 333 else: |
25 | 334 bibtype="generic" |
59 | 335 |
25 | 336 bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC) |
59 | 337 docinfo['bib_type'] = bibtype |
25 | 338 bibmap=metaData.generateMappingForType(bibtype) |
32 | 339 # if there is no mapping bibmap is empty (mapping sometimes has empty fields) |
31 | 340 if len(bibmap) > 0 and len(bibmap['author'][0]) > 0: |
25 | 341 docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0]) |
342 docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0]) | |
343 docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0]) | |
59 | 344 logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype) |
52 | 345 try: |
346 docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0]) | |
347 except: | |
348 docinfo['lang']='' | |
59 | 349 |
22 | 350 return docinfo |
351 | |
352 | |
32 | 353 def getDocinfoFromTextTool(self,url,dom=None,docinfo=None): |
22 | 354 """parse texttool tag in index meta""" |
52 | 355 logger("documentViewer (getdocinfofromtexttool)", logging.INFO,"url: %s"%(url)) |
22 | 356 if docinfo is None: |
357 docinfo = {} | |
358 | |
52 | 359 if docinfo.get('lang',None) is None: |
360 docinfo['lang']='' # default keine Sprache gesetzt | |
32 | 361 if dom is None: |
35 | 362 dom = self.getIndexMeta(url) |
32 | 363 |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
364 archivePath = None |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
365 archiveName = None |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
366 |
32 | 367 archiveNames=dom.xpath("//resource/name") |
368 if archiveNames and (len(archiveNames)>0): | |
369 archiveName=getTextFromNode(archiveNames[0]) | |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
370 else: |
52 | 371 logger("documentViewer (getdocinfofromtexttool)", logging.WARNING,"resource/name missing in: %s"%(url)) |
22 | 372 |
373 archivePaths=dom.xpath("//resource/archive-path") | |
374 if archivePaths and (len(archivePaths)>0): | |
375 archivePath=getTextFromNode(archivePaths[0]) | |
32 | 376 # clean up archive path |
377 if archivePath[0] != '/': | |
378 archivePath = '/' + archivePath | |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
379 if archiveName and (not archivePath.endswith(archiveName)): |
32 | 380 archivePath += "/" + archiveName |
22 | 381 else: |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
382 # try to get archive-path from url |
52 | 383 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
|
384 if (not url.startswith('http')): |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
385 archivePath = url.replace('index.meta', '') |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
386 |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
387 if archivePath is None: |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
388 # we balk without archive-path |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
389 raise IOError("Missing archive-path (for text-tool) in %s"%(url)) |
22 | 390 |
35 | 391 imageDirs=dom.xpath("//texttool/image") |
392 if imageDirs and (len(imageDirs)>0): | |
393 imageDir=getTextFromNode(imageDirs[0]) | |
22 | 394 else: |
52 | 395 # we balk with no image tag / not necessary anymore because textmode is now standard |
396 #raise IOError("No text-tool info in %s"%(url)) | |
397 imageDir="" | |
398 docinfo['numPages']=1 # im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht | |
399 | |
400 docinfo['imagePath'] = "" # keine Bilder | |
401 docinfo['imageURL'] = "" | |
402 | |
35 | 403 if imageDir and archivePath: |
404 #print "image: ", imageDir, " archivepath: ", archivePath | |
405 imageDir=os.path.join(archivePath,imageDir) | |
406 imageDir=imageDir.replace("/mpiwg/online",'') | |
407 docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo) | |
408 docinfo['imagePath'] = imageDir | |
409 docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir | |
22 | 410 |
411 viewerUrls=dom.xpath("//texttool/digiliburlprefix") | |
412 if viewerUrls and (len(viewerUrls)>0): | |
413 viewerUrl=getTextFromNode(viewerUrls[0]) | |
31 | 414 docinfo['viewerURL'] = viewerUrl |
22 | 415 |
416 textUrls=dom.xpath("//texttool/text") | |
417 if textUrls and (len(textUrls)>0): | |
418 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
|
419 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
|
420 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
|
421 |
31 | 422 docinfo['textURL'] = textUrl |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
423 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
424 presentationUrls=dom.xpath("//texttool/presentation") |
52 | 425 docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom) # get info von bib tag |
426 | |
427 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
|
428 # 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
|
429 # durch den relativen Pfad auf die presentation infos |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
430 presentationUrl=url.replace('index.meta',getTextFromNode(presentationUrls[0])) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
431 docinfo = self.getBibinfoFromTextToolPresentation(presentationUrl,docinfo=docinfo,dom=dom) |
52 | 432 |
55
ba8917b93c84
fixed bug in fix for missing getauthinfo in getdocinfofromtexttool
casties
parents:
54
diff
changeset
|
433 docinfo = self.getAuthinfoFromIndexMeta(url,docinfo=docinfo,dom=dom) # get access info |
22 | 434 return docinfo |
435 | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
436 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
437 def getBibinfoFromTextToolPresentation(self,url,docinfo=None,dom=None): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
438 """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
|
439 """ |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
440 dom=self.getPresentationInfoXML(url) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
441 docinfo['author']=getTextFromNode(dom.xpath("//author")[0]) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
442 docinfo['title']=getTextFromNode(dom.xpath("//title")[0]) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
443 docinfo['year']=getTextFromNode(dom.xpath("//date")[0]) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
444 return docinfo |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
445 |
22 | 446 def getDocinfoFromImagePath(self,path,docinfo=None): |
447 """path ist the path to the images it assumes that the index.meta file is one level higher.""" | |
52 | 448 logger("documentViewer (getdocinfofromimagepath)", logging.INFO,"path: %s"%(path)) |
22 | 449 if docinfo is None: |
450 docinfo = {} | |
29 | 451 path=path.replace("/mpiwg/online","") |
22 | 452 docinfo['imagePath'] = path |
31 | 453 docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo) |
454 imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path | |
22 | 455 docinfo['imageURL'] = imageUrl |
456 | |
457 docinfo = self.getBibinfoFromIndexMeta(path,docinfo=docinfo) | |
32 | 458 docinfo = self.getAuthinfoFromIndexMeta(path,docinfo=docinfo) |
22 | 459 return docinfo |
20 | 460 |
22 | 461 |
462 def getDocinfo(self, mode, url): | |
463 """returns docinfo depending on mode""" | |
52 | 464 logger("documentViewer (getdocinfo)", logging.INFO,"mode: %s, url: %s"%(mode,url)) |
22 | 465 # look for cached docinfo in session |
51
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
466 if self.REQUEST.SESSION.has_key('docinfo'): |
22 | 467 docinfo = self.REQUEST.SESSION['docinfo'] |
468 # check if its still current | |
469 if docinfo is not None and docinfo.get('mode') == mode and docinfo.get('url') == url: | |
52 | 470 logger("documentViewer (getdocinfo)", logging.INFO,"docinfo in session: %s"%docinfo) |
22 | 471 return docinfo |
472 # new docinfo | |
473 docinfo = {'mode': mode, 'url': url} | |
474 if mode=="texttool": #index.meta with texttool information | |
475 docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo) | |
476 elif mode=="imagepath": | |
477 docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo) | |
478 else: | |
52 | 479 logger("documentViewer (getdocinfo)", logging.ERROR,"unknown mode!") |
37 | 480 raise ValueError("Unknown mode %s"%(mode)) |
481 | |
52 | 482 logger("documentViewer (getdocinfo)", logging.INFO,"docinfo: %s"%docinfo) |
22 | 483 self.REQUEST.SESSION['docinfo'] = docinfo |
484 return docinfo | |
20 | 485 |
486 | |
25 | 487 def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None): |
22 | 488 """returns pageinfo with the given parameters""" |
489 pageinfo = {} | |
25 | 490 current = getInt(current) |
491 pageinfo['current'] = current | |
492 rows = int(rows or self.thumbrows) | |
493 pageinfo['rows'] = rows | |
494 cols = int(cols or self.thumbcols) | |
495 pageinfo['cols'] = cols | |
496 grpsize = cols * rows | |
497 pageinfo['groupsize'] = grpsize | |
498 start = getInt(start, default=(int(current / grpsize) * grpsize +1)) | |
22 | 499 pageinfo['start'] = start |
25 | 500 pageinfo['end'] = start + grpsize |
501 if docinfo is not None: | |
502 np = int(docinfo['numPages']) | |
503 pageinfo['end'] = min(pageinfo['end'], np) | |
504 pageinfo['numgroups'] = int(np / grpsize) | |
505 if np % grpsize > 0: | |
506 pageinfo['numgroups'] += 1 | |
507 | |
22 | 508 return pageinfo |
509 | |
0 | 510 def text(self,mode,url,pn): |
511 """give text""" | |
512 if mode=="texttool": #index.meta with texttool information | |
513 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | |
514 | |
35 | 515 #print textpath |
0 | 516 try: |
517 dom = NonvalidatingReader.parseUri(textpath) | |
518 except: | |
519 return None | |
520 | |
521 list=[] | |
522 nodes=dom.xpath("//pb") | |
523 | |
524 node=nodes[int(pn)-1] | |
525 | |
526 p=node | |
527 | |
528 while p.tagName!="p": | |
529 p=p.parentNode | |
530 | |
531 | |
532 endNode=nodes[int(pn)] | |
533 | |
534 | |
535 e=endNode | |
536 | |
537 while e.tagName!="p": | |
538 e=e.parentNode | |
539 | |
540 | |
541 next=node.parentNode | |
542 | |
543 #sammle s | |
544 while next and (next!=endNode.parentNode): | |
545 list.append(next) | |
546 next=next.nextSibling | |
547 list.append(endNode.parentNode) | |
548 | |
549 if p==e:# beide im selben paragraphen | |
20 | 550 pass |
551 # else: | |
552 # next=p | |
553 # while next!=e: | |
554 # print next,e | |
555 # list.append(next) | |
556 # next=next.nextSibling | |
557 # | |
558 # for x in list: | |
559 # PrettyPrint(x) | |
560 # | |
561 # return list | |
22 | 562 # |
563 | |
564 def findDigilibUrl(self): | |
565 """try to get the digilib URL from zogilib""" | |
566 url = self.imageViewerUrl[:-1] + "/getScalerUrl" | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
567 #print urlparse.urlparse(url)[0] |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
568 #print urlparse.urljoin(self.absolute_url(),url) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
569 logging.info("finddigiliburl: %s"%urlparse.urlparse(url)[0]) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
570 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
|
571 |
22 | 572 try: |
46 | 573 if urlparse.urlparse(url)[0]=='': #relative path |
574 url=urlparse.urljoin(self.absolute_url()+"/",url) | |
575 | |
22 | 576 scaler = urlopen(url).read() |
577 return scaler.replace("/servlet/Scaler?", "") | |
578 except: | |
579 return None | |
580 | |
46 | 581 def changeDocumentViewer(self,imageViewerUrl,textViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None): |
22 | 582 """init document viewer""" |
583 self.title=title | |
584 self.imageViewerUrl=imageViewerUrl | |
46 | 585 self.textViewerUrl=textViewerUrl |
22 | 586 self.digilibBaseUrl = digilibBaseUrl |
25 | 587 self.thumbrows = thumbrows |
588 self.thumbcols = thumbcols | |
32 | 589 self.authgroups = [s.strip().lower() for s in authgroups.split(',')] |
22 | 590 if RESPONSE is not None: |
591 RESPONSE.redirect('manage_main') | |
0 | 592 |
593 | |
594 | |
595 | |
596 # security.declareProtected('View management screens','renameImageForm') | |
597 | |
598 def manage_AddDocumentViewerForm(self): | |
599 """add the viewer form""" | |
22 | 600 pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self) |
0 | 601 return pt() |
602 | |
46 | 603 def manage_AddDocumentViewer(self,id,imageViewerUrl="",textViewerUrl="",title="",RESPONSE=None): |
0 | 604 """add the viewer""" |
46 | 605 newObj=documentViewer(id,imageViewerUrl,title=title,textViewerUrl=textViewerUrl) |
0 | 606 self._setObject(id,newObj) |
607 | |
608 if RESPONSE is not None: | |
609 RESPONSE.redirect('manage_main') | |
22 | 610 |
611 | |
612 ## | |
613 ## DocumentViewerTemplate class | |
614 ## | |
615 class DocumentViewerTemplate(ZopePageTemplate): | |
616 """Template for document viewer""" | |
617 meta_type="DocumentViewer Template" | |
618 | |
619 | |
620 def manage_addDocumentViewerTemplateForm(self): | |
621 """Form for adding""" | |
622 pt=PageTemplateFile('zpt/addDocumentViewerTemplate', globals()).__of__(self) | |
623 return pt() | |
624 | |
625 def manage_addDocumentViewerTemplate(self, id='viewer_main', title=None, text=None, | |
626 REQUEST=None, submit=None): | |
627 "Add a Page Template with optional file content." | |
628 | |
629 self._setObject(id, DocumentViewerTemplate(id)) | |
630 ob = getattr(self, id) | |
53
f4e0af8c281d
NEW - # 44: ECHO - vollst?ndige bibliographische Angabe
dwinter
parents:
52
diff
changeset
|
631 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
|
632 logging.info("txt %s:"%txt) |
f4e0af8c281d
NEW - # 44: ECHO - vollst?ndige bibliographische Angabe
dwinter
parents:
52
diff
changeset
|
633 ob.pt_edit(txt,"text/html") |
22 | 634 if title: |
635 ob.pt_setTitle(title) | |
636 try: | |
637 u = self.DestinationURL() | |
638 except AttributeError: | |
639 u = REQUEST['URL1'] | |
640 | |
641 u = "%s/%s" % (u, urllib.quote(id)) | |
642 REQUEST.RESPONSE.redirect(u+'/manage_main') | |
643 return '' | |
644 | |
645 | |
41 | 646 |