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