Mercurial > hg > documentViewer
annotate documentViewer.py @ 83:ec12a2440daa
My last update Bukhman Andrey
author | abukhman |
---|---|
date | Mon, 08 Mar 2010 16:02:33 +0100 |
parents | 260078dfd1c4 |
children | a6e4f9b6729a |
rev | line source |
---|---|
46 | 1 |
0 | 2 from OFS.Folder import Folder |
3 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate | |
52 | 4 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
0 | 5 from AccessControl import ClassSecurityInfo |
32 | 6 from AccessControl import getSecurityManager |
0 | 7 from Globals import package_home |
8 | |
9 from Ft.Xml.Domlette import NonvalidatingReader | |
10 from Ft.Xml.Domlette import PrettyPrint, Print | |
38 | 11 from Ft.Xml import EMPTY_NAMESPACE, Parse |
0 | 12 |
83 | 13 |
0 | 14 import Ft.Xml.XPath |
83 | 15 import xmlrpclib |
0 | 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 |
83 | 236 def getTextLanguage(self,url,docinfo): |
237 docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0]) | |
238 lang = urlencode({'':docinfo['lang']}) | |
239 return lang | |
240 | |
35 | 241 |
242 def isAccessible(self, docinfo): | |
32 | 243 """returns if access to the resource is granted""" |
244 access = docinfo.get('accessType', None) | |
52 | 245 logger("documentViewer (accessOK)", logging.INFO, "access type %s"%access) |
45 | 246 if access is not None and access == 'free': |
52 | 247 logger("documentViewer (accessOK)", logging.INFO, "access is free") |
32 | 248 return True |
45 | 249 elif access is None or access in self.authgroups: |
35 | 250 # only local access -- only logged in users |
251 user = getSecurityManager().getUser() | |
252 if user is not None: | |
253 #print "user: ", user | |
254 return (user.getUserName() != "Anonymous User") | |
255 else: | |
256 return False | |
32 | 257 |
52 | 258 logger("documentViewer (accessOK)", logging.INFO, "unknown access type %s"%access) |
32 | 259 return False |
35 | 260 |
32 | 261 |
73 | 262 def getDirinfoFromDigilib(self,path,docinfo=None,cut=0): |
29 | 263 """gibt param von dlInfo aus""" |
40 | 264 num_retries = 3 |
31 | 265 if docinfo is None: |
266 docinfo = {} | |
73 | 267 |
268 for x in range(cut): | |
78 | 269 |
73 | 270 path=getParentDir(path) |
78 | 271 |
40 | 272 infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path |
29 | 273 |
52 | 274 logger("documentViewer (getparamfromdigilib)", logging.INFO, "dirInfo from %s"%(infoUrl)) |
29 | 275 |
40 | 276 for cnt in range(num_retries): |
35 | 277 try: |
40 | 278 # dom = NonvalidatingReader.parseUri(imageUrl) |
279 txt=urllib.urlopen(infoUrl).read() | |
280 dom = Parse(txt) | |
35 | 281 break |
282 except: | |
52 | 283 logger("documentViewer (getdirinfofromdigilib)", logging.ERROR, "error reading %s (try %d)"%(infoUrl,cnt)) |
35 | 284 else: |
40 | 285 raise IOError("Unable to get dir-info from %s"%(infoUrl)) |
29 | 286 |
37 | 287 sizes=dom.xpath("//dir/size") |
52 | 288 logger("documentViewer (getparamfromdigilib)", logging.INFO, "dirInfo:size"%sizes) |
29 | 289 |
37 | 290 if sizes: |
291 docinfo['numPages'] = int(getTextFromNode(sizes[0])) | |
31 | 292 else: |
293 docinfo['numPages'] = 0 | |
294 | |
295 return docinfo | |
32 | 296 |
29 | 297 |
35 | 298 def getIndexMeta(self, url): |
299 """returns dom of index.meta document at url""" | |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
300 num_retries = 3 |
35 | 301 dom = None |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
302 metaUrl = None |
35 | 303 if url.startswith("http://"): |
304 # real URL | |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
305 metaUrl = url |
35 | 306 else: |
307 # online path | |
308 server=self.digilibBaseUrl+"/servlet/Texter?fn=" | |
40 | 309 metaUrl=server+url.replace("/mpiwg/online","") |
35 | 310 if not metaUrl.endswith("index.meta"): |
311 metaUrl += "/index.meta" | |
75
9673218e155b
minorCVS: ----------------------------------------------------------------------
dwinter
parents:
74
diff
changeset
|
312 logging.debug("METAURL: %s"%metaUrl) |
40 | 313 for cnt in range(num_retries): |
35 | 314 try: |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
315 # patch dirk encoding fehler treten dann nicht mehr auf |
38 | 316 # dom = NonvalidatingReader.parseUri(metaUrl) |
39
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
317 txt=urllib.urlopen(metaUrl).read() |
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
318 dom = Parse(txt) |
40 | 319 break |
35 | 320 except: |
52 | 321 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
|
322 |
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
323 if dom is None: |
1dd90aabd366
added retry when reading index meta from texter applet
casties
parents:
38
diff
changeset
|
324 raise IOError("Unable to read index meta from %s"%(url)) |
35 | 325 |
326 return dom | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
327 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
328 def getPresentationInfoXML(self, url): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
329 """returns dom of info.xml document at url""" |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
330 num_retries = 3 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
331 dom = None |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
332 metaUrl = None |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
333 if url.startswith("http://"): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
334 # real URL |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
335 metaUrl = url |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
336 else: |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
337 # online path |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
338 server=self.digilibBaseUrl+"/servlet/Texter?fn=" |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
339 metaUrl=server+url.replace("/mpiwg/online","") |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
340 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
341 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
342 for cnt in range(num_retries): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
343 try: |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
344 # patch dirk encoding fehler treten dann nicht mehr auf |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
345 # dom = NonvalidatingReader.parseUri(metaUrl) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
346 txt=urllib.urlopen(metaUrl).read() |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
347 dom = Parse(txt) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
348 break |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
349 except: |
52 | 350 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
|
351 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
352 if dom is None: |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
353 raise IOError("Unable to read infoXMLfrom %s"%(url)) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
354 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
355 return dom |
35 | 356 |
357 | |
70 | 358 def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0): |
35 | 359 """gets authorization info from the index.meta file at path or given by dom""" |
52 | 360 logger("documentViewer (getauthinfofromindexmeta)", logging.INFO,"path: %s"%(path)) |
32 | 361 |
362 access = None | |
363 | |
364 if docinfo is None: | |
365 docinfo = {} | |
366 | |
367 if dom is None: | |
78 | 368 for x in range(cut): |
70 | 369 path=getParentDir(path) |
370 dom = self.getIndexMeta(path) | |
46 | 371 |
32 | 372 acctype = dom.xpath("//access-conditions/access/@type") |
373 if acctype and (len(acctype)>0): | |
374 access=acctype[0].value | |
35 | 375 if access in ['group', 'institution']: |
32 | 376 access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower() |
377 | |
378 docinfo['accessType'] = access | |
379 return docinfo | |
29 | 380 |
32 | 381 |
70 | 382 def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0): |
35 | 383 """gets bibliographical info from the index.meta file at path or given by dom""" |
59 | 384 logging.debug("documentViewer (getbibinfofromindexmeta) path: %s"%(path)) |
20 | 385 |
22 | 386 if docinfo is None: |
387 docinfo = {} | |
78 | 388 |
22 | 389 if dom is None: |
78 | 390 for x in range(cut): |
70 | 391 path=getParentDir(path) |
392 dom = self.getIndexMeta(path) | |
79
df6952ac93e9
bug in getDocInforFromImagePath, relative lage der index.meta zu path war falsch.
dwinter
parents:
78
diff
changeset
|
393 |
df6952ac93e9
bug in getDocInforFromImagePath, relative lage der index.meta zu path war falsch.
dwinter
parents:
78
diff
changeset
|
394 logging.debug("documentViewer (getbibinfofromindexmeta cutted) path: %s"%(path)) |
59 | 395 # put in all raw bib fields as dict "bib" |
396 bib = dom.xpath("//bib/*") | |
397 if bib and len(bib)>0: | |
398 bibinfo = {} | |
399 for e in bib: | |
400 bibinfo[e.localName] = getTextFromNode(e) | |
401 docinfo['bib'] = bibinfo | |
402 | |
403 # extract some fields (author, title, year) according to their mapping | |
25 | 404 metaData=self.metadata.main.meta.bib |
405 bibtype=dom.xpath("//bib/@type") | |
406 if bibtype and (len(bibtype)>0): | |
407 bibtype=bibtype[0].value | |
20 | 408 else: |
25 | 409 bibtype="generic" |
59 | 410 |
25 | 411 bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC) |
59 | 412 docinfo['bib_type'] = bibtype |
25 | 413 bibmap=metaData.generateMappingForType(bibtype) |
32 | 414 # if there is no mapping bibmap is empty (mapping sometimes has empty fields) |
31 | 415 if len(bibmap) > 0 and len(bibmap['author'][0]) > 0: |
63 | 416 try: |
417 docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0]) | |
418 except: pass | |
419 try: | |
420 docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0]) | |
421 except: pass | |
422 try: | |
423 docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0]) | |
424 except: pass | |
59 | 425 logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype) |
52 | 426 try: |
427 docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0]) | |
428 except: | |
429 docinfo['lang']='' | |
59 | 430 |
22 | 431 return docinfo |
83 | 432 |
433 | |
434 def getNumPages(self, xquery, docinfo=None): #New Method 24.02.2010 | |
435 text=self.viewerTemplates.query.eval("/mpdl/interface/xquery.xql","document="+ docinfo['textURLPath'] +"&xquery="+str(xquery)) | |
436 docinfo['numPages'] = text.count("<pb ") | |
437 return docinfo | |
438 | |
22 | 439 |
32 | 440 def getDocinfoFromTextTool(self,url,dom=None,docinfo=None): |
22 | 441 """parse texttool tag in index meta""" |
52 | 442 logger("documentViewer (getdocinfofromtexttool)", logging.INFO,"url: %s"%(url)) |
22 | 443 if docinfo is None: |
444 docinfo = {} | |
445 | |
52 | 446 if docinfo.get('lang',None) is None: |
447 docinfo['lang']='' # default keine Sprache gesetzt | |
32 | 448 if dom is None: |
35 | 449 dom = self.getIndexMeta(url) |
32 | 450 |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
451 archivePath = None |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
452 archiveName = None |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
453 |
32 | 454 archiveNames=dom.xpath("//resource/name") |
455 if archiveNames and (len(archiveNames)>0): | |
456 archiveName=getTextFromNode(archiveNames[0]) | |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
457 else: |
52 | 458 logger("documentViewer (getdocinfofromtexttool)", logging.WARNING,"resource/name missing in: %s"%(url)) |
22 | 459 |
460 archivePaths=dom.xpath("//resource/archive-path") | |
461 if archivePaths and (len(archivePaths)>0): | |
462 archivePath=getTextFromNode(archivePaths[0]) | |
32 | 463 # clean up archive path |
464 if archivePath[0] != '/': | |
465 archivePath = '/' + archivePath | |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
466 if archiveName and (not archivePath.endswith(archiveName)): |
32 | 467 archivePath += "/" + archiveName |
22 | 468 else: |
43
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
469 # try to get archive-path from url |
52 | 470 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
|
471 if (not url.startswith('http')): |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
472 archivePath = url.replace('index.meta', '') |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
473 |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
474 if archivePath is None: |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
475 # we balk without archive-path |
f3bc59cf64d9
fixed some problems with bad index.meta files (text-tools mode)
casties
parents:
42
diff
changeset
|
476 raise IOError("Missing archive-path (for text-tool) in %s"%(url)) |
22 | 477 |
35 | 478 imageDirs=dom.xpath("//texttool/image") |
479 if imageDirs and (len(imageDirs)>0): | |
480 imageDir=getTextFromNode(imageDirs[0]) | |
83 | 481 |
22 | 482 else: |
52 | 483 # we balk with no image tag / not necessary anymore because textmode is now standard |
484 #raise IOError("No text-tool info in %s"%(url)) | |
485 imageDir="" | |
83 | 486 #xquery="//pb" |
52 | 487 docinfo['imagePath'] = "" # keine Bilder |
488 docinfo['imageURL'] = "" | |
83 | 489 |
35 | 490 if imageDir and archivePath: |
491 #print "image: ", imageDir, " archivepath: ", archivePath | |
492 imageDir=os.path.join(archivePath,imageDir) | |
493 imageDir=imageDir.replace("/mpiwg/online",'') | |
494 docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo) | |
495 docinfo['imagePath'] = imageDir | |
78 | 496 |
35 | 497 docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir |
22 | 498 |
499 viewerUrls=dom.xpath("//texttool/digiliburlprefix") | |
500 if viewerUrls and (len(viewerUrls)>0): | |
501 viewerUrl=getTextFromNode(viewerUrls[0]) | |
31 | 502 docinfo['viewerURL'] = viewerUrl |
22 | 503 |
504 textUrls=dom.xpath("//texttool/text") | |
505 if textUrls and (len(textUrls)>0): | |
506 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
|
507 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
|
508 textUrl=os.path.join(archivePath,textUrl) |
65
c048559460a3
fix problems with full text paths starting with /mpiwg/online
casties
parents:
63
diff
changeset
|
509 # fix URLs starting with /mpiwg/online |
c048559460a3
fix problems with full text paths starting with /mpiwg/online
casties
parents:
63
diff
changeset
|
510 if textUrl.startswith("/mpiwg/online"): |
c048559460a3
fix problems with full text paths starting with /mpiwg/online
casties
parents:
63
diff
changeset
|
511 textUrl = textUrl.replace("/mpiwg/online",'',1) |
c048559460a3
fix problems with full text paths starting with /mpiwg/online
casties
parents:
63
diff
changeset
|
512 |
31 | 513 docinfo['textURL'] = textUrl |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
514 |
82 | 515 textUrls=dom.xpath("//texttool/text-url-path") |
516 if textUrls and (len(textUrls)>0): | |
517 textUrl=getTextFromNode(textUrls[0]) | |
83 | 518 docinfo['textURLPath'] = textUrl |
519 | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
520 presentationUrls=dom.xpath("//texttool/presentation") |
52 | 521 docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom) # get info von bib tag |
522 | |
523 if presentationUrls and (len(presentationUrls)>0): # ueberschreibe diese durch presentation informationen | |
82 | 524 # presentation url ergiebt sich ersetzen von index.meta in der url der fuer die Metadaten |
525 # durch den relativen Pfad auf die presentation infos | |
81
fae97f071724
fixed problem with info.xml when url without index.meta
casties
parents:
79
diff
changeset
|
526 presentationPath = getTextFromNode(presentationUrls[0]) |
fae97f071724
fixed problem with info.xml when url without index.meta
casties
parents:
79
diff
changeset
|
527 if url.endswith("index.meta"): |
fae97f071724
fixed problem with info.xml when url without index.meta
casties
parents:
79
diff
changeset
|
528 presentationUrl=url.replace('index.meta',presentationPath) |
fae97f071724
fixed problem with info.xml when url without index.meta
casties
parents:
79
diff
changeset
|
529 else: |
fae97f071724
fixed problem with info.xml when url without index.meta
casties
parents:
79
diff
changeset
|
530 presentationUrl=url + "/" + presentationPath |
83 | 531 docinfo=self.getNumPages('//pb', docinfo) #im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
532 docinfo = self.getBibinfoFromTextToolPresentation(presentationUrl,docinfo=docinfo,dom=dom) |
52 | 533 |
55
ba8917b93c84
fixed bug in fix for missing getauthinfo in getdocinfofromtexttool
casties
parents:
54
diff
changeset
|
534 docinfo = self.getAuthinfoFromIndexMeta(url,docinfo=docinfo,dom=dom) # get access info |
83 | 535 |
22 | 536 return docinfo |
537 | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
538 |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
539 def getBibinfoFromTextToolPresentation(self,url,docinfo=None,dom=None): |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
540 """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
|
541 """ |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
542 dom=self.getPresentationInfoXML(url) |
62 | 543 try: |
544 docinfo['author']=getTextFromNode(dom.xpath("//author")[0]) | |
545 except: | |
546 pass | |
547 try: | |
548 docinfo['title']=getTextFromNode(dom.xpath("//title")[0]) | |
549 except: | |
550 pass | |
551 try: | |
552 docinfo['year']=getTextFromNode(dom.xpath("//date")[0]) | |
553 except: | |
554 pass | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
555 return docinfo |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
556 |
70 | 557 def getDocinfoFromImagePath(self,path,docinfo=None,cut=0): |
22 | 558 """path ist the path to the images it assumes that the index.meta file is one level higher.""" |
52 | 559 logger("documentViewer (getdocinfofromimagepath)", logging.INFO,"path: %s"%(path)) |
22 | 560 if docinfo is None: |
561 docinfo = {} | |
29 | 562 path=path.replace("/mpiwg/online","") |
22 | 563 docinfo['imagePath'] = path |
73 | 564 docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo,cut=cut) |
78 | 565 |
79
df6952ac93e9
bug in getDocInforFromImagePath, relative lage der index.meta zu path war falsch.
dwinter
parents:
78
diff
changeset
|
566 pathorig=path |
78 | 567 for x in range(cut): |
568 path=getParentDir(path) | |
569 logging.error("PATH:"+path) | |
31 | 570 imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path |
22 | 571 docinfo['imageURL'] = imageUrl |
572 | |
79
df6952ac93e9
bug in getDocInforFromImagePath, relative lage der index.meta zu path war falsch.
dwinter
parents:
78
diff
changeset
|
573 #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
|
574 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
|
575 docinfo = self.getAuthinfoFromIndexMeta(pathorig,docinfo=docinfo,cut=cut+1) |
22 | 576 return docinfo |
20 | 577 |
22 | 578 |
579 def getDocinfo(self, mode, url): | |
580 """returns docinfo depending on mode""" | |
52 | 581 logger("documentViewer (getdocinfo)", logging.INFO,"mode: %s, url: %s"%(mode,url)) |
22 | 582 # look for cached docinfo in session |
51
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
583 if self.REQUEST.SESSION.has_key('docinfo'): |
22 | 584 docinfo = self.REQUEST.SESSION['docinfo'] |
585 # check if its still current | |
586 if docinfo is not None and docinfo.get('mode') == mode and docinfo.get('url') == url: | |
52 | 587 logger("documentViewer (getdocinfo)", logging.INFO,"docinfo in session: %s"%docinfo) |
22 | 588 return docinfo |
589 # new docinfo | |
590 docinfo = {'mode': mode, 'url': url} | |
591 if mode=="texttool": #index.meta with texttool information | |
592 docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo) | |
593 elif mode=="imagepath": | |
594 docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo) | |
70 | 595 elif mode=="filepath": |
75
9673218e155b
minorCVS: ----------------------------------------------------------------------
dwinter
parents:
74
diff
changeset
|
596 docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo,cut=1) |
22 | 597 else: |
52 | 598 logger("documentViewer (getdocinfo)", logging.ERROR,"unknown mode!") |
37 | 599 raise ValueError("Unknown mode %s"%(mode)) |
600 | |
52 | 601 logger("documentViewer (getdocinfo)", logging.INFO,"docinfo: %s"%docinfo) |
22 | 602 self.REQUEST.SESSION['docinfo'] = docinfo |
603 return docinfo | |
20 | 604 |
605 | |
25 | 606 def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None): |
22 | 607 """returns pageinfo with the given parameters""" |
608 pageinfo = {} | |
25 | 609 current = getInt(current) |
610 pageinfo['current'] = current | |
611 rows = int(rows or self.thumbrows) | |
612 pageinfo['rows'] = rows | |
613 cols = int(cols or self.thumbcols) | |
614 pageinfo['cols'] = cols | |
615 grpsize = cols * rows | |
616 pageinfo['groupsize'] = grpsize | |
61 | 617 start = getInt(start, default=(math.ceil(float(current)/float(grpsize))*grpsize-(grpsize-1))) |
618 # int(current / grpsize) * grpsize +1)) | |
22 | 619 pageinfo['start'] = start |
25 | 620 pageinfo['end'] = start + grpsize |
621 if docinfo is not None: | |
83 | 622 |
25 | 623 np = int(docinfo['numPages']) |
624 pageinfo['end'] = min(pageinfo['end'], np) | |
625 pageinfo['numgroups'] = int(np / grpsize) | |
626 if np % grpsize > 0: | |
627 pageinfo['numgroups'] += 1 | |
78 | 628 |
22 | 629 return pageinfo |
630 | |
0 | 631 def text(self,mode,url,pn): |
632 """give text""" | |
633 if mode=="texttool": #index.meta with texttool information | |
634 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | |
635 | |
35 | 636 #print textpath |
0 | 637 try: |
638 dom = NonvalidatingReader.parseUri(textpath) | |
639 except: | |
640 return None | |
641 | |
642 list=[] | |
643 nodes=dom.xpath("//pb") | |
644 | |
645 node=nodes[int(pn)-1] | |
646 | |
647 p=node | |
648 | |
649 while p.tagName!="p": | |
650 p=p.parentNode | |
651 | |
652 | |
653 endNode=nodes[int(pn)] | |
654 | |
655 | |
656 e=endNode | |
657 | |
658 while e.tagName!="p": | |
659 e=e.parentNode | |
660 | |
661 | |
662 next=node.parentNode | |
663 | |
664 #sammle s | |
665 while next and (next!=endNode.parentNode): | |
666 list.append(next) | |
667 next=next.nextSibling | |
668 list.append(endNode.parentNode) | |
669 | |
670 if p==e:# beide im selben paragraphen | |
20 | 671 pass |
672 # else: | |
673 # next=p | |
674 # while next!=e: | |
675 # print next,e | |
676 # list.append(next) | |
677 # next=next.nextSibling | |
678 # | |
679 # for x in list: | |
680 # PrettyPrint(x) | |
681 # | |
682 # return list | |
22 | 683 # |
684 | |
685 def findDigilibUrl(self): | |
686 """try to get the digilib URL from zogilib""" | |
687 url = self.imageViewerUrl[:-1] + "/getScalerUrl" | |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
688 #print urlparse.urlparse(url)[0] |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
689 #print urlparse.urljoin(self.absolute_url(),url) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
690 logging.info("finddigiliburl: %s"%urlparse.urlparse(url)[0]) |
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
691 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
|
692 |
22 | 693 try: |
46 | 694 if urlparse.urlparse(url)[0]=='': #relative path |
695 url=urlparse.urljoin(self.absolute_url()+"/",url) | |
696 | |
22 | 697 scaler = urlopen(url).read() |
698 return scaler.replace("/servlet/Scaler?", "") | |
699 except: | |
700 return None | |
701 | |
46 | 702 def changeDocumentViewer(self,imageViewerUrl,textViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None): |
22 | 703 """init document viewer""" |
704 self.title=title | |
705 self.imageViewerUrl=imageViewerUrl | |
46 | 706 self.textViewerUrl=textViewerUrl |
22 | 707 self.digilibBaseUrl = digilibBaseUrl |
25 | 708 self.thumbrows = thumbrows |
709 self.thumbcols = thumbcols | |
32 | 710 self.authgroups = [s.strip().lower() for s in authgroups.split(',')] |
22 | 711 if RESPONSE is not None: |
712 RESPONSE.redirect('manage_main') | |
0 | 713 |
714 | |
715 | |
716 | |
717 # security.declareProtected('View management screens','renameImageForm') | |
718 | |
719 def manage_AddDocumentViewerForm(self): | |
720 """add the viewer form""" | |
22 | 721 pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self) |
0 | 722 return pt() |
723 | |
46 | 724 def manage_AddDocumentViewer(self,id,imageViewerUrl="",textViewerUrl="",title="",RESPONSE=None): |
0 | 725 """add the viewer""" |
46 | 726 newObj=documentViewer(id,imageViewerUrl,title=title,textViewerUrl=textViewerUrl) |
0 | 727 self._setObject(id,newObj) |
728 | |
729 if RESPONSE is not None: | |
730 RESPONSE.redirect('manage_main') | |
22 | 731 |
732 | |
733 ## | |
734 ## DocumentViewerTemplate class | |
735 ## | |
736 class DocumentViewerTemplate(ZopePageTemplate): | |
737 """Template for document viewer""" | |
738 meta_type="DocumentViewer Template" | |
739 | |
740 | |
741 def manage_addDocumentViewerTemplateForm(self): | |
742 """Form for adding""" | |
743 pt=PageTemplateFile('zpt/addDocumentViewerTemplate', globals()).__of__(self) | |
744 return pt() | |
745 | |
746 def manage_addDocumentViewerTemplate(self, id='viewer_main', title=None, text=None, | |
747 REQUEST=None, submit=None): | |
748 "Add a Page Template with optional file content." | |
749 | |
750 self._setObject(id, DocumentViewerTemplate(id)) | |
751 ob = getattr(self, id) | |
53
f4e0af8c281d
NEW - # 44: ECHO - vollst?ndige bibliographische Angabe
dwinter
parents:
52
diff
changeset
|
752 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
|
753 logging.info("txt %s:"%txt) |
f4e0af8c281d
NEW - # 44: ECHO - vollst?ndige bibliographische Angabe
dwinter
parents:
52
diff
changeset
|
754 ob.pt_edit(txt,"text/html") |
22 | 755 if title: |
756 ob.pt_setTitle(title) | |
757 try: | |
758 u = self.DestinationURL() | |
759 except AttributeError: | |
760 u = REQUEST['URL1'] | |
761 | |
762 u = "%s/%s" % (u, urllib.quote(id)) | |
763 REQUEST.RESPONSE.redirect(u+'/manage_main') | |
764 return '' | |
765 | |
766 | |
41 | 767 |