File:  [Repository] / documentViewer / documentViewer.py
Revision 1.41: download - view: text, annotated - select for diffs - revision graph
Fri Feb 12 14:33:02 2010 UTC (14 years, 4 months ago) by abukhman
Branches: MAIN
CVS tags: HEAD
text-url-path einfŸgen

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

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