mediagoblin-devel
[Top][All Lists]
Advanced

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

[GMG-Devel] [PATCH 14/83] Rename base.py to registry.py and indices.py t


From: Alon Levy
Subject: [GMG-Devel] [PATCH 14/83] Rename base.py to registry.py and indices.py to base.py.
Date: Tue, 25 Feb 2014 21:57:57 +0200

From: Praveen Kumar <address@hidden>

---
 mediagoblin/plugins/search/base.py     | 133 +++++++++++++++++++++++++++------
 mediagoblin/plugins/search/indices.py  | 124 ------------------------------
 mediagoblin/plugins/search/registry.py |  37 +++++++++
 3 files changed, 147 insertions(+), 147 deletions(-)
 delete mode 100644 mediagoblin/plugins/search/indices.py
 create mode 100644 mediagoblin/plugins/search/registry.py

diff --git a/mediagoblin/plugins/search/base.py 
b/mediagoblin/plugins/search/base.py
index 2636fef..648cebf 100644
--- a/mediagoblin/plugins/search/base.py
+++ b/mediagoblin/plugins/search/base.py
@@ -1,37 +1,124 @@
+import logging
+import os
 
-class IndexRegistry(object):
-    _registry = {}
+from mediagoblin.tools import pluginapi
+from mediagoblin.plugins.search.base import IndexRegistry
+from mediagoblin.plugins.search.exceptions import IndexDoesNotExistsError
+from mediagoblin.plugins.search.schemas import MediaEntryIndexSchema
 
-    @staticmethod
-    def register(search_index_obj):
+import whoosh
+
+from whoosh.filedb.multiproc import MultiSegmentWriter
+
+config = pluginapi.get_config('mediagoblin.plugins.search')
+_log = logging.getLogger(__name__)
+
+
+class SearchIndex(object):
+    """
+    Represents a search index. 
+
+    This class encapsulates various methods of Whoosh API
+    for creating, modifying, updating and searching in a search
+    index.
+    """
+    
+    def __init__(self, model, schema, search_index_dir=None, 
use_multiprocessing=None):
+        self.schema = schema
+        self.field_names = self.schema.field_names
+
+        self.search_index = None
+        self.search_index_name = self.__class__.__name__.lower()
+        
+        self.search_index_dir = search_index_dir
+        if not self.search_index_dir:
+            self.search_index_dir = config['search_index_dir']
+        
+        self.use_multiprocessing = use_multiprocessing 
+        if not self.use_multiprocessing:
+            self.use_multiprocessing = config['use_multiprocessing']
+    
+        self.create_index()
+
+    def _index_exists(self):
         """
-        Registers an index object.
+        Returns whether a valid index exists in self.search_index_dir.
+        
+        If self.search_index is None, it implies that no index has been
+        created yet. In this case, and IndexDoesNotExistsError exception
+        is raised.
         """
-        identifier = search_index_obj.__tablename__
-        IndexRegistry._registry[identifier] = search_index_obj
+        if not self.search_index:
+            raise IndexDoesNotExistsError(
+                self.search_index_dir, self.search_index_name)
+        
+        if self.search_index.exists_in(
+            self.search_index_dir, indexname=self.search_index_name):
+            return True
+
+        return False
     
-    @staticmethod
-    def indices():
+    def _check_index_is_valid(self):
+        self._index_exists()   
+
+
+    def _get_writer(self):
+        writer = None
+        if self.use_multiprocessing:
+            writer = MultiSegmentWriter(self.search_index)
+        else:
+            writer = self.search_index.writer()
+        
+        return writer
+
+
+    def create_index(self):
         """
-        Return all the index objects registered.
+        Creates an index from the supplied `schema`.
+
+        `schema` should be an object of whoosh.fields.Schema.
         """
-        return IndexRegistry._registry
+        if not schema:
+            return
 
-    @staticmethod
-    def get(identifier, not_found=None):
+        if not os.path.exists(self.search_index_dir):
+            os.mkdir(self.search_index_dir)
+
+        self.search_index = whoosh.index.create_in(self.search_index_dir,
+                indexname=self.search_index_name, schema=self.schema)
+        
+
+    def add_document(self, **document):
+        """
+        Adds a document to the index represented by this class.
         """
-        Return an index identified bu the `identifier`.
+        self._check_index_is_valid()
+        writer = self._get_writer()
+        writer.add_document(**document)
+        writer.commit()
 
-        Returns `not_found` if the index object was not found.
-        in the regstered indices.
+    def add_documents(self, documents):
         """
-        index = IndexRegistry._registry.get(identifier, not_found)
-        return index
+        Adds multiple documents to the index.
 
-    @staticmethod
-    def get_index_for_object(db_object, not_found=None):
+        documents should be an iterable object composed of dicts.
         """
