-
Notifications
You must be signed in to change notification settings - Fork 3
/
manager.py
121 lines (89 loc) · 3.84 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import config
import pprint
"""
.. module:: manager
:platform: Unix, Windows, Linux
:synopsis: Code for managing indexing and searching data converters
.. :moduleauthor: Michal Domanski <[email protected]>
"""
class Manager(object):
"""
Generic object for managing Searchables/Indexables module loading \n
.. rubric:: Usage:
>>> from models import Advert
>>> ad = Advert.objects.filter(title__icontains='test')[:1][0]
>>> doc = Manager(model_name=ad.__class__.__name__.lower()).init_from(ad)
"""
def __init__(self, *args, **kwargs):
self.model_name = kwargs.get('model_name')
self._searchables = []
self._indexables = []
@property
def searchables(self):
"""
List of modules that are initialized from object for searching using solr.
"""
if not self._searchables:
self._load_searchables()
return self._searchables
@property
def indexables(self):
"""
List of modules that are initialized from object for indexing data into solr.
"""
if not self._indexables:
self._load_indexables()
return self._indexables
def _load_modules(self):
"""
Loads all modules defined in config.SEARCH_PLUGINS into local attribute
"""
self._modules = [__import__(module_name, None, None, ['']) for module_name in config.SEARCH_PLUGINS]
def _load_ables(self, attr_name, ables):
if not self._modules: self._load_modules()
for module in self._modules:
loaded_ables = getattr(module, attr_name, dict())
if loaded_ables:
for able, mapping in loaded_ables.iteritems():
if self.model_name in mapping and not able in ables:
ables.append(able)
def _load_searchables(self):
"""
using modules loaded, performs the searching and loading of searchable for given model
"""
if not hasattr(self,'_modules'): self._load_modules()
self._load_ables("SEARCHABLES", self._searchables)
def _load_indexables(self):
"""
using modules loaded, performs the searching and loading of indexables for given model
"""
if not hasattr(self,'_modules'): self._load_modules()
self._load_ables("INDEXABLES", self._indexables)
def _show_loaded_modules(self):
self._load_modules()
pprint.pprint(self._modules)
def init_from(self, object):
"""
Only method of manager instance that should be actually used.
.. warning::
You need to write indexables and searchables and configure them for object you want to index.
Check :mod:`interface` for reference on this.
:param object: object that represents data you want to index
:type object: any object that can be used to initialize indexables configured for it
"""
data = dict()
indexables = [i(object = object) for i in self.indexables]
for indexable in indexables:
data.update(indexable.indexed_as())
return data