[library-web] [web] refactor symbol search/lookup code to be more extensible



commit 5641f24f742749d0e556fd3f50fed1b935d1fb51
Author: Frédéric Péters <fpeters 0d be>
Date:   Thu Aug 13 14:57:25 2009 +0200

    [web] refactor symbol search/lookup code to be more extensible

 web/symbols_server.py |   87 +++++++++++++++++++++++++++++-------------------
 1 files changed, 52 insertions(+), 35 deletions(-)
---
diff --git a/web/symbols_server.py b/web/symbols_server.py
index d24d89d..fccc6a3 100644
--- a/web/symbols_server.py
+++ b/web/symbols_server.py
@@ -33,6 +33,9 @@ sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '
 
 from config import Config
 
+class NoMatch(Exception):
+    pass
+
 class SymbolsHandler(SCGIHandler):
     def __init__(self, parent_fd):
         SCGIHandler.__init__(self, parent_fd)
@@ -62,47 +65,61 @@ class SymbolsHandler(SCGIHandler):
         uri = uri.rstrip('?') # remove trailing ?
         if uri.split('/')[0] == 'lookup':
             # url is /symbols/lookup/prefix_of_symbol
-            if self.config.debug:
-                print ' looking up...'
-            symbol = uri.split('/')[1]
-            cur = self.sqlcon.cursor()
-            # limit to 100 rows; so the autocomplete javascript doesn't fall
-            # down trying to parse and cache thousands of gtk_ matches.
-            cur.execute('select symbol from symbols where symbol like ? escape "\\" '
-                        'order by symbol limit 50',
-                        (symbol.replace('\\', '\\\\').replace('_', '\\_') + '%',))
-            i = 0
-            print >> output, 'Content-type: text/plain'
-            print >> output, ''
-            for line in cur.fetchall():
-                print >> output, line[0]
-                i += 1
-            if self.config.debug:
-                print '   found', i, 'symbols'
-            return
+            return self.lookup(uri, env, output)
         else:
-            # url is /symbols/?q=name_of_symbol
-            params = cgi.parse_qs(env.get('QUERY_STRING', ''))
-            if params.has_key('q'):
-                symbol = params.get('q')[0]
-                cur = self.sqlcon.cursor()
-                cur.execute('select path from symbols where symbol = ? limit 1', (symbol,))
-                path = cur.fetchone()
-                if path:
-                    path = path[0]
-                    # absolute paths will be handled as internal redirects by
-                    # the SCGI module, hack around this, changing the path to
-                    # be relative
-                    print >> output, 'Status: 302'
-                    print >> output, 'Location:', '..' + path
-                    print >> output, ''
-                    print >> output, 'redirecting to', path
-                    return
+            try:
+                return self.redirect(uri, env, output)
+            except NoMatch:
+                pass
+
+        return self.fallback(uri, env, output)
 
+    def fallback(self, uri, env, output):
         print >> output, 'Content-type: text/plain'
         print >> output, ''
         print >> output, 'unhandled uri'
 
+
+    def lookup(self, uri, env, output):
+        if self.config.debug:
+            print ' looking up...'
+        symbol = uri.split('/')[1]
+        cur = self.sqlcon.cursor()
+        # limit to 100 rows; so the autocomplete javascript doesn't fall
+        # down trying to parse and cache thousands of gtk_ matches.
+        cur.execute('select symbol from symbols where symbol like ? escape "\\" '
+                    'order by symbol limit 50',
+                    (symbol.replace('\\', '\\\\').replace('_', '\\_') + '%',))
+        i = 0
+        print >> output, 'Content-type: text/plain'
+        print >> output, ''
+        for line in cur.fetchall():
+            print >> output, line[0]
+            i += 1
+        if self.config.debug:
+            print '   found', i, 'symbols'
+
+    def redirect(self, uri, env, output):
+        # url is /symbols/?q=name_of_symbol
+        params = cgi.parse_qs(env.get('QUERY_STRING', ''))
+        if params.has_key('q'):
+            symbol = params.get('q')[0]
+            cur = self.sqlcon.cursor()
+            cur.execute('select path from symbols where symbol = ? limit 1', (symbol,))
+            path = cur.fetchone()
+            if path:
+                path = path[0]
+                # absolute paths will be handled as internal redirects by
+                # the SCGI module, hack around this, changing the path to
+                # be relative
+                print >> output, 'Status: 302'
+                print >> output, 'Location:', '..' + path
+                print >> output, ''
+                print >> output, 'redirecting to', path
+                return
+        raise NoMatch()
+
+
 def main():
     parser = OptionParser()
     parser.add_option('-c', '--config', dest = 'config')



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]