annotate MpiwgXmlTextServer.py @ 609:7962e6891d99

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