~jendrikseipp/pogo/github-mirror

« back to all changes in this revision

Viewing changes to pogo/modules/Search.py

  • Committer: GitHub
  • Author(s): Jendrik Seipp
  • Date: 2017-07-16 15:22:48 UTC
  • mfrom: (695.1.4)
  • Revision ID: git-v1:08a9790f5259c0afc6beb390a10358e104e209c9
Merge pull request #42 from jendrikseipp/pep8

Fix code style (PEP8)

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
CACHE_QUERY = "Caching files"
43
43
 
44
44
 
45
 
 
46
45
class Search(modules.ThreadedModule):
47
46
 
48
47
    def __init__(self):
49
48
        """ Constructor """
50
49
        handlers = {
51
 
                        consts.MSG_EVT_APP_STARTED:         self.onAppStarted,
52
 
                        consts.MSG_EVT_SEARCH_START:        self.onSearch,
53
 
                        consts.MSG_EVT_MUSIC_PATHS_CHANGED: self.onPathsChanged,
54
 
                   }
 
50
            consts.MSG_EVT_APP_STARTED: self.onAppStarted,
 
51
            consts.MSG_EVT_SEARCH_START: self.onSearch,
 
52
            consts.MSG_EVT_MUSIC_PATHS_CHANGED: self.onPathsChanged,
 
53
        }
55
54
 
56
55
        modules.ThreadedModule.__init__(self, handlers)
57
56
 
58
 
 
59
57
    def gtk_initialize(self):
60
58
        """ Called by GTK main loop. """
61
59
        wTree = tools.prefs.getWidgetsTree()
83
81
        main_win.connect('key-press-event', self.on_key_pressed)
84
82
        self.searchbox.grab_focus()
85
83
 
86
 
 
87
84
    def search_dir(self, dir, query):
88
85
        if dir == consts.dirBaseUsr:
89
86
            cmd = ['locate', '--existing', '--ignore-case',
118
115
        logging.info('Results for %s in %s: %s' % (query, dir, len(results)))
119
116
        return results
120
117
 
121
 
 
122
118
    def stop_searches(self):
123
119
        logging.info('Stopping all searches')
124
120
        self.should_stop = True
129
125
                search.kill()
130
126
        self.searches = []
131
127
 
132
 
 
133
128
    def filter_results(self, results, search_path, regex):
134
129
        '''
135
130
        Remove subpaths of parent directories
176
171
 
177
172
        return (dirs, files)
178
173
 
179
 
 
180
174
    def cache_dirs(self, keep_caching):
181
175
        for index, path in enumerate(self.paths):
182
176
            # Cache dirs one by one after a small timeout
185
179
        # Keep caching in regular intervals
186
180
        return keep_caching
187
181
 
188
 
 
189
182
    def get_search_paths(self):
190
183
        """Do not search in subdirectories if we already search in parent dir.
191
184
 
201
194
        logging.info('Searching at %s' % search_paths)
202
195
        return search_paths
203
196
 
204
 
 
205
197
    # --== Message handlers ==--
206
198
 
207
 
 
208
199
    def onAppStarted(self):
209
200
        """ The module has been loaded.
210
201
 
221
212
            # Cache the music folders regularly for faster searches
222
213
            GObject.timeout_add_seconds(100, self.cache_dirs, True)
223
214
 
224
 
 
225
215
    def onSearch(self, query):
226
216
        self.should_stop = False
227
217
        self.found_something = False
228
218
 
229
219
        # Transform whitespace-separated query into OR-regex.
230
220
        regex = re.compile('|'.join(tools.get_pattern(word)
231
 
                           for word in query.split()), re.IGNORECASE)
 
221
                                    for word in query.split()), re.IGNORECASE)
232
222
 
233
223
        for dir in self.get_search_paths() + [consts.dirBaseUsr]:
234
224
            # Check if search has been aborted during filtering
253
243
 
254
244
        modules.postMsg(consts.MSG_EVT_SEARCH_END)
255
245
 
256
 
 
257
246
    def onPathsChanged(self, paths):
258
247
        self.paths = paths
259
248
        if self.allow_caching:
260
249
            # Cache the new paths once
261
250
            GObject.timeout_add_seconds(5, self.cache_dirs, False)
262
251
 
263
 
 
264
 
    #------- GTK handlers ----------------
 
252
    # ------- GTK handlers ----------------
265
253
 
266
254
    def on_key_pressed(self, widget, event):
267
255
        """
274
262
            self.searchbox.grab_focus()
275
263
            return True
276
264
 
277
 
 
278
265
    def on_searchbox_activate(self, _entry):
279
266
        self.stop_searches()
280
267
        query = self.searchbox.get_text().strip()
286
273
        logging.info('Query: %s' % query)
287
274
        modules.postMsg(consts.MSG_EVT_SEARCH_START, {'query': query})
288
275
 
289
 
 
290
276
    def on_searchbox_changed(self, _entry):
291
277
        if self.searchbox.get_text().strip() == '':
292
278
            self.stop_searches()
293
279
            modules.postMsg(consts.MSG_EVT_SEARCH_RESET, {})
294
280
 
295
 
 
296
281
    def on_searchbox_clear(self, _entry, icon_pos, _event):
297
282
        '''
298
283
        An icon has been pressed