Diff for /documentViewer/documentViewer.py between versions 1.24 and 1.42

version 1.24, 2007/05/03 17:28:23 version 1.42, 2010/03/08 15:02:33
Line 1 Line 1
   
   
 from OFS.Folder import Folder  from OFS.Folder import Folder
 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate  from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
 from Products.PageTemplates.PageTemplateFile import PageTemplateFile   from Products.PageTemplates.PageTemplateFile import PageTemplateFile 
Line 11  from Ft.Xml.Domlette import Nonvalidatin Line 10  from Ft.Xml.Domlette import Nonvalidatin
 from Ft.Xml.Domlette import PrettyPrint, Print  from Ft.Xml.Domlette import PrettyPrint, Print
 from Ft.Xml import EMPTY_NAMESPACE, Parse  from Ft.Xml import EMPTY_NAMESPACE, Parse
   
 import Ft.Xml.XPath  
   
   import Ft.Xml.XPath
   import xmlrpclib
 import os.path  import os.path
 import sys  import sys
 import cgi  import cgi
 import urllib  import urllib
 import logging  import logging
   import math
   
 import urlparse   import urlparse 
   from types import *
 def logger(txt,method,txt2):  def logger(txt,method,txt2):
     """logging"""      """logging"""
     logging.info(txt+ txt2)      logging.info(txt+ txt2)
Line 31  def getInt(number, default=0): Line 32  def getInt(number, default=0):
     try:      try:
         return int(number)          return int(number)
     except:      except:
         return default          return int(default)
   
 def getTextFromNode(nodename):  def getTextFromNode(nodename):
     """get the cdata content of a node"""      """get the cdata content of a node"""
Line 80  class documentViewer(Folder): Line 81  class documentViewer(Folder):
     image_main = PageTemplateFile('zpt/image_main', globals())      image_main = PageTemplateFile('zpt/image_main', globals())
     head_main = PageTemplateFile('zpt/head_main', globals())      head_main = PageTemplateFile('zpt/head_main', globals())
     docuviewer_css = PageTemplateFile('css/docuviewer.css', globals())      docuviewer_css = PageTemplateFile('css/docuviewer.css', globals())
       info_xml = PageTemplateFile('zpt/info_xml', globals())
   
       thumbs_main_rss = PageTemplateFile('zpt/thumbs_main_rss', globals())
     security.declareProtected('View management screens','changeDocumentViewerForm')          security.declareProtected('View management screens','changeDocumentViewerForm')    
     changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())      changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())
   
Line 104  class documentViewer(Folder): Line 107  class documentViewer(Folder):
         self.manage_addFolder('template')          self.manage_addFolder('template')
   
   
       security.declareProtected('View','thumbs_rss')
       def thumbs_rss(self,mode,url,viewMode="auto",start=None,pn=1):
           '''
           view it
           @param mode: defines how to access the document behind url 
           @param url: url which contains display information
           @param viewMode: if images display images, if text display text, default is images (text,images or auto)
           
           '''
           logging.info("HHHHHHHHHHHHHH:load the rss")
           logger("documentViewer (index)", logging.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn))
           
           if not hasattr(self, 'template'):
               # create template folder if it doesn't exist
               self.manage_addFolder('template')
               
           if not self.digilibBaseUrl:
               self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
               
           docinfo = self.getDocinfo(mode=mode,url=url)
           pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
           pt = getattr(self.template, 'thumbs_main_rss')
           
           if viewMode=="auto": # automodus gewaehlt
               if docinfo.get("textURL",'') and self.textViewerUrl: #texturl gesetzt und textViewer konfiguriert
                   viewMode="text"
               else:
                   viewMode="images"
                  
           return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode)
     
     security.declareProtected('View','index_html')      security.declareProtected('View','index_html')
     def index_html(self,mode,url,viewMode="auto",start=None,pn=1):      def index_html(self,mode,url,viewMode="auto",start=None,pn=1,mk=None):
         '''          '''
         view it          view it
         @param mode: defines which type of document is behind url (text,images or auto)          @param mode: defines how to access the document behind url 
         @param url: url which contains display information          @param url: url which contains display information
         @param viewMode: if images display images, if text display text, default is images          @param viewMode: if images display images, if text display text, default is images (text,images or auto)
                   
         '''          '''
                   
