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