annotate MpiwgXmlTextServer.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 ca0274423382
children 7962e6891d99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
1 from OFS.SimpleItem import SimpleItem
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
2 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
3
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
4 import xml.etree.ElementTree as ET
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
5
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
6 import re
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
7 import logging
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
8 import urllib
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
9 import urlparse
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
10 import base64
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
11
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
12 from datetime import datetime
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
13
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
14 from SrvTxtUtils import getInt, getText, getHttpData
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
15
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
16 def serialize(node):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
17 """returns a string containing an XML snippet of node"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
18 s = ET.tostring(node, 'UTF-8')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
19 # snip off XML declaration
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
20 if s.startswith('<?xml'):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
21 i = s.find('?>')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
22 return s[i+3:]
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
23
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
24 return s
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
25
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
26
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
27 class MpiwgXmlTextServer(SimpleItem):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
28 """TextServer implementation for MPIWG-XML server"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
29 meta_type="MPIWG-XML TextServer"
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
30
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
31 manage_options=(
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
32 {'label':'Config','action':'manage_changeMpiwgXmlTextServerForm'},
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
33 )+SimpleItem.manage_options
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
34
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
35 manage_changeMpiwgXmlTextServerForm = PageTemplateFile("zpt/manage_changeMpiwgXmlTextServer", globals())
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
36
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
37 def __init__(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpiwg-mpdl-cms-web/", timeout=40, serverName=None, repositoryType='production'):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
38 """constructor"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
39 self.id=id
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
40 self.title=title
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
41 self.timeout = timeout
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
42 self.repositoryType = repositoryType
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
43 if serverName is None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
44 self.serverUrl = serverUrl
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
45 else:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
46 self.serverUrl = "http://%s/mpiwg-mpdl-cms-web/"%serverName
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
47
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
48 def getHttpData(self, url, data=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
49 """returns result from url+data HTTP request"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
50 return getHttpData(url,data,timeout=self.timeout)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
51
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
52 def getServerData(self, method, data=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
53 """returns result from text server for method+data"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
54 url = self.serverUrl+method
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
55 return getHttpData(url,data,timeout=self.timeout)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
56
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
57
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
58 def getRepositoryType(self):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
59 """returns the repository type, e.g. 'production'"""
572
51800c42bcda deal with empty repositoryType
casties
parents: 570
diff changeset
60 return getattr(self, 'repositoryType', None)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
61
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
62 def getTextDownloadUrl(self, type='xml', docinfo=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
63 """returns a URL to download the current text"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
64 docpath = docinfo.get('textURLPath', None)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
65 if not docpath:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
66 return None
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
67
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
68 docpath = docpath.replace('.xml','.'+type)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
69 url = '%sdoc/GetDocument?id=%s'%(self.serverUrl.replace('interface/',''), docpath)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
70 return url
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
71
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
72
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
73 def getPlacesOnPage(self, docinfo=None, pn=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
74 """Returns list of GIS places of page pn"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
75 #FIXME!
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
76 docpath = docinfo.get('textURLPath',None)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
77 if not docpath:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
78 return None
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
79
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
80 places=[]
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
81 text=self.getServerData("xpath.xql", "document=%s&xpath=//place&pn=%s"%(docpath,pn))
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
82 dom = ET.fromstring(text)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
83 result = dom.findall(".//resultPage/place")
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
84 for l in result:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
85 id = l.get("id")
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
86 name = l.text
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
87 place = {'id': id, 'name': name}
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
88 places.append(place)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
89
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
90 return places
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
91
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
92
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
93 def getTextInfo(self, mode=None, docinfo=None):
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
94 """reads document info, including page concordance, from text server"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
95 logging.debug("getTextInfo mode=%s"%mode)
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
96
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
97 field = ''
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
98 if mode in ['pages', 'toc', 'figures', 'handwritten']:
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
99 # translate mode to field param
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
100 field = '&field=%s'%mode
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
101 else:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
102 mode = None
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
103
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
104 # check cached info
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
105 if mode:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
106 # cached toc-request?
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
107 if 'full_%s'%mode in docinfo:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
108 return docinfo
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
109
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
110 else:
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
111 # cached but no toc-request?
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
112 if 'numTextPages' in docinfo:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
113 return docinfo
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
114
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
115 docpath = docinfo.get('textURLPath', None)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
116 if docpath is None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
117 logging.error("getTextInfo: no textURLPath!")
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
118 return docinfo
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
119
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
120 # fetch docinfo
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
121 pagexml = self.getServerData("query/GetDocInfo","docId=%s%s"%(docpath,field))
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
122 dom = ET.fromstring(pagexml)
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
123 # all info in tag <doc>
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
124 doc = dom
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
125 if doc is None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
126 logging.error("getTextInfo: unable to find document-tag!")
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
127 else:
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
128 if mode is None:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
129 # get general info from system-tag
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
130 sys = doc.find('system')
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
131 if sys is not None:
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
132 docinfo['numTextPages'] = getInt(getText(sys.find('countPages')))
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
133 docinfo['numFigureEntries'] = getInt(getText(sys.find('countFigures')))
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
134 docinfo['numHandwritten'] = getInt(getText(sys.find('countHandwritten')))
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
135 docinfo['numTocEntries'] = getInt(getText(sys.find('countTocEntries')))
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
136
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
137 else:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
138 # result is in list-tag
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
139 l = doc.find('list')
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
140 if l is not None:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
141 lt = l.get('type')
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
142 # pageNumbers
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
143 if lt == 'pages':
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
144 # contains tags with page numbers
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
145 # <item n="14" o="2" o-norm="2" file="0014"/>
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
146 # n=scan number, o=original page no, on=normalized original page no
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
147 # pageNumbers is a dict indexed by scan number
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
148 pages = {}
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
149 for i in l:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
150 page = {}
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
151 pn = getInt(i.get('n'))
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
152 page['pn'] = pn
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
153 no = i.get('o')
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
154 page['no'] = no
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
155 non = i.get('o-norm')
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
156 page['non'] = non
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
157
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
158 if pn > 0:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
159 pages[pn] = page
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
160
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
161 docinfo['pageNumbers'] = pages
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
162
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
163 # toc
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
164 elif lt == 'toc' or lt == 'figures' or lt == 'handwritten':
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
165 # contains tags with table of contents/figures
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
166 # <item n="2.1." lv="2">CAP.I. <ref o="119">132</ref></item>
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
167 tocs = []
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
168 for te in l:
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
169 if te.tag == 'item':
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
170 toc = {}
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
171 toc['level-string'] = te.get('n')
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
172 toc['level'] = te.get('lv')
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
173 toc['content'] = te.text.strip()
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
174 ref = te.find('ref')
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
175 toc['pn'] = getInt(ref.text)
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
176 toc['no'] = ref.get('o')
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
177 toc['non'] = ref.get('o-norm')
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
178 tocs.append(toc)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
179
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
180 # save as full_toc/full_figures
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
181 docinfo['full_%s'%mode] = tocs
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
182
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
183 return docinfo
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
184
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
185
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
186 def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
187 """returns single page from fulltext"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
188
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
189 logging.debug("getTextPage mode=%s, pn=%s"%(mode,pn))
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
190 startTime = datetime.now()
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
191 # check for cached text -- but ideally this shouldn't be called twice
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
192 if pageinfo.has_key('textPage'):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
193 logging.debug("getTextPage: using cached text")
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
194 return pageinfo['textPage']
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
195
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
196 docpath = docinfo.get('textURLPath', None)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
197 if not docpath:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
198 return None
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
199
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
200 # stuff for constructing full urls
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
201 selfurl = docinfo['viewerUrl']
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
202 textParams = {'docId': docpath,
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
203 'page': pn}
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
204
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
205 normMode = pageinfo.get('characterNormalization', 'reg')
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
206 # TODO: change values in form
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
207 if normMode == 'regPlusNorm':
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
208 normMode = 'norm'
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
209
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
210 # TODO: this should not be necessary when the backend is fixed
579
fc861a6cef17 update in w-tag format.
casties
parents: 577
diff changeset
211 #textParams['normalization'] = normMode
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
212
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
213 if not mode:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
214 # default is dict
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
215 mode = 'text'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
216
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
217 modes = mode.split(',')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
218 # check for multiple layers
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
219 if len(modes) > 1:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
220 logging.debug("getTextPage: more than one mode=%s"%mode)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
221
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
222 # search mode
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
223 if 'search' in modes:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
224 # add highlighting
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
225 highlightQuery = pageinfo.get('highlightQuery', None)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
226 if highlightQuery:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
227 textParams['highlightQuery'] = highlightQuery
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
228 textParams['highlightElem'] = pageinfo.get('highlightElement', '')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
229 textParams['highlightElemPos'] = pageinfo.get('highlightElementPos', '')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
230
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
231 # ignore mode in the following
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
232 modes.remove('search')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
233
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
234 # pundit mode
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
235 punditMode = False
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
236 if 'pundit' in modes:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
237 punditMode = True
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
238 # ignore mode in the following
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
239 modes.remove('pundit')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
240
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
241 # other modes don't combine
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
242 if 'dict' in modes:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
243 textmode = 'dict'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
244 textParams['outputFormat'] = 'html'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
245 elif 'xml' in modes:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
246 textmode = 'xml'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
247 textParams['outputFormat'] = 'xmlDisplay'
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
248 normMode = 'orig'
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
249 elif 'gis' in modes:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
250 #FIXME!
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
251 textmode = 'gis'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
252 else:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
253 # text is default mode
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
254 textmode = 'plain'
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
255 textParams['outputFormat'] = 'html'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
256
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
257 try:
570
61d53ccbdd70 more resilience to server errors.
casties
parents: 568
diff changeset
258 # fetch the page
61d53ccbdd70 more resilience to server errors.
casties
parents: 568
diff changeset
259 pagexml = self.getServerData("query/GetPage",urllib.urlencode(textParams))
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
260 dom = ET.fromstring(pagexml)
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
261 except Exception, e:
570
61d53ccbdd70 more resilience to server errors.
casties
parents: 568
diff changeset
262 logging.error("Error reading page: %s"%e)
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
263 return None
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
264
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
265 # plain text or text-with-links mode
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
266 if textmode == "plain" or textmode == "dict":
574
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
267 # the text is in div@class=text
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
268 pagediv = dom.find(".//div[@class='text']")
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
269 logging.debug("pagediv: %s"%repr(pagediv))
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
270 if pagediv is not None:
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
271 # add textmode and normMode classes
579
fc861a6cef17 update in w-tag format.
casties
parents: 577
diff changeset
272 #pagediv.set('class', 'text %s %s'%(textmode, normMode))
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
273 self._processWTags(textmode, normMode, pagediv)
567
8b1e20bf300d more new textserver
casties
parents: 566
diff changeset
274 #self._processPbTag(pagediv, pageinfo)
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
275 self._processFigures(pagediv, docinfo)
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
276 #self._fixEmptyDivs(pagediv)
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
277 # get full url assuming documentViewer is parent
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
278 selfurl = self.getLink()
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
279 # check all a-tags
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
280 links = pagediv.findall('.//a')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
281 for l in links:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
282 href = l.get('href')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
283 if href:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
284 # is link with href
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
285 linkurl = urlparse.urlparse(href)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
286 if linkurl.path.endswith('GetDictionaryEntries'):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
287 #TODO: replace wordInfo page
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
288 # add target to open new page
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
289 l.set('target', '_blank')
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
290
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
291 if punditMode:
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
292 self._addPunditAttributes(pagediv, pageinfo, docinfo)
577
9251719154a3 toc with list of handwritten notes.
casties
parents: 576
diff changeset
293
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
294 s = serialize(pagediv)
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
295 logging.debug("getTextPage done in %s"%(datetime.now()-startTime))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
296 return s
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
297
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
298 # xml mode
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
299 elif textmode == "xml":
574
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
300 # the text is in body
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
301 pagediv = dom.find(".//body")
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
302 logging.debug("pagediv: %s"%repr(pagediv))
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
303 if pagediv is not None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
304 return serialize(pagediv)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
305
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
306 # pureXml mode WTF?
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
307 elif textmode == "pureXml":
574
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
308 # the text is in body
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
309 pagediv = dom.find(".//body")
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
310 logging.debug("pagediv: %s"%repr(pagediv))
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
311 if pagediv is not None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
312 return serialize(pagediv)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
313
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
314 # gis mode FIXME!
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
315 elif textmode == "gis":
574
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
316 # the text is in div@class=text
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
317 pagediv = dom.find(".//div[@class='text']")
4778900ae3e2 viewMode=xml works now
casties
parents: 572
diff changeset
318 logging.debug("pagediv: %s"%repr(pagediv))
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
319 if pagediv is not None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
320 # fix empty div tags
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
321 self._fixEmptyDivs(pagediv)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
322 # check all a-tags
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
323 links = pagediv.findall(".//a")
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
324 # add our URL as backlink
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
325 selfurl = self.getLink()
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
326 doc = base64.b64encode(selfurl)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
327 for l in links:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
328 href = l.get('href')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
329 if href:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
330 if href.startswith('http://mappit.mpiwg-berlin.mpg.de'):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
331 l.set('href', re.sub(r'doc=[\w+/=]+', 'doc=%s'%doc, href))
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
332 l.set('target', '_blank')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
333
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
334 return serialize(pagediv)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
335
579
fc861a6cef17 update in w-tag format.
casties
parents: 577
diff changeset
336 logging.error("getTextPage: error in text mode %s or in text!"%(textmode))
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
337 return None
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
338
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
339 def _processWTags(self, textMode, normMode, pagediv):
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
340 """selects the necessary information from w-spans and removes the rest from pagediv"""
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
341 logging.debug("processWTags(textMode=%s,norm=%s,pagediv"%(repr(textMode),repr(normMode)))
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
342 startTime = datetime.now()
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
343 wtags = pagediv.findall(".//span[@class='w']")
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
344 for wtag in wtags:
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
345 if textMode == 'dict':
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
346 # delete non-a-tags
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
347 wtag.remove(wtag.find("span[@class='nodictionary orig']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
348 wtag.remove(wtag.find("span[@class='nodictionary reg']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
349 wtag.remove(wtag.find("span[@class='nodictionary norm']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
350 # delete non-matching children of a-tag and suppress remaining tag name
579
fc861a6cef17 update in w-tag format.
casties
parents: 577
diff changeset
351 atag = wtag.find("*[@class='dictionary']")
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
352 if normMode == 'orig':
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
353 atag.remove(atag.find("span[@class='reg']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
354 atag.remove(atag.find("span[@class='norm']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
355 atag.find("span[@class='orig']").tag = None
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
356 elif normMode == 'reg':
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
357 atag.remove(atag.find("span[@class='orig']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
358 atag.remove(atag.find("span[@class='norm']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
359 atag.find("span[@class='reg']").tag = None
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
360 elif normMode == 'norm':
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
361 atag.remove(atag.find("span[@class='orig']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
362 atag.remove(atag.find("span[@class='reg']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
363 atag.find("span[@class='norm']").tag = None
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
364
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
365 else:
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
366 # delete a-tag
579
fc861a6cef17 update in w-tag format.
casties
parents: 577
diff changeset
367 wtag.remove(wtag.find("*[@class='dictionary']"))
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
368 # delete non-matching children and suppress remaining tag name
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
369 if normMode == 'orig':
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
370 wtag.remove(wtag.find("span[@class='nodictionary reg']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
371 wtag.remove(wtag.find("span[@class='nodictionary norm']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
372 wtag.find("span[@class='nodictionary orig']").tag = None
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
373 elif normMode == 'reg':
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
374 wtag.remove(wtag.find("span[@class='nodictionary orig']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
375 wtag.remove(wtag.find("span[@class='nodictionary norm']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
376 wtag.find("span[@class='nodictionary reg']").tag = None
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
377 elif normMode == 'norm':
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
378 wtag.remove(wtag.find("span[@class='nodictionary orig']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
379 wtag.remove(wtag.find("span[@class='nodictionary reg']"))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
380 wtag.find("span[@class='nodictionary norm']").tag = None
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
381
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
382 # suppress w-tag name
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
383 wtag.tag = None
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
384
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
385 logging.debug("processWTags in %s"%(datetime.now()-startTime))
575
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
386 return pagediv
f0e5e9c6737f new w-tag solution with css.
casties
parents: 574
diff changeset
387
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
388 def _processPbTag(self, pagediv, pageinfo):
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
389 """extracts information from pb-tag and removes it from pagediv"""
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
390 pbdiv = pagediv.find(".//span[@class='pb']")
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
391 if pbdiv is None:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
392 logging.warning("getTextPage: no pb-span!")
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
393 return pagediv
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
394
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
395 # extract running head
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
396 rh = pbdiv.find(".//span[@class='rhead']")
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
397 if rh is not None:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
398 pageinfo['pageHeaderTitle'] = getText(rh)
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
399
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
400 # remove pb-div from parent
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
401 ppdiv = pagediv.find(".//span[@class='pb']/..")
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
402 ppdiv.remove(pbdiv)
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
403 return pagediv
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
404
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
405 def _addPunditAttributes(self, pagediv, pageinfo, docinfo):
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
406 """add about attributes for pundit annotation tool"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
407 textid = docinfo.get('DRI', "fn=%s"%docinfo.get('documentPath', '???'))
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
408 pn = pageinfo.get('pn', '1')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
409 # TODO: use pn as well?
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
410 # check all div-tags
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
411 divs = pagediv.findall(".//div")
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
412 for d in divs:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
413 id = d.get('id')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
414 if id:
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
415 # TODO: check path (cf RFC2396)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
416 d.set('about', "http://echo.mpiwg-berlin.mpg.de/%s/pn=%s/#%s"%(textid,pn,id))
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
417 cls = d.get('class','')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
418 cls += ' pundit-content'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
419 d.set('class', cls.strip())
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
420
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
421 return pagediv
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
422
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
423 def _processFigures(self, pagediv, docinfo):
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
424 """processes figure-tags"""
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
425 # unfortunately etree can not select class.startswith('figure')
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
426 divs = pagediv.findall(".//span[@class]")
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
427 scalerUrl = docinfo['digilibScalerUrl']
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
428 viewerUrl = docinfo['digilibViewerUrl']
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
429 for d in divs:
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
430 if not d.get('class').startswith('figure'):
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
431 continue
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
432
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
433 try:
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
434 a = d.find('a')
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
435 img = a.find('img')
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
436 imgsrc = img.get('src')
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
437 imgurl = urlparse.urlparse(imgsrc)
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
438 imgq = imgurl.query
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
439 imgparams = urlparse.parse_qs(imgq)
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
440 fn = imgparams.get('fn', None)
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
441 if fn is not None:
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
442 # parse_qs puts parameters in lists
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
443 fn = fn[0]
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
444 # TODO: check valid path
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
445 # fix img@src
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
446 newsrc = '%s?fn=%s&dw=200&dh=200'%(scalerUrl,fn)
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
447 img.set('src', newsrc)
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
448 # fix a@href
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
449 newlink = '%s?fn=%s'%(viewerUrl,fn)
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
450 a.set('href', newlink)
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
451 a.set('target', '_blank')
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
452
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
453 except:
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
454 logging.warn("processFigures: strange figure!")
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
455
583
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
456
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
457 def _cleanSearchResult(self, pagediv):
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
458 """fixes search result html (change pbs and figures)"""
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
459 # replace figure-tag with figureNumText
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
460 for fig in pagediv.findall(".//span[@class='figure']"):
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
461 txt = fig.findtext(".//span[@class='figureNumText']")
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
462 tail = fig.tail
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
463 fig.clear()
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
464 fig.set('class', 'figure')
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
465 fig.text = txt
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
466 fig.tail = tail
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
467
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
468 # replace lb-tag with "//"
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
469 for lb in pagediv.findall(".//br[@class='lb']"):
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
470 lb.tag = 'span'
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
471 lb.text = '//'
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
472
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
473 # replace pb-tag with "///"
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
474 for pb in pagediv.findall(".//span[@class='pb']"):
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
475 tail = pb.tail
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
476 pb.clear()
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
477 pb.set('class', 'pb')
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
478 pb.text = '///'
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
479 pb.tail = tail
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
480
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
481 return pagediv
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
482
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
483 def _cleanSearchResult2(self, pagediv):
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
484 """fixes search result html (change pbs and figures)"""
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
485 # unfortunately etree can not select class.startswith('figure')
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
486 divs = pagediv.findall(".//span[@class]")
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
487 for d in divs:
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
488 cls = d.get('class')
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
489 if cls.startswith('figure'):
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
490 # replace figure-tag with figureNumText
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
491 txt = d.findtext(".//span[@class='figureNumText']")
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
492 d.clear()
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
493 d.set('class', 'figure')
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
494 d.text = txt
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
495
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
496 elif cls.startswith('pb'):
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
497 # replace pb-tag with "//"
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
498 d.clear()
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
499 d.set('class', 'pb')
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
500 d.text = '//'
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
501
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
502 return pagediv
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
503
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
504
566
4a31608f8b0e more new MpiwgXmlTextServer.
casties
parents: 565
diff changeset
505
565
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
506 def _fixEmptyDivs(self, pagediv):
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
507 """fixes empty div-tags by inserting a space"""
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
508 divs = pagediv.findall('.//div')
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
509 for d in divs:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
510 if len(d) == 0 and not d.text:
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
511 # make empty divs non-empty
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
512 d.text = ' '
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
513
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
514 return pagediv
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
515
1b483194901c more new MpiwgXmlTextServer.
casties
parents: 564
diff changeset
516
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
517 def getSearchResults(self, mode, query=None, pageinfo=None, docinfo=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
518 """loads list of search results and stores XML in docinfo"""
583
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
519 normMode = pageinfo.get('characterNormalization', 'reg')
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
520 logging.debug("getSearchResults mode=%s query=%s norm=%s"%(mode, query, normMode))
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
521 if mode == "none":
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
522 return docinfo
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
523
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
524 #TODO: put mode into query
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
525
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
526 cachedQuery = docinfo.get('cachedQuery', None)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
527 if cachedQuery is not None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
528 # cached search result
583
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
529 if cachedQuery == '%s_%s_%s'%(mode,query,normMode):
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
530 # same query
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
531 return docinfo
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
532
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
533 else:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
534 # different query
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
535 del docinfo['resultSize']
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
536 del docinfo['results']
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
537
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
538 # cache query
583
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
539 docinfo['cachedQuery'] = '%s_%s_%s'%(mode,query,normMode)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
540
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
541 # fetch full results
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
542 docpath = docinfo['textURLPath']
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
543 params = {'docId': docpath,
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
544 'query': query,
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
545 'pageSize': 1000,
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
546 'page': 1,
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
547 'outputFormat': 'html'}
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
548 pagexml = self.getServerData("query/QueryDocument",urllib.urlencode(params))
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
549 results = []
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
550 try:
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
551 dom = ET.fromstring(pagexml)
583
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
552 # clean html output
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
553 self._processWTags('plain', normMode, dom)
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
554 self._cleanSearchResult(dom)
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
555 # page content is currently in multiple <td align=left>
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
556 alldivs = dom.findall(".//tr[@class='hit']")
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
557 for div in alldivs:
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
558 # change tr to div
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
559 div.tag = 'div'
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
560 # change td to span
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
561 for d in div.findall('td'):
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
562 d.tag = 'span'
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
563
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
564 # TODO: can we put etree in the session?
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
565 results.append(div)
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
566
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
567 except Exception, e:
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
568 logging.error("GetSearchResults: Error parsing search result: %s"%e)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
569
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
570 # store results in docinfo
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
571 docinfo['resultSize'] = len(results)
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
572 docinfo['results'] = results
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
573
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
574 return docinfo
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
575
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
576
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
577 def getResultsPage(self, mode="text", query=None, pn=None, start=None, size=None, pageinfo=None, docinfo=None):
583
ca0274423382 follow changes in html format of new text-backend.
casties
parents: 579
diff changeset
578 """returns single page from the list of search results"""
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
579 logging.debug("getResultsPage mode=%s, pn=%s"%(mode,pn))
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
580 # get (cached) result
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
581 self.getSearchResults(mode=mode, query=query, pageinfo=pageinfo, docinfo=docinfo)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
582
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
583 resultxml = docinfo.get('results', None)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
584 if not resultxml:
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
585 logging.error("getResultPage: unable to find results")
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
586 return "Error: no result!"
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
587
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
588 if size is None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
589 size = pageinfo.get('resultPageSize', 10)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
590
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
591 if start is None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
592 start = (pn - 1) * size
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
593
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
594 if resultxml is not None:
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
595 # paginate
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
596 first = start-1
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
597 last = first+size
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
598 tocdivs = resultxml[first:last]
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
599
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
600 toc = ET.Element('div', attrib={'class':'queryResultPage'})
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
601 for div in tocdivs:
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
602 # check all a-tags
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
603 links = div.findall(".//a")
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
604 for l in links:
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
605 href = l.get('href')
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
606 if href:
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
607 # assume all links go to pages
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
608 linkUrl = urlparse.urlparse(href)
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
609 linkParams = urlparse.parse_qs(linkUrl.query)
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
610 # take some parameters (make sure it works even if the link was already parsed)
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
611 params = {'pn': linkParams.get('page',linkParams.get('pn', None)),
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
612 'highlightQuery': linkParams.get('highlightQuery',None),
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
613 'highlightElement': linkParams.get('highlightElem',linkParams.get('highlightElement',None)),
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
614 'highlightElementPos': linkParams.get('highlightElemPos',linkParams.get('highlightElementPos',None))
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
615 }
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
616 if not params['pn']:
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
617 logging.warn("getResultsPage: link has no page: %s"%href)
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
618
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
619 url = self.getLink(params=params)
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
620 l.set('href', url)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
621
576
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
622 toc.append(div)
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
623
b2c7e272e075 new w-tag solution with etree. search works now.
casties
parents: 575
diff changeset
624 return serialize(toc)
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
625
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
626 return "ERROR: no results!"
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
627
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
628
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
629 def getToc(self, mode='text', docinfo=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
630 """returns list of table of contents from docinfo"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
631 logging.debug("getToc mode=%s"%mode)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
632 if mode == 'text':
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
633 queryType = 'toc'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
634 else:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
635 queryType = mode
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
636
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
637 if not 'full_%s'%queryType in docinfo:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
638 # get new toc
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
639 docinfo = self.getTextInfo(queryType, docinfo)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
640
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
641 return docinfo.get('full_%s'%queryType, [])
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
642
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
643
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
644 def getTocPage(self, mode='text', pn=None, start=None, size=None, pageinfo=None, docinfo=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
645 """returns single page from the table of contents"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
646 logging.debug("getTocPage mode=%s, pn=%s start=%s size=%s"%(mode,repr(pn),repr(start),repr(size)))
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
647 fulltoc = self.getToc(mode=mode, docinfo=docinfo)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
648 if len(fulltoc) < 1:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
649 logging.error("getTocPage: unable to find toc!")
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
650 return "Error: no table of contents!"
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
651
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
652 if size is None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
653 size = pageinfo.get('tocPageSize', 30)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
654
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
655 if start is None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
656 start = (pn - 1) * size
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
657
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
658 # paginate
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
659 first = (start - 1)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
660 last = first + size
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
661 tocs = fulltoc[first:last]
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
662 tp = '<div>'
577
9251719154a3 toc with list of handwritten notes.
casties
parents: 576
diff changeset
663 label = {'figures': 'Figure', 'handwritten': 'Handwritten note'}.get(mode, 'Item')
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
664 for toc in tocs:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
665 pageurl = self.getLink('pn', toc['pn'])
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
666 tp += '<div class="tocline">'
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
667 content = toc['content']
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
668 if content:
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
669 tp += '<div class="toc name">[%s] %s</div>'%(toc['level-string'], toc['content'])
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
670 else:
577
9251719154a3 toc with list of handwritten notes.
casties
parents: 576
diff changeset
671 tp += '<div class="toc name">[%s %s]</div>'%(label, toc['level-string'])
568
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
672
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
673 if toc.get('no', None):
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
674 tp += '<div class="toc page"><a href="%s">Page: %s (%s)</a></div>'%(pageurl, toc['pn'], toc['no'])
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
675 else:
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
676 tp += '<div class="toc page"><a href="%s">Page: %s</a></div>'%(pageurl, toc['pn'])
694935574177 more new MpiwgXmlTextServer.
casties
parents: 567
diff changeset
677
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
678 tp += '</div>\n'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
679
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
680 tp += '</div>\n'
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
681
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
682 return tp
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
683
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
684
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
685 def manage_changeMpiwgXmlTextServer(self,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,repositoryType=None,RESPONSE=None):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
686 """change settings"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
687 self.title=title
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
688 self.timeout = timeout
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
689 self.serverUrl = serverUrl
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
690 if repositoryType:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
691 self.repositoryType = repositoryType
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
692 if RESPONSE is not None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
693 RESPONSE.redirect('manage_main')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
694
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
695 # management methods
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
696 def manage_addMpiwgXmlTextServerForm(self):
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
697 """Form for adding"""
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
698 pt = PageTemplateFile("zpt/manage_addMpiwgXmlTextServer", globals()).__of__(self)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
699 return pt()
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
700
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
701 def manage_addMpiwgXmlTextServer(self,id,title="",serverUrl="http://mpdl-text.mpiwg-berlin.mpg.de/mpdl/interface/",timeout=40,RESPONSE=None):
577
9251719154a3 toc with list of handwritten notes.
casties
parents: 576
diff changeset
702 """add MpiwgXmlTextServer"""
564
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
703 newObj = MpiwgXmlTextServer(id=id,title=title,serverUrl=serverUrl,timeout=timeout)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
704 self.Destination()._setObject(id, newObj)
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
705 if RESPONSE is not None:
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
706 RESPONSE.redirect('manage_main')
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
707
31f562fa7214 first version of MpiwgXmlTextServer.
casties
parents:
diff changeset
708