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