Line 133  class documentViewer(Folder): Line 167  class documentViewer(Folder):
             else:              else:
                 viewMode="images"                  viewMode="images"
                                 
           return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode,mk=self.generateMarks(mk))
   
         return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode)      def generateMarks(self,mk):
           ret=""
       if mk is None:
           return ""
       
       if type(mk) is not ListType:
           mk=[mk]
           for m in mk:
               ret+="mk=%s"%m
           return ret
       
     def getLink(self,param=None,val=None):      def getLink(self,param=None,val=None):
         """link to documentviewer with parameter param set to val"""          """link to documentviewer with parameter param set to val"""
Line 146  class documentViewer(Folder): Line 189  class documentViewer(Folder):
                     del params[param]                      del params[param]
             else:              else:
                 params[param] = str(val)                  params[param] = str(val)
           if params["mode"] == "filepath": #wenn beim erst Aufruf filepath gesetzt wurde aendere das nun zu imagepath
                   params["mode"] = "imagepath"
                   params["url"] = getParentDir(params["url"])
                                   
         # quote values and assemble into query string          # quote values and assemble into query string
         ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()])          ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()])
         url=self.REQUEST['URL1']+"?"+ps          url=self.REQUEST['URL1']+"?"+ps
         return url          return url
   
       def getLinkAmp(self,param=None,val=None):
           """link to documentviewer with parameter param set to val"""
           params=self.REQUEST.form.copy()
           if param is not None:
               if val is None:
                   if params.has_key(param):
                       del params[param]
               else:
                   params[param] = str(val)
                   
           # quote values and assemble into query string
           logging.info("XYXXXXX: %s"%repr(params.items()))
           ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()])
           url=self.REQUEST['URL1']+"?"+ps
           return url
       
       def getInfo_xml(self,url,mode):
           """returns info about the document as XML"""
   
           if not self.digilibBaseUrl:
               self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
           
           docinfo = self.getDocinfo(mode=mode,url=url)
           pt = getattr(self.template, 'info_xml')
           return pt(docinfo=docinfo)
   
           
     def getStyle(self, idx, selected, style=""):      def getStyle(self, idx, selected, style=""):
         """returns a string with the given style and append 'sel' if path == selected."""          """returns a string with the given style and append 'sel' if path == selected."""
Line 161  class documentViewer(Folder): Line 233  class documentViewer(Folder):
         else:          else:
             return style              return style
                   
       def getTextLanguage(self,url,docinfo):
           docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0])
           lang = urlencode({'':docinfo['lang']})
           return lang
           
                   
     def isAccessible(self, docinfo):      def isAccessible(self, docinfo):
         """returns if access to the resource is granted"""          """returns if access to the resource is granted"""
Line 182  class documentViewer(Folder): Line 259  class documentViewer(Folder):
         return False          return False
           
                                   
     def getDirinfoFromDigilib(self,path,docinfo=None):      def getDirinfoFromDigilib(self,path,docinfo=None,cut=0):
         """gibt param von dlInfo aus"""          """gibt param von dlInfo aus"""
         num_retries = 3          num_retries = 3
         if docinfo is None:          if docinfo is None:
             docinfo = {}              docinfo = {}
                           
           for x in range(cut):
                  
                   path=getParentDir(path)
          
         infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path          infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path
           
         logger("documentViewer (getparamfromdigilib)", logging.INFO, "dirInfo from %s"%(infoUrl))          logger("documentViewer (getparamfromdigilib)", logging.INFO, "dirInfo from %s"%(infoUrl))
Line 228  class documentViewer(Folder): Line 309  class documentViewer(Folder):
             metaUrl=server+url.replace("/mpiwg/online","")              metaUrl=server+url.replace("/mpiwg/online","")
             if not metaUrl.endswith("index.meta"):              if not metaUrl.endswith("index.meta"):
                 metaUrl += "/index.meta"                  metaUrl += "/index.meta"
         print metaUrl          logging.debug("METAURL: %s"%metaUrl)
         for cnt in range(num_retries):          for cnt in range(num_retries):
             try:              try:
                 # patch dirk encoding fehler treten dann nicht mehr auf                  # patch dirk encoding fehler treten dann nicht mehr auf
