on master help sign in
Merge pull request #816 from dalf/debian

[mod] the static and templates directories can be defined in the settings.yml

commitc0bb89fd4660b0577f29a184bf330c0a276930df authorAdam Tauber <asciimoo@gmail.com> dateTue 16 May 2017, 18:35 (9 years ago) parentc233bf09c91ab3
5 files changed+59−43
searx/__init__.py+18−7 searx/settings.yml+2−1 searx/settings_robot.yml+2−1 searx/utils.py+18−16 searx/webapp.py+19−18
searx/__init__.py+18−7
@@ -18,7 +18,7 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
1818import certifi
1919import logging
2020from os import environ
21−from os.path import realpath, dirname, join, abspath
21+from os.path import realpath, dirname, join, abspath, isfile
2222from ssl import OPENSSL_VERSION_INFO, OPENSSL_VERSION
2323try:
2424 from yaml import load
@@ -30,13 +30,24 @@ except:
3030searx_dir = abspath(dirname(__file__))
3131engine_dir = dirname(realpath(__file__))
3232
33−# if possible set path to settings using the
34−# enviroment variable SEARX_SETTINGS_PATH
33+
34+def check_settings_yml(file_name):
35+ if isfile(file_name):
36+ return file_name
37+ else:
38+ return None
39+
40+# find location of settings.yml
3541if 'SEARX_SETTINGS_PATH' in environ:
36− settings_path = environ['SEARX_SETTINGS_PATH']
37−# otherwise using default path
42+ # if possible set path to settings using the
43+ # enviroment variable SEARX_SETTINGS_PATH
44+ settings_path = check_settings_yml(environ['SEARX_SETTINGS_PATH'])
3845else:
39− settings_path = join(searx_dir, 'settings.yml')
46+ # if not, get it from searx code base or last solution from /etc/searx
47+ settings_path = check_settings_yml(join(searx_dir, 'settings.yml')) or check_settings_yml('/etc/searx/settings.yml')
48+
49+if not settings_path:
50+ raise Exception('settings.yml not found')
4051
4152# load settings
4253with open(settings_path) as settings_yaml:
@@ -67,7 +78,7 @@ else:
6778 logging.basicConfig(level=logging.WARNING)
6879
6980logger = logging.getLogger('searx')
70−
81+logger.debug('read configuration from %s', settings_path)
7182# Workaround for openssl versions <1.0.2
7283# https://github.com/certifi/python-certifi/issues/26
7384if OPENSSL_VERSION_INFO[0:3] < (1, 0, 2):
searx/settings.yml+2−1
@@ -16,7 +16,8 @@ server:
1616 http_protocol_version : "1.0" # 1.0 and 1.1 are supported
1717
1818ui:
19− themes_path : "" # Custom ui themes path - leave it blank if you didn't change
19+ static_path : "" # Custom static path - leave it blank if you didn't change
20+ templates_path : "" # Custom templates path - leave it blank if you didn't change
2021 default_theme : oscar # ui theme
2122 default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section
2223
searx/settings_robot.yml+2−1
@@ -16,7 +16,8 @@ server:
1616 http_protocol_version : "1.0"
1717
1818ui:
19− themes_path : ""
19+ static_path : ""
20+ templates_path : ""
2021 default_theme : oscar
2122 default_locale : ""
2223
searx/utils.py+18−16
@@ -178,37 +178,39 @@ class UnicodeWriter:
178178 self.writerow(row)
179179
180180
181−def get_themes(root):
182− """Returns available themes list."""
181+def get_resources_directory(searx_directory, subdirectory, resources_directory):
182+ if not resources_directory:
183+ resources_directory = os.path.join(searx_directory, subdirectory)
184+ if not os.path.isdir(resources_directory):
185+ raise Exception(directory + " is not a directory")
186+ return resources_directory
183187
184− static_path = os.path.join(root, 'static')
185− templates_path = os.path.join(root, 'templates')
186188
187− themes = os.listdir(os.path.join(static_path, 'themes'))
189+def get_themes(templates_path):
190+ """Returns available themes list."""
191+ themes = os.listdir(templates_path)
188192 if '__common__' in themes:
189193 themes.remove('__common__')
190− return static_path, templates_path, themes
194+ return themes
191195
192196
193−def get_static_files(base_path):
194− base_path = os.path.join(base_path, 'static')
197+def get_static_files(static_path):
195198 static_files = set()
196− base_path_length = len(base_path) + 1
197− for directory, _, files in os.walk(base_path):
199+ static_path_length = len(static_path) + 1
200+ for directory, _, files in os.walk(static_path):
198201 for filename in files:
199− f = os.path.join(directory[base_path_length:], filename)
202+ f = os.path.join(directory[static_path_length:], filename)
200203 static_files.add(f)
201204 return static_files
202205
203206
204−def get_result_templates(base_path):
205− base_path = os.path.join(base_path, 'templates')
207+def get_result_templates(templates_path):
206208 result_templates = set()
207− base_path_length = len(base_path) + 1
208− for directory, _, files in os.walk(base_path):
209+ templates_path_length = len(templates_path) + 1
210+ for directory, _, files in os.walk(templates_path):
209211 if directory.endswith('result_templates'):
210212 for filename in files:
211− f = os.path.join(directory[base_path_length:], filename)
213+ f = os.path.join(directory[templates_path_length:], filename)
212214 result_templates.add(f)
213215 return result_templates
214216
searx/webapp.py+19−18
@@ -56,9 +56,9 @@ from searx.engines import (
5656 categories, engines, engine_shortcuts, get_engines_stats, initialize_engines
5757)
5858from searx.utils import (
59− UnicodeWriter, highlight_content, html_to_text, get_themes,
60− get_static_files, get_result_templates, gen_useragent, dict_subset,
61− prettify_url
59+ UnicodeWriter, highlight_content, html_to_text, get_resources_directory,
60+ get_static_files, get_result_templates, get_themes, gen_useragent,
61+ dict_subset, prettify_url
6262)
6363from searx.version import VERSION_STRING
6464from searx.languages import language_codes
@@ -91,17 +91,25 @@ if sys.version_info[0] == 3:
9191from werkzeug.serving import WSGIRequestHandler
9292WSGIRequestHandler.protocol_version = "HTTP/{}".format(settings['server'].get('http_protocol_version', '1.0'))
9393
94−static_path, templates_path, themes =\
95− get_themes(settings['ui']['themes_path']
96− if settings['ui']['themes_path']
97− else searx_dir)
94+# about static
95+static_path = get_resources_directory(searx_dir, 'static', settings['ui']['static_path'])
96+logger.debug('static directory is %s', static_path)
97+static_files = get_static_files(static_path)
9898
99+# about templates
99100default_theme = settings['ui']['default_theme']
101+templates_path = get_resources_directory(searx_dir, 'templates', settings['ui']['templates_path'])
102+logger.debug('templates directory is %s', templates_path)
103+themes = get_themes(templates_path)
104+result_templates = get_result_templates(templates_path)
105+global_favicons = []
106+for indice, theme in enumerate(themes):
107+ global_favicons.append([])
108+ theme_img_path = os.path.join(static_path, 'themes', theme, 'img', 'icons')
109+ for (dirpath, dirnames, filenames) in os.walk(theme_img_path):
110+ global_favicons[indice].extend(filenames)
100111
101−static_files = get_static_files(searx_dir)
102−
103−result_templates = get_result_templates(searx_dir)
104−
112+# Flask app
105113app = Flask(
106114 __name__,
107115 static_folder=static_path,
@@ -120,13 +128,6 @@ babel = Babel(app)
120128rtl_locales = ['ar', 'arc', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'glk', 'he',
121129 'ku', 'mzn', 'pnb'', ''ps', 'sd', 'ug', 'ur', 'yi']
122130
123−global_favicons = []
124−for indice, theme in enumerate(themes):
125− global_favicons.append([])
126− theme_img_path = searx_dir + "/static/themes/" + theme + "/img/icons/"
127− for (dirpath, dirnames, filenames) in os.walk(theme_img_path):
128− global_favicons[indice].extend(filenames)
129−
130131# used when translating category names
131132_category_names = (gettext('files'),
132133 gettext('general'),