Diff for /documentViewer/documentViewer.py between versions 1.8 and 1.18

version 1.8, 2006/04/11 17:27:57 version 1.18, 2006/09/09 10:52:59
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 7  from Globals import package_home Line 9  from Globals import package_home
   
 from Ft.Xml.Domlette import NonvalidatingReader  from Ft.Xml.Domlette import NonvalidatingReader
 from Ft.Xml.Domlette import PrettyPrint, Print  from Ft.Xml.Domlette import PrettyPrint, Print
 from Ft.Xml import EMPTY_NAMESPACE  from Ft.Xml import EMPTY_NAMESPACE, Parse
   
 import Ft.Xml.XPath  import Ft.Xml.XPath
   
Line 16  import sys Line 18  import sys
 import cgi  import cgi
 import urllib  import urllib
 import zLOG  import zLOG
   import urlparse 
   
 def getInt(number, default=0):  def getInt(number, default=0):
     """returns always an int (0 in case of problems)"""      """returns always an int (0 in case of problems)"""
Line 24  def getInt(number, default=0): Line 27  def getInt(number, default=0):
     except:      except:
         return default          return default
           
   
 def getTextFromNode(nodename):  def getTextFromNode(nodename):
       """get the cdata content of a node"""
     if nodename is None:      if nodename is None:
         return ""          return ""
     nodelist=nodename.childNodes      nodelist=nodename.childNodes
Line 35  def getTextFromNode(nodename): Line 38  def getTextFromNode(nodename):
            rc = rc + node.data             rc = rc + node.data
     return rc      return rc
   
           
   def getParentDir(path):
       """returns pathname shortened by one"""
       return '/'.join(path.split('/')[0:-1])
           
   
 import socket  import socket
   
 def urlopen(url,timeout=2):  def urlopen(url,timeout=2):
Line 50  def urlopen(url,timeout=2): Line 59  def urlopen(url,timeout=2):
 ##  ##
 class documentViewer(Folder):  class documentViewer(Folder):
     """document viewer"""      """document viewer"""
       textViewerUrl="http://127.0.0.1:8080/HFQP/testXSLT/getPage?"
   
     meta_type="Document viewer"      meta_type="Document viewer"
           
Line 69  class documentViewer(Folder): Line 79  class documentViewer(Folder):
     changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())      changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())
   
           
     def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10,authgroups="mpiwg"):      def __init__(self,id,imageViewerUrl,textViewerUrl=None,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10,authgroups="mpiwg"):
         """init document viewer"""          """init document viewer"""
         self.id=id          self.id=id
         self.title=title          self.title=title
         self.imageViewerUrl=imageViewerUrl          self.imageViewerUrl=imageViewerUrl
           self.textViewerUrl=textViewerUrl
           
         if not digilibBaseUrl:          if not digilibBaseUrl:
             self.digilibBaseUrl = self.findDigilibUrl()              self.digilibBaseUrl = self.findDigilibUrl()
         else:          else:
Line 87  class documentViewer(Folder): Line 99  class documentViewer(Folder):
   
   
     security.declareProtected('View','index_html')      security.declareProtected('View','index_html')
     def index_html(self,mode,url,start=None,pn=1):      def index_html(self,mode,url,viewMode="images",start=None,pn=1):
         '''          '''
         view it          view it
         @param mode: defines which type of document is behind url          @param mode: defines which type of document is 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
           
         '''          '''
                   
         zLOG.LOG("documentViewer (index)", zLOG.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn))          zLOG.LOG("documentViewer (index)", zLOG.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn))