Line 274  class documentViewer(Folder): Line 355  class documentViewer(Folder):
         return dom          return dom
                                                   
                   
     def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None):      def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0):
         """gets authorization info from the index.meta file at path or given by dom"""          """gets authorization info from the index.meta file at path or given by dom"""
         logger("documentViewer (getauthinfofromindexmeta)", logging.INFO,"path: %s"%(path))          logger("documentViewer (getauthinfofromindexmeta)", logging.INFO,"path: %s"%(path))
                   
Line 284  class documentViewer(Folder): Line 365  class documentViewer(Folder):
             docinfo = {}              docinfo = {}
                           
         if dom is None:          if dom is None:
             dom = self.getIndexMeta(getParentDir(path))              for x in range(cut):
                   path=getParentDir(path)
               dom = self.getIndexMeta(path)
                 
         acctype = dom.xpath("//access-conditions/access/@type")          acctype = dom.xpath("//access-conditions/access/@type")
         if acctype and (len(acctype)>0):          if acctype and (len(acctype)>0):
Line 296  class documentViewer(Folder): Line 379  class documentViewer(Folder):
         return docinfo          return docinfo
           
                   
     def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None):      def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None,cut=0):
         """gets bibliographical info from the index.meta file at path or given by dom"""          """gets bibliographical info from the index.meta file at path or given by dom"""
         logger("documentViewer (getbibinfofromindexmeta)", logging.INFO,"path: %s"%(path))          logging.debug("documentViewer (getbibinfofromindexmeta) path: %s"%(path))
                   
         if docinfo is None:          if docinfo is None:
             docinfo = {}              docinfo = {}
                           
         if dom is None:          if dom is None:
             dom = self.getIndexMeta(getParentDir(path))              for x in range(cut):
                   path=getParentDir(path)
               dom = self.getIndexMeta(path)
           
           logging.debug("documentViewer (getbibinfofromindexmeta cutted) path: %s"%(path))
           # put in all raw bib fields as dict "bib"
           bib = dom.xpath("//bib/*")
           if bib and len(bib)>0:
               bibinfo = {}
               for e in bib:
                   bibinfo[e.localName] = getTextFromNode(e)
               docinfo['bib'] = bibinfo
                           
           # extract some fields (author, title, year) according to their mapping
         metaData=self.metadata.main.meta.bib          metaData=self.metadata.main.meta.bib
         bibtype=dom.xpath("//bib/@type")          bibtype=dom.xpath("//bib/@type")
         if bibtype and (len(bibtype)>0):          if bibtype and (len(bibtype)>0):
             bibtype=bibtype[0].value              bibtype=bibtype[0].value
         else:          else:
             bibtype="generic"              bibtype="generic"
               
         bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC)          bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC)
           docinfo['bib_type'] = bibtype
         bibmap=metaData.generateMappingForType(bibtype)          bibmap=metaData.generateMappingForType(bibtype)
         #print "bibmap: ", bibmap, " for: ", bibtype  
         # if there is no mapping bibmap is empty (mapping sometimes has empty fields)          # if there is no mapping bibmap is empty (mapping sometimes has empty fields)
         if len(bibmap) > 0 and len(bibmap['author'][0]) > 0:          if len(bibmap) > 0 and len(bibmap['author'][0]) > 0:
               try:
             docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])              docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])
               except: pass
               try:
             docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0])              docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0])
               except: pass
               try:
             docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0])              docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0])
                           except: pass
             logging.info("bla")              logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype)
             try:              try:
                 docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0])                  docinfo['lang']=getTextFromNode(dom.xpath("//bib/lang")[0])
             except:              except:
                 docinfo['lang']=''                  docinfo['lang']=''
   
           return docinfo
       
       
       def getNumPages(self, xquery, docinfo=None): #New Method 24.02.2010
          text=self.viewerTemplates.query.eval("/mpdl/interface/xquery.xql","document="+ docinfo['textURLPath'] +"&xquery="+str(xquery))
          docinfo['numPages'] = text.count("<pb ")
         return docinfo          return docinfo
   
                   
