changeset 478:cd37d6f8d5e8 elementtree

more cleanup
author casties
date Fri, 12 Aug 2011 16:41:39 +0200
parents 17f0290b2327
children fe5b0e4ac5f2
files MpdlXmlTextServer.py documentViewer.py
diffstat 2 files changed, 42 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/MpdlXmlTextServer.py	Wed Aug 10 21:18:03 2011 +0200
+++ b/MpdlXmlTextServer.py	Fri Aug 12 16:41:39 2011 +0200
@@ -89,7 +89,7 @@
         url = docinfo['url']
         pagesize = pageinfo['queryPageSize']
         pn = pageinfo.get('searchPN',1)
-        sn = pageinfo['sn']
+        sn = pageinfo.get('sn',None) #TODO: is this s now?
         highlightQuery = pageinfo['highlightQuery']
         query =pageinfo['query']
         queryType =pageinfo['queryType']
@@ -269,7 +269,7 @@
     def getTextPage(self, mode="text", pn=1, docinfo=None, pageinfo=None):
         """returns single page from fulltext"""
         logging.debug("getTextPage mode=%s, pn=%s"%(mode,pn))
-        # check for cached text -- but this shouldn't be called twice
+        # check for cached text -- but ideally this shouldn't be called twice
         if pageinfo.has_key('textPage'):
             logging.debug("getTextPage: using cached text")
             return pageinfo['textPage']
@@ -287,7 +287,8 @@
         tocMode = pageinfo.get('tocMode', None)
         tocPN = pageinfo.get('tocPN',None)
         characterNormalization = pageinfo.get('characterNormalization', None)
-        selfurl = docinfo['viewerUrl']   
+        
+        selfurl = docinfo['viewerUrl']
         
         if mode == "dict" or mode == "text_dict":
             # dict is called textPollux in the backend
@@ -322,12 +323,14 @@
         
         # plain text mode
         if mode == "text":
+            # get full url assuming documentViewer is parent
+            selfurl = self.getLink()
             if pagediv is not None:
                 links = pagediv.findall(".//a")
                 for l in links:
                     href = l.get('href')
                     if href and href.startswith('#note-'):
-                        href = href.replace('#note-',"?mode=%s&url=%s&viewMode=text&tocMode=%s&tocPN=%s&pn=%s#note-"%(urlmode,url,tocMode,tocPN,pn))
+                        href = href.replace('#note-',"%s#note-"%selfurl)
                         l.set('href', href)
 
                 return serialize(pagediv)
@@ -335,6 +338,8 @@
         # text-with-links mode
         elif mode == "dict":
             if pagediv is not None:
+                viewerurl = docinfo['viewerUrl']
+                selfurl = self.getLink()
                 # check all a-tags
                 links = pagediv.findall(".//a")
                 for l in links:
@@ -343,14 +348,13 @@
                     if href:
                         # is link with href
                         if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql'):
-                            # is pollux link
-                            selfurl = self.absolute_url()
-                            # change href
-                            l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql','%s/head_main_voc'%selfurl))
-                            # add target
+                            # is dictionary link - change href (keeping parameters)
+                            l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql','%s/template/viewer_wordinfo'%viewerurl))
+                            # add target to open new page
                             l.set('target', '_blank')
                                                           
-                        if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql'):    
+                        # TODO: is this needed?
+                        if href.startswith('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql'):
                             selfurl = self.absolute_url()
                             l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/lt/lemma.xql','%s/head_main_lemma'%selfurl))
                             l.set('target', '_blank')
@@ -358,7 +362,8 @@
                             l.set('ondblclick', 'popupWin.focus();')   
                     
                         if href.startswith('#note-'):
-                            l.set('href', href.replace('#note-',"?mode=%s&url=%s&viewMode=text_dict&tocMode=%s&tocPN=%s&pn=%s#note-"%(urlmode,url,tocMode,tocPN,pn)))
+                            # note link
+                            l.set('href', href.replace('#note-',"%s#note-"%selfurl))
                               
                 return serialize(pagediv)
             
@@ -389,28 +394,10 @@
                     
         return "no text here"
     
-    # WTF: is this needed?
-    def getOrigPages(self, docinfo=None, pageinfo=None):
-        logging.debug("CALLED: getOrigPages!")
-        if not pageinfo.has_key('pageNumberOrig'):
-            logging.warning("getOrigPages: not in pageinfo!")
-            return None
-        
-        return pageinfo['pageNumberOrig']
-    
-    # WTF: is this needed?
-    def getOrigPagesNorm(self, docinfo=None, pageinfo=None):
-        logging.debug("CALLED: getOrigPagesNorm!")
-        if not pageinfo.has_key('pageNumberOrigNorm'):
-            logging.warning("getOrigPagesNorm: not in pageinfo!")
-            return None
-        
-        return pageinfo['pageNumberOrigNorm']
-                
     # TODO: should be getWordInfo
