@@ -0,0 +1,125 @@
4+searx is free software: you can redistribute it and/or modify
5+it under the terms of the GNU Affero General Public License as published by
6+the Free Software Foundation, either version 3 of the License, or
7+(at your option) any later version.
9+searx is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU Affero General Public License for more details.
14+You should have received a copy of the GNU Affero General Public License
15+along with searx. If not, see < http://www.gnu.org/licenses/ >.
17+(C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
20+from searx.languages import language_codes
21+from searx.engines import (
22+ categories, engines, engine_shortcuts
31+ def __init__(self, query, blocked_engines):
33+ self.blocked_engines = []
36+ self.blocked_engines = blocked_engines
38+ self.query_parts = []
42+ def parse_query(self):
43+ self.query_parts = []
45+ # split query, including whitespaces
46+ raw_query_parts = re.split(r'(\s+)', self.query)
50+ for query_part in raw_query_parts:
52+ self.query_parts[-1] += query_part
57+ # part does only contain spaces, skip
58+ if query_part.isspace():
60+ self.query_parts.append(query_part)
63+ # this force a language
64+ if query_part[0] == ':':
65+ lang = query_part[1:].lower()
67+ # check if any language-code is equal with declared language-codes
68+ for lc in language_codes:
69+ lang_id, lang_name, country = map(str.lower, lc)
71+ # if correct language-code is found, set it as new search-language
73+ or lang_id.startswith(lang)\
74+ or lang == lang_name\
77+ self.languages.append(lang)
80+ # this force a engine or category
81+ if query_part[0] == '!':
82+ prefix = query_part[1:].replace('_', ' ')
84+ # check if prefix is equal with engine shortcut
85+ if prefix in engine_shortcuts\
86+ and not engine_shortcuts[prefix] in self.blocked_engines:
88+ self.engines.append({'category': 'none',
89+ 'name': engine_shortcuts[prefix]})
91+ # check if prefix is equal with engine name
92+ elif prefix in engines\
93+ and not prefix in self.blocked_engines:
95+ self.engines.append({'category': 'none',
98+ # check if prefix is equal with categorie name
99+ elif prefix in categories:
100+ # using all engines for that search, which are declared under that categorie name
102+ self.engines.extend({'category': prefix,
103+ 'name': engine.name}
104+ for engine in categories[prefix]
105+ if not engine in self.blocked_engines)
107+ # append query part to query_part list
108+ self.query_parts.append(query_part)
110+ def changeSearchQuery(self, search_query):
111+ if len(self.query_parts):
112+ self.query_parts[-1] = search_query
114+ self.query_parts.append(search_query)
116+ def getSearchQuery(self):
117+ if len(self.query_parts):
118+ return self.query_parts[-1]
122+ def getFullQuery(self):
123+ # get full querry including whitespaces
124+ return string.join(self.query_parts, '')