|
@@ -36,7 +36,7 @@ def build_key_to_index(seq, key):
|
36
|
36
|
return dict((d[key], index) for (index, d) in enumerate(seq))
|
37
|
37
|
|
38
|
38
|
|
39
|
|
-def check_file(file_name):
|
|
39
|
+def file_or_none(file_name):
|
40
|
40
|
if isfile(file_name):
|
41
|
41
|
return file_name
|
42
|
42
|
else:
|
|
@@ -48,22 +48,41 @@ def load_yaml(file_name):
|
48
|
48
|
return load(file_yaml)
|
49
|
49
|
|
50
|
50
|
|
51
|
|
-def load_embedded_yaml(name):
|
52
|
|
- file_name = join(searx_dir, name)
|
53
|
|
- logger.debug('read configuration from %s', file_name)
|
54
|
|
- if check_file(file_name):
|
55
|
|
- return load_yaml(file_name)
|
|
51
|
+def get_embedded_filename(name):
|
|
52
|
+ return join(searx_dir, name)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+def update_settings_with_path(settings, path, key=None):
|
|
56
|
+ if path is None or path == '':
|
|
57
|
+ return
|
|
58
|
+ elif isfile(path):
|
|
59
|
+ logger.debug('read configuration from %s', path)
|
|
60
|
+ y = load_yaml(path)
|
|
61
|
+ if isinstance(settings, dict):
|
|
62
|
+ settings.update(y)
|
|
63
|
+ elif isinstance(settings, list):
|
|
64
|
+ kindex = build_key_to_index(settings, key)
|
|
65
|
+ for e in y:
|
|
66
|
+ if key is not None and key in e:
|
|
67
|
+ i = kindex.get(e[key], None)
|
|
68
|
+ if i is not None:
|
|
69
|
+ settings[i] = e
|
|
70
|
+ continue
|
|
71
|
+ settings.append(e)
|
|
72
|
+ else:
|
|
73
|
+ logger.error('%s content is neither a list nor a dictionary')
|
56
|
74
|
else:
|
57
|
|
- logger.warning('{0} is not found.')
|
|
75
|
+ logger.error('%s is not a file', path)
|
|
76
|
+
|
58
|
77
|
|
59
|
78
|
# find location of settings.yml
|
60
|
79
|
if 'SEARX_SETTINGS_PATH' in environ:
|
61
|
80
|
# if possible set path to settings using the
|
62
|
81
|
# enviroment variable SEARX_SETTINGS_PATH
|
63
|
|
- user_settings_path = check_file(environ['SEARX_SETTINGS_PATH'])
|
|
82
|
+ user_settings_path = file_or_none(environ['SEARX_SETTINGS_PATH'])
|
64
|
83
|
else:
|
65
|
84
|
# if not, get it from searx code base or last solution from /etc/searx
|
66
|
|
- user_settings_path = check_file(join(searx_dir, 'settings.yml')) or check_file('/etc/searx/settings.yml')
|
|
85
|
+ user_settings_path = file_or_none(get_embedded_filename('settings.yml')) or file_or_none('/etc/searx/settings.yml')
|
67
|
86
|
|
68
|
87
|
if not user_settings_path:
|
69
|
88
|
raise Exception('settings.yml not found')
|
|
@@ -106,15 +125,26 @@ if OPENSSL_VERSION_INFO[0:3] < (1, 0, 2):
|
106
|
125
|
'''
|
107
|
126
|
Load all settings
|
108
|
127
|
'''
|
109
|
|
-
|
110
|
128
|
# settings are merged from different yml files
|
111
|
|
-settings = dict()
|
|
129
|
+settings = {
|
|
130
|
+ 'search': {
|
|
131
|
+ 'engines': list()
|
|
132
|
+ },
|
|
133
|
+ 'engines': list(),
|
|
134
|
+ 'locales': dict(),
|
|
135
|
+ 'doi': dict()
|
|
136
|
+}
|
112
|
137
|
# load embedded settings first
|
113
|
|
-settings.update(load_embedded_yaml('engines.yml'))
|
114
|
|
-settings.update(load_embedded_yaml('doi.yml'))
|
115
|
|
-settings.update(load_embedded_yaml('locales.yml'))
|
116
|
|
-# load user settings at the end (may override embedded settings)
|
|
138
|
+update_settings_with_path(settings['engines'],
|
|
139
|
+ user_settings.get('search', {}).get('engines_path', None)
|
|
140
|
+ or get_embedded_filename('engines.yml'))
|
|
141
|
+update_settings_with_path(settings['doi'], get_embedded_filename('doi.yml'))
|
|
142
|
+update_settings_with_path(settings['locales'], get_embedded_filename('locales.yml'))
|
|
143
|
+# load user settings (may override embedded settings)
|
117
|
144
|
settings.update(user_settings)
|
|
145
|
+# add additional engines (replace engine definition if it already exists)
|
|
146
|
+update_settings_with_path(settings['engines'],
|
|
147
|
+ user_settings.get('search', {}).get('include_engines_path', None), 'name')
|
118
|
148
|
# are there some user engine settings ?
|
119
|
149
|
user_engine_settings = settings.get('search', {}).get('engines', None)
|
120
|
150
|
if user_engine_settings:
|