-        Returns the index object for the given db model object.
+        self._check_index_is_valid()
+        writer = self._get_writer()
+        for document in documents:
+            writer.add_document(**document)
+
+        writer.commit()
+
+    def update_document(self, document={}):
         """
-        identifier = db_object.__tablename__
-        return IndexRegistry.get(identifier, not_found)
+        Updates an existing document in the index.
+
+        The index must contain a field which is defined as unique and is
+        indexed.
+        """
+        self._check_index_is_valid()
+        writer = self._get_writer()
+        writer.update_document(**document)
+        writer.commit()
+
diff --git a/mediagoblin/plugins/search/indices.py 
b/mediagoblin/plugins/search/indices.py
deleted file mode 100644
index 648cebf..0000000
--- a/mediagoblin/plugins/search/indices.py
+++ /dev/null
@@ -1,124 +0,0 @@
-import logging
-import os
-
-from mediagoblin.tools import pluginapi
-from mediagoblin.plugins.search.base import IndexRegistry
-from mediagoblin.plugins.search.exceptions import IndexDoesNotExistsError
-from mediagoblin.plugins.search.schemas import MediaEntryIndexSchema
-
-import whoosh
-
-from whoosh.filedb.multiproc import MultiSegmentWriter
-
-config = pluginapi.get_config('mediagoblin.plugins.search')
-_log = logging.getLogger(__name__)
-
-
-class SearchIndex(object):
-    """
-    Represents a search index. 
-
-    This class encapsulates various methods of Whoosh API
-    for creating, modifying, updating and searching in a search
-    index.
-    """
-    
-    def __init__(self, model, schema, search_index_dir=None, 
use_multiprocessing=None):
-        self.schema = schema
-        self.field_names = self.schema.field_names
-
-        self.search_index = None
-        self.search_index_name = self.__class__.__name__.lower()
-        
-        self.search_index_dir = search_index_dir
-        if not self.search_index_dir:
-            self.search_index_dir = config['search_index_dir']
-        
-        self.use_multiprocessing = use_multiprocessing 
-        if not self.use_multiprocessing:
-            self.use_multiprocessing = config['use_multiprocessing']
-    
-        self.create_index()
-
-    def _index_exists(self):
-        """
-        Returns whether a valid index exists in self.search_index_dir.
-        
-        If self.search_index is None, it implies that no index has been
-        created yet. In this case, and IndexDoesNotExistsError exception
-        is raised.
-        """
-        if not self.search_index:
-            raise IndexDoesNotExistsError(
-                self.search_index_dir, self.search_index_name)
-        
-        if self.search_index.exists_in(
-            self.search_index_dir, indexname=self.search_index_name):
-            return True
-
-        return False
-    
-    def _check_index_is_valid(self):
-        self._index_exists()   
-
-
-    def _get_writer(self):
-        writer = None
-        if self.use_multiprocessing:
-            writer = MultiSegmentWriter(self.search_index)
-        else:
-            writer = self.search_index.writer()
-        
-        return writer
-
-
-    def create_index(self):
-        """
-        Creates an index from the supplied `schema`.
-
-        `schema` should be an object of whoosh.fields.Schema.
-        """
-        if not schema:
-            return
-
-        if not os.path.exists(self.search_index_dir):
-            os.mkdir(self.search_index_dir)
-
-        self.search_index = whoosh.index.create_in(self.search_index_dir,
-                indexname=self.search_index_name, schema=self.schema)
-        
-
-    def add_document(self, **document):
-        """
-        Adds a document to the index represented by this class.
-        """
-        self._check_index_is_valid()
-        writer = self._get_writer()
-        writer.add_document(**document)
-        writer.commit()
-
-    def add_documents(self, documents):
-        """
-        Adds multiple documents to the index.
-
-        documents should be an iterable object composed of dicts.
-        """
-        self._check_index_is_valid()
-        writer = self._get_writer()
-        for document in documents:
-            writer.add_document(**document)
-
-        writer.commit()
-
-    def update_document(self, document={}):
-        """
-        Updates an existing document in the index.
-
-        The index must contain a field which is defined as unique and is
-        indexed.
-        """
-        self._check_index_is_valid()
-        writer = self._get_writer()
-        writer.update_document(**document)
-        writer.commit()
-
diff --git a/mediagoblin/plugins/search/registry.py 
b/mediagoblin/plugins/search/registry.py
new file mode 100644
index 0000000..2636fef
--- /dev/null
+++ b/mediagoblin/plugins/search/registry.py
@@ -0,0 +1,37 @@
+
+class IndexRegistry(object):
+    _registry = {}
+
+    @staticmethod
+    def register(search_index_obj):
+        """
+        Registers an index object.
+        """
+        identifier = search_index_obj.__tablename__
+        IndexRegistry._registry[identifier] = search_index_obj
+    
+    @staticmethod
+    def indices():
+        """
+        Return all the index objects registered.
+        """
+        return IndexRegistry._registry
+
+    @staticmethod
+    def get(identifier, not_found=None):
+        """
+        Return an index identified bu the `identifier`.
+
+        Returns `not_found` if the index object was not found.
+        in the regstered indices.
+        """
+        index = IndexRegistry._registry.get(identifier, not_found)
+        return index
+
+    @staticmethod
+    def get_index_for_object(db_object, not_found=None):
+        """
+        Returns the index object for the given db model object.
+        """
+        identifier = db_object.__tablename__
+        return IndexRegistry.get(identifier, not_found)
-- 
1.8.5.3



reply via email to

[Prev in Thread] Current Thread [Next in Thread]