-    def getTranslate(self, word=None, language=None):
-        """translate into another languages"""
-        data = self.getServerData("lt/wordInfo.xql","language="+str(language)+"&word="+urllib.quote(word)+"&output=html")
+    def getWordInfo(self, word='', language='', display=''):
+        """show information (like dictionaries) about word"""
+        data = self.getServerData("lt/wordInfo.xql","language=%s&word=%s&display=%s&output=html"%(language,urllib.quote(word),urllib.quote(display)))
         return data
     
     # WTF: what does this do?
--- a/documentViewer.py	Wed Aug 10 21:18:03 2011 +0200
+++ b/documentViewer.py	Fri Aug 12 16:41:39 2011 +0200
@@ -209,9 +209,9 @@
         """get all gis places """
         return self.template.fulltextclient.getAllGisPlaces(**args)
        
-    def getTranslate(self, **args):
+    def getWordInfo(self, **args):
         """get translate"""
-        return self.template.fulltextclient.getTranslate(**args)
+        return self.template.fulltextclient.getWordInfo(**args)
 
     def getLemma(self, **args):
         """get lemma"""
@@ -310,6 +310,7 @@
             
         # stringify viewType
         if isinstance(viewType, list):
+            logging.debug("index_html: viewType is list:%s"%viewType)
             viewType = ','.join([t for t in viewType if t])
                         
         pageinfo = self.getPageinfo(start=start, current=pn, docinfo=docinfo, viewMode=viewMode, viewType=viewType, tocMode=tocMode)
@@ -358,7 +359,7 @@
         else:
             return style
     
-    def getParams(self, param=None, val=None, params=None):
+    def getParams(self, param=None, val=None, params=None, duplicates=None):
         """returns dict with URL parameters.
         
         Takes URL parameters and additionally param=val or dict params.
@@ -375,8 +376,7 @@
                 
         # change more params
         if params is not None:
-            for k in params.keys():
-                v = params[k]
+            for (k, v) in params.items():
                 if v is None:
                     # val=None removes param
                     if newParams.has_key(k):
@@ -384,12 +384,23 @@
                         
                 else:
                     newParams[k] = v
-                    
+
+        if duplicates:
+            # eliminate lists (coming from duplicate keys)
+            for (k,v) in newParams.items():
+                if isinstance(v, list):
+                    if duplicates == 'comma':
+                        # make comma-separated list of non-empty entries
+                        newParams[k] = ','.join([t for t in v if t])
+                    elif duplicates == 'first':
+                        # take first non-empty entry
+                        newParams[k] = [t for t in v if t][0]
+        
         return newParams
     
-    def getLink(self, param=None, val=None, params=None, baseUrl=None, paramSep='&'):
+    def getLink(self, param=None, val=None, params=None, baseUrl=None, paramSep='&', duplicates='comma'):
         """returns URL to documentviewer with parameter param set to val or from dict params"""
-        urlParams = self.getParams(param=param, val=val, params=params)
+        urlParams = self.getParams(param=param, val=val, params=params, duplicates=duplicates)
         # quote values and assemble into query string (not escaping '/')
         ps = paramSep.join(["%s=%s"%(k,urllib.quote_plus(unicode(v),'/')) for (k, v) in urlParams.items()])
         if baseUrl is None:
@@ -398,9 +409,9 @@
         url = "%s?%s"%(baseUrl, ps)
         return url
 
-    def getLinkAmp(self, param=None, val=None, params=None, baseUrl=None):
+    def getLinkAmp(self, param=None, val=None, params=None, baseUrl=None, duplicates='comma'):
         """link to documentviewer with parameter param set to val"""
-        return self.getLink(param, val, params, baseUrl, '&')
+        return self.getLink(param=param, val=val, params=params, baseUrl=baseUrl, paramSep='&', duplicates=duplicates)
     
     
     def getInfo_xml(self,url,mode):
@@ -686,8 +697,9 @@
         return docinfo
     
 
-    def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None, viewMode=None, viewType=None, tocMode=None):
+    def getPageinfo(self, current=None, start=None, rows=None, cols=None, docinfo=None, viewMode=None, viewType=None, tocMode=None):
         """returns pageinfo with the given parameters"""
+        logging.debug("getPageInfo(current=%s, start=%s, rows=%s, cols=%s, viewMode=%s, viewType=%s, tocMode=%s)"%(current,start,rows,cols,viewMode,viewType,tocMode))
         pageinfo = {}
         pageinfo['viewMode'] = viewMode
         pageinfo['viewType'] = viewType