move the LogSearchIndex open from handlers to log_search, and make it lazy
authorTero Marttila <terom@fixme.fi>
Wed, 11 Feb 2009 03:04:35 +0200
changeset 96 d30c88e89a7e
parent 95 ebdbda3dd5d0
child 97 6165f1ba458d
move the LogSearchIndex open from handlers to log_search, and make it lazy
handlers.py
log_search.py
--- a/handlers.py	Wed Feb 11 02:21:43 2009 +0200
+++ b/handlers.py	Wed Feb 11 03:04:35 2009 +0200
@@ -9,7 +9,7 @@
 import urls, channels, helpers
 import preferences as prefs
 from preferences import preferences
-import config
+import config, log_search
 
 # load templates from here
 templates = template.TemplateLoader("templates",
@@ -19,11 +19,6 @@
     config          = config,
 )
 
-# our LogSearch thing
-# XXX: move elsewhere
-import log_search
-search_index = log_search.LogSearchIndex(config.LOG_CHANNELS, config.SEARCH_INDEX_PATH, 'r')
-
 def index (request) :
     """
         The topmost index page, display a list of available channels, perhaps some general stats
@@ -214,7 +209,7 @@
     if q :
         try :
             # do search
-            lines = search_index.search_simple(channel, q, count, skip)
+            lines = log_search.get_index().search_simple(channel, q, count, skip)
 
             # update max?
             if max and page > max :
--- a/log_search.py	Wed Feb 11 02:21:43 2009 +0200
+++ b/log_search.py	Wed Feb 11 03:04:35 2009 +0200
@@ -7,7 +7,7 @@
 
 import HyperEstraier as hype
 
-import log_line, utils
+import log_line, utils, config
 
 class LogSearchError (Exception) :
     """
@@ -304,3 +304,21 @@
             max         = count,
             skip        = skip
         )
+
+# global read-only index
+_index = None
+
+def get_index () :
+    """
+        Returns the default read-only index, suitable for searching
+    """
+    
+    global _index
+    
+    # open?
+    if not _index :
+        _index = LogSearchIndex(config.LOG_CHANNELS, config.SEARCH_INDEX_PATH, 'r')
+
+    # return
+    return _index
+