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