Line 370  class documentViewer(Folder): Line 478  class documentViewer(Folder):
        imageDirs=dom.xpath("//texttool/image")         imageDirs=dom.xpath("//texttool/image")
        if imageDirs and (len(imageDirs)>0):         if imageDirs and (len(imageDirs)>0):
            imageDir=getTextFromNode(imageDirs[0])             imageDir=getTextFromNode(imageDirs[0])
              
        else:         else:
            # we balk with no image tag / not necessary anymore because textmode is now standard             # we balk with no image tag / not necessary anymore because textmode is now standard
            #raise IOError("No text-tool info in %s"%(url))             #raise IOError("No text-tool info in %s"%(url))
            imageDir=""             imageDir=""
            docinfo['numPages']=1 # im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht             #xquery="//pb"  
          
            docinfo['imagePath'] = "" # keine Bilder             docinfo['imagePath'] = "" # keine Bilder
            docinfo['imageURL'] = ""             docinfo['imageURL'] = ""
   
Line 385  class documentViewer(Folder): Line 493  class documentViewer(Folder):
            imageDir=imageDir.replace("/mpiwg/online",'')             imageDir=imageDir.replace("/mpiwg/online",'')
            docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo)             docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo)
            docinfo['imagePath'] = imageDir             docinfo['imagePath'] = imageDir
              
            docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir             docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir
                         
        viewerUrls=dom.xpath("//texttool/digiliburlprefix")         viewerUrls=dom.xpath("//texttool/digiliburlprefix")
Line 397  class documentViewer(Folder): Line 506  class documentViewer(Folder):
            textUrl=getTextFromNode(textUrls[0])             textUrl=getTextFromNode(textUrls[0])
            if urlparse.urlparse(textUrl)[0]=="": #keine url             if urlparse.urlparse(textUrl)[0]=="": #keine url
                textUrl=os.path.join(archivePath,textUrl)                  textUrl=os.path.join(archivePath,textUrl) 
              # fix URLs starting with /mpiwg/online
              if textUrl.startswith("/mpiwg/online"):
                  textUrl = textUrl.replace("/mpiwg/online",'',1)
   
            docinfo['textURL'] = textUrl             docinfo['textURL'] = textUrl
         
          textUrls=dom.xpath("//texttool/text-url-path")
          if textUrls and (len(textUrls)>0):
              textUrl=getTextFromNode(textUrls[0])
              docinfo['textURLPath'] = textUrl   
           
        presentationUrls=dom.xpath("//texttool/presentation")         presentationUrls=dom.xpath("//texttool/presentation")
        docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)   # get info von bib tag         docinfo = self.getBibinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)   # get info von bib tag
                 
        if presentationUrls and (len(presentationUrls)>0): # ueberschreibe diese durch presentation informationen          if presentationUrls and (len(presentationUrls)>0): # ueberschreibe diese durch presentation informationen 
             # presentation url ergiebt sich ersetzen von index.meta in der url der fŸr die Metadaten              # presentation url ergiebt sich ersetzen von index.meta in der url der fuer die Metadaten
             # durch den relativen Pfad auf die presentation infos              # durch den relativen Pfad auf die presentation infos
            presentationUrl=url.replace('index.meta',getTextFromNode(presentationUrls[0]))             presentationPath = getTextFromNode(presentationUrls[0])
              if url.endswith("index.meta"): 
                  presentationUrl=url.replace('index.meta',presentationPath)
              else:
                  presentationUrl=url + "/" + presentationPath
              docinfo=self.getNumPages('//pb', docinfo) #im moment einfach auf eins setzen, navigation ueber die thumbs geht natuerlich nicht    
            docinfo = self.getBibinfoFromTextToolPresentation(presentationUrl,docinfo=docinfo,dom=dom)             docinfo = self.getBibinfoFromTextToolPresentation(presentationUrl,docinfo=docinfo,dom=dom)
   
        docinfo = self.getAuthinfoFromIndexMeta(path,docinfo=docinfo)         docinfo = self.getAuthinfoFromIndexMeta(url,docinfo=docinfo,dom=dom)   # get access info
          
        return docinfo         return docinfo
         
         
