Mercurial > hg > documentViewer
comparison documentViewer.py @ 0:96f74b2bab24
fist
author | dwinter |
---|---|
date | Sun, 18 Dec 2005 13:35:02 +0100 |
parents | |
children | 8cc283757c39 9884703dae70 ecdb238aa595 ede0c93de798 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:96f74b2bab24 |
---|---|
1 | |
2 genericDigilib="http://nausikaa2.rz-berlin.mpg.de/digitallibrary/" | |
3 | |
4 from OFS.Folder import Folder | |
5 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate | |
6 from Products.PageTemplates.PageTemplateFile import PageTemplateFile | |
7 from AccessControl import ClassSecurityInfo | |
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 | |
13 | |
14 import Ft.Xml.XPath | |
15 | |
16 import os.path | |
17 import cgi | |
18 import urllib | |
19 | |
20 def getTextFromNode(nodename): | |
21 nodelist=nodename.childNodes | |
22 rc = "" | |
23 for node in nodelist: | |
24 if node.nodeType == node.TEXT_NODE: | |
25 rc = rc + node.data | |
26 return rc | |
27 | |
28 import socket | |
29 | |
30 def urlopen(url): | |
31 """urlopen mit timeout""" | |
32 socket.setdefaulttimeout(2) | |
33 ret=urllib.urlopen(url) | |
34 socket.setdefaulttimeout(5) | |
35 return ret | |
36 | |
37 def getParamFromDigilib(path,param): | |
38 """gibt param von dlInfo aus""" | |
39 imageUrl=genericDigilib+"/dlInfo-xml.jsp?fn="+path | |
40 | |
41 try: | |
42 dom = NonvalidatingReader.parseUri(imageUrl) | |
43 except: | |
44 return None | |
45 | |
46 | |
47 params=dom.xpath("//document-parameters/parameter[@name='%s']/@value"%param) | |
48 | |
49 if params: | |
50 return params[0].value | |
51 | |
52 def parseUrlTextTool(url): | |
53 """parse index meta""" | |
54 | |
55 try: | |
56 dom = NonvalidatingReader.parseUri(url) | |
57 except: | |
58 zLOG.LOG("documentViewer (parseUrlTexttool)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2]) | |
59 return (None,None,None) | |
60 | |
61 archivePaths=dom.xpath("//resource/archive-path") | |
62 | |
63 if archivePaths and (len(archivePaths)>0): | |
64 archivePath=getTextFromNode(archivePaths[0]) | |
65 else: | |
66 archivePath=None | |
67 | |
68 | |
69 images=dom.xpath("//texttool/image") | |
70 | |
71 if images and (len(images)>0): | |
72 image=getTextFromNode(images[0]) | |
73 else: | |
74 image=None | |
75 | |
76 if image and archivePath: | |
77 image=os.path.join(archivePath,image) | |
78 image=image.replace("/mpiwg/online",'') | |
79 pt=getParamFromDigilib(image,'pt') | |
80 | |
81 else: | |
82 image=None | |
83 | |
84 viewerUrls=dom.xpath("//texttool/digiliburlprefix") | |
85 | |
86 if viewerUrls and (len(viewerUrls)>0): | |
87 viewerUrl=getTextFromNode(viewerUrls[0]) | |
88 else: | |
89 viewerUrl=None | |
90 | |
91 | |
92 textUrls=dom.xpath("//texttool/text") | |
93 | |
94 if textUrls and (len(textUrls)>0): | |
95 textUrl=getTextFromNode(textUrls[0]) | |
96 else: | |
97 textUrl=None | |
98 return viewerUrl,(image,pt),textUrl | |
99 | |
100 | |
101 class documentViewer(ZopePageTemplate): | |
102 """document viewer""" | |
103 | |
104 meta_type="Document viewer" | |
105 | |
106 security=ClassSecurityInfo() | |
107 manage_options=ZopePageTemplate.manage_options+( | |
108 {'label':'main config','action':'changeDocumentViewerForm'}, | |
109 ) | |
110 | |
111 _default_content_fn = os.path.join(package_home(globals()),'zpt','documentViewer_template.zpt') | |
112 | |
113 def __init__(self,id,imageViewerUrl,title=""): | |
114 """init document viewer""" | |
115 self.id=id | |
116 self.title=title | |
117 self.imageViewerUrl=imageViewerUrl | |
118 | |
119 security.declareProtected('View management screens','changeDocumentViewerForm') | |
120 def changeDocumentViewerForm(self): | |
121 """change it""" | |
122 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeDocumentViewer.zpt')).__of__(self) | |
123 return pt() | |
124 | |
125 | |
126 def changeDocumentViewer(self,imageViewerUrl,title="",RESPONSE=None): | |
127 """init document viewer""" | |
128 self.title=title | |
129 self.imageViewerUrl=imageViewerUrl | |
130 | |
131 if RESPONSE is not None: | |
132 RESPONSE.redirect('manage_main') | |
133 | |
134 | |
135 def imageLink(self,nr): | |
136 """link hinter den images""" | |
137 paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING']) | |
138 params={} | |
139 for x in paramsTmp.iteritems(): | |
140 params[x[0]]=x[1][0] | |
141 | |
142 params['pn']=nr | |
143 newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) | |
144 return newUrl | |
145 | |
146 | |
147 def thumbruler(self,cols,rows,start,maximum): | |
148 """ruler for thumbs""" | |
149 ret="" | |
150 paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING']) | |
151 params={} | |
152 for x in paramsTmp.iteritems(): | |
153 | |
154 if not x[0]=="start": | |
155 params[x[0]]=x[1][0] | |
156 | |
157 newUrlSelect=self.REQUEST['URL']+"?"+urllib.urlencode(params) | |
158 if start>0: | |
159 newStart=max(start-cols*rows,0) | |
160 params['start']=newStart | |
161 newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) | |
162 ret+="""<a href="%s">prev</a>"""%newUrl | |
163 | |
164 | |
165 ret+="""<select onChange="location.href='%s&start='+this.options[this.selectedIndex].value" """%newUrlSelect | |
166 nr,rest=divmod(maximum,cols*rows) | |
167 if rest > 0: | |
168 nr+=1 | |
169 for i in range(nr): | |
170 nr=i*cols*rows | |
171 | |
172 if (start >= nr) and (start < nr+cols*rows): | |
173 ret+="""<option value="%s" selected>%s</option>"""%(nr,nr) | |
174 else: | |
175 ret+="""<option value="%s">%s</option>"""%(nr,nr) | |
176 ret+="</select>" | |
177 | |
178 if start<maximum: | |
179 newStart=min(start+cols*rows,maximum) | |
180 params['start']=newStart | |
181 newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params) | |
182 ret+="""<a href="%s">next</a>"""%newUrl | |
183 | |
184 return ret | |
185 | |
186 def textToolThumb(self,url,start=0): | |
187 """understands the texttool format | |
188 @param url: url to index.meta with texttool tag | |
189 """ | |
190 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | |
191 | |
192 imageUrl=genericDigilib+"/servlet/Scaler?fn=%s"%imagepath[0] | |
193 | |
194 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','thumbs.zpt')).__of__(self) | |
195 return pt(imageUrl=imageUrl,pt=imagepath[1],start=start) | |
196 | |
197 def text(self,mode,url,pn): | |
198 """give text""" | |
199 if mode=="texttool": #index.meta with texttool information | |
200 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | |
201 | |
202 print textpath | |
203 try: | |
204 dom = NonvalidatingReader.parseUri(textpath) | |
205 except: | |
206 return None | |
207 | |
208 list=[] | |
209 nodes=dom.xpath("//pb") | |
210 | |
211 node=nodes[int(pn)-1] | |
212 | |
213 p=node | |
214 | |
215 while p.tagName!="p": | |
216 p=p.parentNode | |
217 | |
218 | |
219 endNode=nodes[int(pn)] | |
220 | |
221 | |
222 e=endNode | |
223 | |
224 while e.tagName!="p": | |
225 e=e.parentNode | |
226 | |
227 | |
228 next=node.parentNode | |
229 | |
230 #sammle s | |
231 while next and (next!=endNode.parentNode): | |
232 list.append(next) | |
233 next=next.nextSibling | |
234 list.append(endNode.parentNode) | |
235 | |
236 if p==e:# beide im selben paragraphen | |
237 | |
238 else: | |
239 next=p | |
240 while next!=e: | |
241 print next,e | |
242 list.append(next) | |
243 next=next.nextSibling | |
244 | |
245 for x in list: | |
246 PrettyPrint(x) | |
247 | |
248 return list | |
249 | |
250 def image(self,mode,url,pn): | |
251 """give image out""" | |
252 if mode=="texttool": #index.meta with texttool information | |
253 (viewerUrl,imagepath,textpath)=parseUrlTextTool(url) | |
254 url=viewerUrl+"pn=%s&fn=%s"%(pn,imagepath[0]) | |
255 ret="""<iframe height="100%%" width="100%%" src="%s"/>"""%url | |
256 return url | |
257 | |
258 def thumbs(self,mode,url,start): | |
259 """give thumbs out""" | |
260 if mode=="texttool": #index.meta with texttool information | |
261 return self.textToolThumb(url,int(start)) | |
262 | |
263 security.declareProtected('View','index_html') | |
264 | |
265 | |
266 def index_html(self,mode,url,start=0,pn=0): | |
267 ''' | |
268 view it | |
269 @param mode: defines which type of document is behind url | |
270 @param url: url which contains display information | |
271 ''' | |
272 | |
273 | |
274 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','documentViewer_template.zpt')).__of__(self) | |
275 return pt(mode=mode,url=url,start=start,pn=pn) | |
276 | |
277 | |
278 | |
279 # security.declareProtected('View management screens','renameImageForm') | |
280 | |
281 def manage_AddDocumentViewerForm(self): | |
282 """add the viewer form""" | |
283 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addDocumentViewer.zpt')).__of__(self) | |
284 return pt() | |
285 | |
286 def manage_AddDocumentViewer(self,id,imageViewerUrl="",title="",RESPONSE=None): | |
287 """add the viewer""" | |
288 newObj=documentViewer(id,imageViewerUrl,title) | |
289 self._setObject(id,newObj) | |
290 | |
291 if RESPONSE is not None: | |
292 RESPONSE.redirect('manage_main') | |
293 |