Line 106  class documentViewer(Folder): Line 120  class documentViewer(Folder):
         docinfo = self.getDocinfo(mode=mode,url=url)          docinfo = self.getDocinfo(mode=mode,url=url)
         pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)          pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
         pt = getattr(self.template, 'viewer_main')          pt = getattr(self.template, 'viewer_main')
         return pt(docinfo=docinfo,pageinfo=pageinfo)          return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode)
       
       
     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"""
         params=cgi.parse_qs(self.REQUEST['QUERY_STRING'])          params=self.REQUEST.form.copy()
         if param is not None:          if param is not None:
             if val is None:              if val is None:
                 if params.has_key(param):                  if params.has_key(param):
                     del params[param]                      del params[param]
             else:              else:
                 params[param] = [str(val)]                  params[param] = str(val)
                                   
         ps = "&".join(["%s=%s"%(k,urllib.quote(v[0])) for (k, v) in params.items()])          # quote values and assemble into query string
         url=self.REQUEST['URL']+"?"+ps          ps = "&".join(["%s=%s"%(k,urllib.quote(v)) for (k, v) in params.items()])
         #url=self.REQUEST['URL']+"?"+urllib.urlencode(params, doseq=True)          url=self.REQUEST['URL1']+"?"+ps
         return url          return url
   
           
Line 133  class documentViewer(Folder): Line 147  class documentViewer(Folder):
         else:          else:
             return style                  return style    
                   
     def accessOK(self, docinfo):          
       def isAccessible(self, docinfo):
         """returns if access to the resource is granted"""          """returns if access to the resource is granted"""
         access = docinfo.get('accessType', None)          access = docinfo.get('accessType', None)
         if access is None:          zLOG.LOG("documentViewer (accessOK)", zLOG.INFO, "access type %s"%access)
             # no information - no access (not yet)          if access is not None and access == 'free':
               zLOG.LOG("documentViewer (accessOK)", zLOG.INFO, "access is free")
             return True              return True
         elif access == 'free':          elif access is None or access in self.authgroups:
             return True              # only local access -- only logged in users
                       user = getSecurityManager().getUser()
         print "access: ", access, " authgroups: ", self.authgroups              if user is not None:
         if access in self.authgroups:                  #print "user: ", user
             # local access OK                  return (user.getUserName() != "Anonymous User")
             user = getSecurityManager().getUser().getUserName()              else:
             print "user: ", user                  return False
             return (user != "Anonymous User")  
                   
         zLOG.LOG("documentViewer (accessOK)", zLOG.INFO, "unknown access group %s"%access)          zLOG.LOG("documentViewer (accessOK)", zLOG.INFO, "unknown access type %s"%access)
         return False          return False
                                   
                   
     def getDirinfoFromDigilib(self,path,docinfo=None):      def getDirinfoFromDigilib(self,path,docinfo=None):
         """gibt param von dlInfo aus"""          """gibt param von dlInfo aus"""
           num_retries = 3
         if docinfo is None:          if docinfo is None:
             docinfo = {}              docinfo = {}
                           
         imageUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path          infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path
           
         zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo from %s"%(imageUrl))          zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo from %s"%(infoUrl))
                   
           for cnt in range(num_retries):
         try:          try:
             dom = NonvalidatingReader.parseUri(imageUrl)                  # dom = NonvalidatingReader.parseUri(imageUrl)
                   txt=urllib.urlopen(infoUrl).read()
                   dom = Parse(txt)
                   break
         except:          except:
             zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.ERROR, "error reading %s"%(imageUrl))                  zLOG.LOG("documentViewer (getdirinfofromdigilib)", zLOG.ERROR, "error reading %s (try %d)"%(infoUrl,cnt))
             raise IOError("Unable to get dirinfo from %s"%(imageUrl))          else:
               raise IOError("Unable to get dir-info from %s"%(infoUrl))
                   
         params=dom.xpath("//dir/size")          sizes=dom.xpath("//dir/size")
         zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo:size"%params)          zLOG.LOG("documentViewer (getparamfromdigilib)", zLOG.INFO, "dirInfo:size"%sizes)
                   
         if params:          if sizes:
             docinfo['numPages'] = int(getTextFromNode(params[0]))              docinfo['numPages'] = int(getTextFromNode(sizes[0]))
         else:          else:
             docinfo['numPages'] = 0              docinfo['numPages'] = 0
                                                   
         return docinfo          return docinfo
           
                           
       def getIndexMeta(self, url):
           """returns dom of index.meta document at url"""
           num_retries = 3
           dom = None
           metaUrl = None
           if url.startswith("http://"):
               # real URL
               metaUrl = url
           else:
               # online path
               server=self.digilibBaseUrl+"/servlet/Texter?fn="
               metaUrl=server+url.replace("/mpiwg/online","")
               if not metaUrl.endswith("index.meta"):
                   metaUrl += "/index.meta"
           print metaUrl
           for cnt in range(num_retries):
               try:
                   # patch dirk encoding fehler treten dann nicht mehr auf
                   # dom = NonvalidatingReader.parseUri(metaUrl)
                   txt=urllib.urlopen(metaUrl).read()
                   dom = Parse(txt)
                   break
               except:
                   zLOG.LOG("ERROR documentViewer (getIndexMata)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2])
                   
           if dom is None:
               raise IOError("Unable to read index meta from %s"%(url))
                    
           return dom
                           
           
     def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None):      def getAuthinfoFromIndexMeta(self,path,docinfo=None,dom=None):
         """gets authorization info from the index.meta file at url or given by dom"""          """gets authorization info from the index.meta file at path or given by dom"""
         zLOG.LOG("documentViewer (getbibinfofromindexmeta)", zLOG.INFO,"path: %s"%(path))          zLOG.LOG("documentViewer (getauthinfofromindexmeta)", zLOG.INFO,"path: %s"%(path))
                   
         access = None          access = None
                   
Line 189  class documentViewer(Folder): Line 241  class documentViewer(Folder):
             docinfo = {}              docinfo = {}
                           
         if dom is None:          if dom is None:
             server=self.digilibBaseUrl+"/servlet/Texter?fn="              dom = self.getIndexMeta(getParentDir(path))
             path="/".join(path.split("/")[0:-1])  
             metaUrl=server+path+"/index.meta"  
             try:  
                 dom = NonvalidatingReader.parseUri(metaUrl)  
             except:  
                 return docinfo  
                           
         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):
             access=acctype[0].value              access=acctype[0].value
             if access == 'group':              if access in ['group', 'institution']:
                 access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower()                  access = getTextFromNode(dom.xpath("//access-conditions/access/name")[0]).lower()
                           
         docinfo['accessType'] = access          docinfo['accessType'] = access
Line 208  class documentViewer(Folder): Line 254  class documentViewer(Folder):
           
                   
     def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None):      def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None):
         """gets bibliographical info from the index.meta file at url or given by dom"""          """gets bibliographical info from the index.meta file at path or given by dom"""
         zLOG.LOG("documentViewer (getbibinfofromindexmeta)", zLOG.INFO,"path: %s"%(path))          zLOG.LOG("documentViewer (getbibinfofromindexmeta)", zLOG.INFO,"path: %s"%(path))
                   
         if docinfo is None:          if docinfo is None:
             docinfo = {}              docinfo = {}
                           
         if dom is None:          if dom is None:
             server=self.digilibBaseUrl+"/servlet/Texter?fn="              dom = self.getIndexMeta(getParentDir(path))
             path="/".join(path.split("/")[0:-1])  
             metaUrl=server+path+"/index.meta"  
             try:  
                 dom = NonvalidatingReader.parseUri(metaUrl)  
             except:  
                 return docinfo  
                   
         metaData=self.metadata.main.meta.bib          metaData=self.metadata.main.meta.bib
         bibtype=dom.xpath("//bib/@type")          bibtype=dom.xpath("//bib/@type")
Line 231  class documentViewer(Folder): Line 271  class documentViewer(Folder):
             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)
         bibmap=metaData.generateMappingForType(bibtype)          bibmap=metaData.generateMappingForType(bibtype)
         print "bibmap: ", bibmap, " for: ", 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:
             docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])              docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])
Line 248  class documentViewer(Folder): Line 288  class documentViewer(Folder):
            docinfo = {}             docinfo = {}
                         
        if dom is None:         if dom is None:
            try:             dom = self.getIndexMeta(url)
                dom = NonvalidatingReader.parseUri(url)         
            except:         archivePath = None
                zLOG.LOG("documentViewer (parseUrlTexttool)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2])         archiveName = None
                raise IOError("Unable to get texttool info from %s"%(url))  
                 
        archiveNames=dom.xpath("//resource/name")         archiveNames=dom.xpath("//resource/name")
        if archiveNames and (len(archiveNames)>0):         if archiveNames and (len(archiveNames)>0):
            archiveName=getTextFromNode(archiveNames[0])             archiveName=getTextFromNode(archiveNames[0])
          else:
              zLOG.LOG("documentViewer (getdocinfofromtexttool)", zLOG.WARNING,"resource/name missing in: %s"%(url))
                 
        archivePaths=dom.xpath("//resource/archive-path")         archivePaths=dom.xpath("//resource/archive-path")
        if archivePaths and (len(archivePaths)>0):         if archivePaths and (len(archivePaths)>0):
Line 264  class documentViewer(Folder): Line 305  class documentViewer(Folder):
            # clean up archive path             # clean up archive path
            if archivePath[0] != '/':             if archivePath[0] != '/':
                archivePath = '/' + archivePath                 archivePath = '/' + archivePath
            if not archivePath.endswith(archiveName):             if archiveName and (not archivePath.endswith(archiveName)):
                archivePath += "/" + archiveName                 archivePath += "/" + archiveName
        else:         else:
            archivePath=None             # try to get archive-path from url
              zLOG.LOG("documentViewer (getdocinfofromtexttool)", zLOG.WARNING,"resource/archive-path missing in: %s"%(url))
              if (not url.startswith('http')):
                  archivePath = url.replace('index.meta', '')
                  
          if archivePath is None:
              # we balk without archive-path
              raise IOError("Missing archive-path (for text-tool) in %s"%(url))
          
          imageDirs=dom.xpath("//texttool/image")
          if imageDirs and (len(imageDirs)>0):
              imageDir=getTextFromNode(imageDirs[0])
          else:
              # we balk with no image tag
              raise IOError("No text-tool info in %s"%(url))
                 
        images=dom.xpath("//texttool/image")         if imageDir and archivePath:
        if images and (len(images)>0):             #print "image: ", imageDir, " archivepath: ", archivePath
            image=getTextFromNode(images[0])             imageDir=os.path.join(archivePath,imageDir)
        else:             imageDir=imageDir.replace("/mpiwg/online",'')
            image=None             docinfo=self.getDirinfoFromDigilib(imageDir,docinfo=docinfo)
                         docinfo['imagePath'] = imageDir
        if image and archivePath:             docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+imageDir
            print "image: ", image, " archivepath: ", archivePath  
            image=os.path.join(archivePath,image)  
            image=image.replace("/mpiwg/online",'')  
            docinfo=self.getDirinfoFromDigilib(image,docinfo=docinfo)  
            docinfo['imagePath'] = image  
            docinfo['imageURL'] = self.digilibBaseUrl+"/servlet/Scaler?fn="+image  
                         
        viewerUrls=dom.xpath("//texttool/digiliburlprefix")         viewerUrls=dom.xpath("//texttool/digiliburlprefix")
        if viewerUrls and (len(viewerUrls)>0):         if viewerUrls and (len(viewerUrls)>0):
Line 332  class documentViewer(Folder): Line 381  class documentViewer(Folder):
             docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo)              docinfo = self.getDocinfoFromImagePath(url, docinfo=docinfo)
         else:          else:
             zLOG.LOG("documentViewer (getdocinfo)", zLOG.ERROR,"unknown mode!")              zLOG.LOG("documentViewer (getdocinfo)", zLOG.ERROR,"unknown mode!")
               raise ValueError("Unknown mode %s"%(mode))
                           
         zLOG.LOG("documentViewer (getdocinfo)", zLOG.INFO,"docinfo: %s"%docinfo)          zLOG.LOG("documentViewer (getdocinfo)", zLOG.INFO,"docinfo: %s"%docinfo)
         self.REQUEST.SESSION['docinfo'] = docinfo          self.REQUEST.SESSION['docinfo'] = docinfo
         return docinfo          return docinfo
Line 365  class documentViewer(Folder): Line 416  class documentViewer(Folder):
         if mode=="texttool": #index.meta with texttool information          if mode=="texttool": #index.meta with texttool information
             (viewerUrl,imagepath,textpath)=parseUrlTextTool(url)              (viewerUrl,imagepath,textpath)=parseUrlTextTool(url)
                   
         print textpath          #print textpath
         try:          try:
             dom = NonvalidatingReader.parseUri(textpath)              dom = NonvalidatingReader.parseUri(textpath)
         except:          except:
Line 417  class documentViewer(Folder): Line 468  class documentViewer(Folder):
     def findDigilibUrl(self):      def findDigilibUrl(self):
         """try to get the digilib URL from zogilib"""          """try to get the digilib URL from zogilib"""
         url = self.imageViewerUrl[:-1] + "/getScalerUrl"          url = self.imageViewerUrl[:-1] + "/getScalerUrl"
           print urlparse.urlparse(url)[0]
           print urlparse.urljoin(self.absolute_url(),url)
         try:          try:
               if urlparse.urlparse(url)[0]=='': #relative path
                   url=urlparse.urljoin(self.absolute_url()+"/",url)
                   
             scaler = urlopen(url).read()              scaler = urlopen(url).read()
             return scaler.replace("/servlet/Scaler?", "")              return scaler.replace("/servlet/Scaler?", "")
         except:          except:
             return None              return None
           
     def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None):      def changeDocumentViewer(self,imageViewerUrl,textViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,authgroups='mpiwg',RESPONSE=None):
         """init document viewer"""          """init document viewer"""
         self.title=title          self.title=title
         self.imageViewerUrl=imageViewerUrl          self.imageViewerUrl=imageViewerUrl
           self.textViewerUrl=textViewerUrl
         self.digilibBaseUrl = digilibBaseUrl          self.digilibBaseUrl = digilibBaseUrl
         self.thumbrows = thumbrows          self.thumbrows = thumbrows
         self.thumbcols = thumbcols          self.thumbcols = thumbcols
Line 444  def manage_AddDocumentViewerForm(self): Line 501  def manage_AddDocumentViewerForm(self):
     pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self)      pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self)
     return pt()      return pt()
       
 def manage_AddDocumentViewer(self,id,imageViewerUrl="",title="",RESPONSE=None):  def manage_AddDocumentViewer(self,id,imageViewerUrl="",textViewerUrl="",title="",RESPONSE=None):
     """add the viewer"""      """add the viewer"""
     newObj=documentViewer(id,imageViewerUrl,title)      newObj=documentViewer(id,imageViewerUrl,title=title,textViewerUrl=textViewerUrl)
     self._setObject(id,newObj)      self._setObject(id,newObj)
           
     if RESPONSE is not None:      if RESPONSE is not None:

Removed from v.1.8  
changed lines
  Added in v.1.18


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