Line 417  class documentViewer(Folder): Line 540  class documentViewer(Folder):
         """gets the bibliographical information from the preseantion entry in texttools          """gets the bibliographical information from the preseantion entry in texttools
         """          """
         dom=self.getPresentationInfoXML(url)          dom=self.getPresentationInfoXML(url)
           try:
         docinfo['author']=getTextFromNode(dom.xpath("//author")[0])          docinfo['author']=getTextFromNode(dom.xpath("//author")[0])
           except:
               pass
           try:
         docinfo['title']=getTextFromNode(dom.xpath("//title")[0])          docinfo['title']=getTextFromNode(dom.xpath("//title")[0])
           except:
               pass
           try:
         docinfo['year']=getTextFromNode(dom.xpath("//date")[0])          docinfo['year']=getTextFromNode(dom.xpath("//date")[0])
           except:
               pass
         return docinfo          return docinfo
           
     def getDocinfoFromImagePath(self,path,docinfo=None):      def getDocinfoFromImagePath(self,path,docinfo=None,cut=0):
         """path ist the path to the images it assumes that the index.meta file is one level higher."""          """path ist the path to the images it assumes that the index.meta file is one level higher."""
         logger("documentViewer (getdocinfofromimagepath)", logging.INFO,"path: %s"%(path))          logger("documentViewer (getdocinfofromimagepath)", logging.INFO,"path: %s"%(path))
         if docinfo is None:          if docinfo is None:
             docinfo = {}              docinfo = {}
         path=path.replace("/mpiwg/online","")          path=path.replace("/mpiwg/online","")
         docinfo['imagePath'] = path          docinfo['imagePath'] = path
         docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo)          docinfo=self.getDirinfoFromDigilib(path,docinfo=docinfo,cut=cut)
           
           pathorig=path
           for x in range(cut):       
                   path=getParentDir(path)
           logging.error("PATH:"+path)
         imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path          imageUrl=self.digilibBaseUrl+"/servlet/Scaler?fn="+path
         docinfo['imageURL'] = imageUrl          docinfo['imageURL'] = imageUrl
                   
         docinfo = self.getBibinfoFromIndexMeta(path,docinfo=docinfo)          #path ist the path to the images it assumes that the index.meta file is one level higher.
         docinfo = self.getAuthinfoFromIndexMeta(path,docinfo=docinfo)          docinfo = self.getBibinfoFromIndexMeta(pathorig,docinfo=docinfo,cut=cut+1)
           docinfo = self.getAuthinfoFromIndexMeta(pathorig,docinfo=docinfo,cut=cut+1)
         return docinfo          return docinfo
           
           
Line 454  class documentViewer(Folder): Line 592  class documentViewer(Folder):
             docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo)              docinfo = self.getDocinfoFromTextTool(url, docinfo=docinfo)
         elif mode=="imagepath":          elif mode=="imagepath":
             docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo)              docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo)
           elif mode=="filepath":
               docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo,cut=1)
         else:          else:
             logger("documentViewer (getdocinfo)", logging.ERROR,"unknown mode!")              logger("documentViewer (getdocinfo)", logging.ERROR,"unknown mode!")
             raise ValueError("Unknown mode %s"%(mode))              raise ValueError("Unknown mode %s"%(mode))
Line 474  class documentViewer(Folder): Line 614  class documentViewer(Folder):
         pageinfo['cols'] = cols          pageinfo['cols'] = cols
         grpsize = cols * rows          grpsize = cols * rows
         pageinfo['groupsize'] = grpsize          pageinfo['groupsize'] = grpsize
         start = getInt(start, default=(int(current / grpsize) * grpsize +1))          start = getInt(start, default=(math.ceil(float(current)/float(grpsize))*grpsize-(grpsize-1)))
           # int(current / grpsize) * grpsize +1))
         pageinfo['start'] = start          pageinfo['start'] = start
         pageinfo['end'] = start + grpsize          pageinfo['end'] = start + grpsize
         if docinfo is not None:          if docinfo is not None:
               
             np = int(docinfo['numPages'])              np = int(docinfo['numPages'])
             pageinfo['end'] = min(pageinfo['end'], np)              pageinfo['end'] = min(pageinfo['end'], np)
             pageinfo['numgroups'] = int(np / grpsize)              pageinfo['numgroups'] = int(np / grpsize)

Removed from v.1.24  
changed lines
  Added in v.1.42


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>