annotate MpiwgXmlTextServer.py @ 610:0488cd12355b

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