Browse Source

[mod] query parsing refactor vol.1

asciimoo 11 years ago
parent
commit
7f45e8a562
1 changed files with 25 additions and 20 deletions
  1. 25
    20
      searx/search.py

+ 25
- 20
searx/search.py View File

@@ -13,7 +13,6 @@ class Search(object):
13 13
         self.query = None
14 14
         self.engines = []
15 15
         self.categories = []
16
-        query_engines = []
17 16
         self.paging = False
18 17
         self.pageno = 1
19 18
         self.lang = 'all'
@@ -46,25 +45,7 @@ class Search(object):
46 45
 
47 46
         self.pageno = int(pageno_param)
48 47
 
49
-        query_parts = self.query.split()
50
-        if query_parts[0].startswith('!'):
51
-            prefix = query_parts[0][1:].replace('_', ' ')
52
-            if prefix in engine_shortcuts\
53
-               and not engine_shortcuts[prefix] in self.blocked_engines:
54
-                self.engines.append({'category': 'none',
55
-                                     'name': engine_shortcuts[prefix]})
56
-            elif prefix in engines\
57
-                    and not prefix in self.blocked_engines:
58
-                self.engines.append({'category': 'none',
59
-                                    'name': prefix})
60
-            elif prefix in categories:
61
-                self.engines.extend({'category': prefix,
62
-                                    'name': engine.name}
63
-                                    for engine in categories[prefix]
64
-                                    if not engine in self.blocked_engines)
65
-
66
-        if len(query_engines):
67
-            self.query = self.query.replace(query_parts[0], '', 1).strip()
48
+        self.parse_query()
68 49
 
69 50
         self.categories = []
70 51
 
@@ -92,3 +73,27 @@ class Search(object):
92 73
                                      'name': x.name}
93 74
                                     for x in categories[categ]
94 75
                                     if not x.name in self.blocked_engines)
76
+
77
+    def parse_query(self):
78
+        query_parts = self.query.split()
79
+        modified = False
80
+        if query_parts[0].startswith('!'):
81
+            prefix = query_parts[0][1:].replace('_', ' ')
82
+            if prefix in engine_shortcuts\
83
+               and not engine_shortcuts[prefix] in self.blocked_engines:
84
+                modified = True
85
+                self.engines.append({'category': 'none',
86
+                                     'name': engine_shortcuts[prefix]})
87
+            elif prefix in engines\
88
+                    and not prefix in self.blocked_engines:
89
+                modified = True
90
+                self.engines.append({'category': 'none',
91
+                                    'name': prefix})
92
+            elif prefix in categories:
93
+                modified = True
94
+                self.engines.extend({'category': prefix,
95
+                                    'name': engine.name}
96
+                                    for engine in categories[prefix]
97
+                                    if not engine in self.blocked_engines)
98
+        if modified:
99
+            self.query = self.query.replace(query_parts[0], '', 1).strip()