comparison documentViewer.py @ 587:6000c7e24d8a

new parameter "pf" to specify image file name. (still some issues)
author casties
date Thu, 15 Nov 2012 17:09:45 +0100
parents 83eeed69793f
children d8d6975cebcb
comparison
equal deleted inserted replaced
586:230ba358da3c 587:6000c7e24d8a
44 """returns pathname shortened by cnt""" 44 """returns pathname shortened by cnt"""
45 # make sure path doesn't end with / 45 # make sure path doesn't end with /
46 path = path.rstrip('/') 46 path = path.rstrip('/')
47 # split by /, shorten, and reassemble 47 # split by /, shorten, and reassemble
48 return '/'.join(path.split('/')[0:-cnt]) 48 return '/'.join(path.split('/')[0:-cnt])
49
50 def getPnForPf(docinfo, pf):
51 """returns image number for image file name or 0"""
52 if 'imgFileNames' in docinfo:
53 pn = docinfo['imgFileNames'].get(pf, None)
54 if pn is None:
55 # try to cut extension
56 xi = pf.rfind('.')
57 if xi > 0:
58 pf = pf[:xi]
59 # try again, else return 0
60 pn = docinfo['imgFileNames'].get(pf, 0)
61
62 return pn
63
64 return 0
49 65
50 66
51 ## 67 ##
52 ## documentViewer class 68 ## documentViewer class
53 ## 69 ##
202 if not self.digilibBaseUrl: 218 if not self.digilibBaseUrl:
203 self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary" 219 self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
204 220
205 docinfo = self.getDocinfo(mode=mode,url=url) 221 docinfo = self.getDocinfo(mode=mode,url=url)
206 #pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo) 222 #pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
207 pageinfo = self.getPageinfo(start=start,current=pn, docinfo=docinfo) 223 pageinfo = self.getPageinfo(start=start,pn=pn, docinfo=docinfo)
208 ''' ZDES ''' 224 ''' ZDES '''
209 pt = getattr(self.template, 'thumbs_main_rss') 225 pt = getattr(self.template, 'thumbs_main_rss')
210 226
211 if viewMode=="auto": # automodus gewaehlt 227 if viewMode=="auto": # automodus gewaehlt
212 if docinfo.has_key("textURL") or docinfo.get('textURLPath',None): #texturl gesetzt und textViewer konfiguriert 228 if docinfo.has_key("textURL") or docinfo.get('textURLPath',None): #texturl gesetzt und textViewer konfiguriert
216 232
217 return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode) 233 return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode)
218 234
219 235
220 security.declareProtected('View','index_html') 236 security.declareProtected('View','index_html')
221 def index_html(self,url,mode="texttool",viewMode="auto",viewLayer=None,tocMode=None,start=1,pn=1): 237 def index_html(self,url,mode="texttool",viewMode="auto",viewLayer=None,tocMode=None,start=None,pn=None,pf=None):
222 """ 238 """
223 show page 239 show page
224 @param url: url which contains display information 240 @param url: url which contains display information
225 @param mode: defines how to access the document behind url 241 @param mode: defines how to access the document behind url
226 @param viewMode: 'images': display images, 'text': display text, 'xml': display xml, default is 'auto' 242 @param viewMode: 'images': display images, 'text': display text, 'xml': display xml, default is 'auto'
227 @param viewLayer: sub-type of viewMode, e.g. layer 'dict' for viewMode='text' 243 @param viewLayer: sub-type of viewMode, e.g. layer 'dict' for viewMode='text'
228 @param tocMode: type of 'table of contents' for navigation (thumbs, text, figures, none) 244 @param tocMode: type of 'table of contents' for navigation (thumbs, text, figures, none)
229 """ 245 """
230 246
231 logging.debug("documentViewer(index_html) mode=%s url=%s viewMode=%s viewLayer=%s start=%s pn=%s"%(mode,url,viewMode,viewLayer,start,pn)) 247 logging.debug("documentViewer(index_html) mode=%s url=%s viewMode=%s viewLayer=%s start=%s pn=%s pf=%s"%(mode,url,viewMode,viewLayer,start,pn,pf))
232 248
233 if not hasattr(self, 'template'): 249 if not hasattr(self, 'template'):
234 # this won't work 250 # this won't work
235 logging.error("template folder missing!") 251 logging.error("template folder missing!")
236 return "ERROR: template folder missing!" 252 return "ERROR: template folder missing!"
269 285
270 # safe viewLayer in userinfo 286 # safe viewLayer in userinfo
271 userinfo['viewLayer'] = viewLayer 287 userinfo['viewLayer'] = viewLayer
272 288
273 # pageinfo: information about page (not cached) 289 # pageinfo: information about page (not cached)
274 pageinfo = self.getPageinfo(start=start, current=pn, docinfo=docinfo, userinfo=userinfo, viewMode=viewMode, viewLayer=viewLayer, tocMode=tocMode) 290 pageinfo = self.getPageinfo(start=start, pn=pn, pf=pf, docinfo=docinfo, userinfo=userinfo, viewMode=viewMode, viewLayer=viewLayer, tocMode=tocMode)
275 291
276 # get template /template/viewer_$viewMode 292 # get template /template/viewer_$viewMode
277 pt = getattr(self.template, 'viewer_%s'%viewMode, None) 293 pt = getattr(self.template, 'viewer_%s'%viewMode, None)
278 if pt is None: 294 if pt is None:
279 logging.error("No template for viewMode=%s!"%viewMode) 295 logging.error("No template for viewMode=%s!"%viewMode)
449 return (user is not None) 465 return (user is not None)
450 466
451 logging.error("documentViewer (accessOK) unknown access type %s"%access) 467 logging.error("documentViewer (accessOK) unknown access type %s"%access)
452 return False 468 return False
453 469
454
455 def getUserinfo(self): 470 def getUserinfo(self):
456 """returns userinfo object""" 471 """returns userinfo object"""
457 logging.debug("getUserinfo") 472 logging.debug("getUserinfo")
458 userinfo = {} 473 userinfo = {}
459 # look for cached userinfo in session 474 # look for cached userinfo in session
731 pass 746 pass
732 747
733 return docinfo 748 return docinfo
734 749
735 def getDocinfoFromDigilib(self, docinfo, path): 750 def getDocinfoFromDigilib(self, docinfo, path):
736 infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path 751 infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?fn="+path
737 # fetch data 752 # fetch data
738 txt = getHttpData(infoUrl) 753 txt = getHttpData(infoUrl)
739 if not txt: 754 if not txt:
740 logging.error("Unable to get dir-info from %s"%(infoUrl)) 755 logging.error("Unable to get dir-info from %s"%(infoUrl))
741 return docinfo 756 return docinfo
742 757
743 dom = ET.fromstring(txt) 758 dom = ET.fromstring(txt)
744 size = getText(dom.find("size")) 759 dir = dom
760 # save size
761 size = dir.findtext('size')
745 logging.debug("getDocinfoFromDigilib: size=%s"%size) 762 logging.debug("getDocinfoFromDigilib: size=%s"%size)
746 if size: 763 if size:
747 docinfo['numPages'] = int(size) 764 docinfo['numPages'] = int(size)
748 else: 765 else:
749 docinfo['numPages'] = 0 766 docinfo['numPages'] = 0
750 767 return docinfo
751 # TODO: produce and keep list of image names and numbers 768
769 # save list of image names and numbers
770 imgNames = {}
771 for f in dir:
772 fn = f.findtext('name')
773 pn = f.findtext('index')
774 imgNames[fn] = getInt(pn)
775
776 docinfo['imgFileNames'] = imgNames
752 return docinfo 777 return docinfo
753 778
754 779
755 def getDocinfoFromPresentationInfoXml(self,docinfo): 780 def getDocinfoFromPresentationInfoXml(self,docinfo):
756 """gets DC-like bibliographical information from the presentation entry in texttools""" 781 """gets DC-like bibliographical information from the presentation entry in texttools"""
764 if url.startswith("http://"): 789 if url.startswith("http://"):
765 # real URL 790 # real URL
766 metaUrl = url 791 metaUrl = url
767 else: 792 else:
768 # online path 793 # online path
769
770 server=self.digilibBaseUrl+"/servlet/Texter?fn=" 794 server=self.digilibBaseUrl+"/servlet/Texter?fn="
771 metaUrl=server+url 795 metaUrl=server+url
772 796
773 txt=getHttpData(metaUrl) 797 txt=getHttpData(metaUrl)
774 if txt is None: 798 if txt is None:
780 docinfo['title']=getText(dom.find(".//title")) 804 docinfo['title']=getText(dom.find(".//title"))
781 docinfo['date']=getText(dom.find(".//date")) 805 docinfo['date']=getText(dom.find(".//date"))
782 return docinfo 806 return docinfo
783 807
784 808
785 def getPageinfo(self, current=None, start=None, rows=None, cols=None, docinfo=None, userinfo=None, viewMode=None, viewLayer=None, tocMode=None): 809 def getPageinfo(self, pn=None, pf=None, start=None, rows=None, cols=None, docinfo=None, userinfo=None, viewMode=None, viewLayer=None, tocMode=None):
786 """returns pageinfo with the given parameters""" 810 """returns pageinfo with the given parameters"""
787 logging.debug("getPageInfo(current=%s, start=%s, rows=%s, cols=%s, viewMode=%s, viewLayer=%s, tocMode=%s)"%(current,start,rows,cols,viewMode,viewLayer,tocMode)) 811 logging.debug("getPageInfo(pn=%s, pf=%s, start=%s, rows=%s, cols=%s, viewMode=%s, viewLayer=%s, tocMode=%s)"%(pn,pf,start,rows,cols,viewMode,viewLayer,tocMode))
788 pageinfo = {} 812 pageinfo = {}
789 pageinfo['viewMode'] = viewMode 813 pageinfo['viewMode'] = viewMode
790 # split viewLayer if necessary 814 # split viewLayer if necessary
791 if isinstance(viewLayer,basestring): 815 if isinstance(viewLayer,basestring):
792 viewLayer = viewLayer.split(',') 816 viewLayer = viewLayer.split(',')
805 829
806 pageinfo['viewLayer'] = viewLayer 830 pageinfo['viewLayer'] = viewLayer
807 pageinfo['tocMode'] = tocMode 831 pageinfo['tocMode'] = tocMode
808 832
809 # TODO: unify current and pn! 833 # TODO: unify current and pn!
810 current = getInt(current) 834 #pageinfo['current'] = current
811 pageinfo['current'] = current 835 # pf takes precedence over pn
812 pageinfo['pn'] = current 836 if pf:
837 pageinfo['pf'] = pf
838 pn = getPnForPf(docinfo, pf)
839 else:
840 pn = getInt(pn, 1)
841
842 pageinfo['pn'] = pn
813 rows = int(rows or self.thumbrows) 843 rows = int(rows or self.thumbrows)
814 pageinfo['rows'] = rows 844 pageinfo['rows'] = rows
815 cols = int(cols or self.thumbcols) 845 cols = int(cols or self.thumbcols)
816 pageinfo['cols'] = cols 846 pageinfo['cols'] = cols
817 grpsize = cols * rows 847 grpsize = cols * rows
818 pageinfo['groupsize'] = grpsize 848 pageinfo['groupsize'] = grpsize
819 # is start is empty use one around current 849 # is start is empty use one around pn
820 start = getInt(start, default=(math.ceil(float(current)/float(grpsize))*grpsize-(grpsize-1))) 850 start = getInt(start, default=(math.ceil(float(pn)/float(grpsize))*grpsize-(grpsize-1)))
821 # int(current / grpsize) * grpsize +1)) 851 # int(current / grpsize) * grpsize +1))
822 pageinfo['start'] = start 852 pageinfo['start'] = start
823 # get number of pages 853 # get number of pages
824 np = int(docinfo.get('numPages', 0)) 854 np = int(docinfo.get('numPages', 0))
825 if np == 0: 855 if np == 0:
842 pageinfo['pageBatch'] = self.getPageBatch(start=start, rows=rows, cols=cols, pageFlowLtr=pageFlowLtr, pageZero=pageZero, minIdx=1, maxIdx=np) 872 pageinfo['pageBatch'] = self.getPageBatch(start=start, rows=rows, cols=cols, pageFlowLtr=pageFlowLtr, pageZero=pageZero, minIdx=1, maxIdx=np)
843 # more page parameters 873 # more page parameters
844 pageinfo['characterNormalization'] = self.REQUEST.get('characterNormalization','reg') 874 pageinfo['characterNormalization'] = self.REQUEST.get('characterNormalization','reg')
845 if docinfo.get('pageNumbers'): 875 if docinfo.get('pageNumbers'):
846 # get original page numbers 876 # get original page numbers
847 pageNumber = docinfo['pageNumbers'].get(current, None) 877 pageNumber = docinfo['pageNumbers'].get(pn, None)
848 if pageNumber is not None: 878 if pageNumber is not None:
849 pageinfo['pageNumberOrig'] = pageNumber['no'] 879 pageinfo['pageNumberOrig'] = pageNumber['no']
850 pageinfo['pageNumberOrigNorm'] = pageNumber['non'] 880 pageinfo['pageNumberOrigNorm'] = pageNumber['non']
851 881
852 # cache search results 882 # cache search results