summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorasciimoo <asciimoo@gmail.com>2013-10-23 23:54:46 +0200
committerasciimoo <asciimoo@gmail.com>2013-10-23 23:54:46 +0200
commit39d229e1104dc10c7c7f00380c02d46118e3d895 (patch)
tree2bdbb2e3bd5c85a06ca913214f2fc97a5a27d5a5
parent75a2a6fb73219c2502e527187cab0547d685f7f3 (diff)
downloadsearxng-39d229e1104dc10c7c7f00380c02d46118e3d895.tar.gz
searxng-39d229e1104dc10c7c7f00380c02d46118e3d895.zip
[enh][mod] engines.cfg support
-rw-r--r--searx/engines/__init__.py42
1 files changed, 28 insertions, 14 deletions
diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py
index ee215a6ef..0a875b5b5 100644
--- a/searx/engines/__init__.py
+++ b/searx/engines/__init__.py
@@ -17,37 +17,51 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
'''
from os.path import realpath, dirname, splitext, join
-from os import listdir
from imp import load_source
import grequests
from itertools import izip_longest, chain
from operator import itemgetter
from urlparse import urlparse
from searx import settings
+import ConfigParser
+import sys
engine_dir = dirname(realpath(__file__))
+searx_dir = join(engine_dir, '../../')
+
+engines_config = ConfigParser.SafeConfigParser()
+engines_config.read(join(searx_dir, 'engines.cfg'))
engines = {}
categories = {'general': []}
-for filename in listdir(engine_dir):
- if filename.startswith('_') or not filename.endswith('.py'):
- continue
+def load_module(filename):
modname = splitext(filename)[0]
- if modname in settings.blacklist:
- continue
+ if modname in sys.modules:
+ del sys.modules[modname]
filepath = join(engine_dir, filename)
- engine = load_source(modname, filepath)
- engine.name = modname
- if not hasattr(engine, 'request') or not hasattr(engine, 'response'):
- continue
- engines[modname] = engine
- if not hasattr(engine, 'categories'):
- categories['general'].append(engine)
- else:
+ module = load_source(modname, filepath)
+ module.name = modname
+ return module
+
+for section in engines_config.sections():
+ engine_data = engines_config.options(section)
+ engine = load_module(engines_config.get(section, 'engine')+'.py')
+ engine.name = section
+ for param_name in engine_data:
+ if param_name == 'engine':
+ continue
+ if param_name == 'categories':
+ engine.categories = map(str.strip, engines_config.get(section, param_name).split(','))
+ continue
+ setattr(engine, param_name, engines_config.get(section, param_name))
+ engines[engine.name] = engine
+ if hasattr(engine, 'categories'):
for category_name in engine.categories:
categories.setdefault(category_name, []).append(engine)
+ else:
+ categories['general'].append(engine)
def default_request_params():
return {'method': 'GET', 'headers': {}, 'data': {}, 'url': '', 'cookies': {}}