mirror of https://github.com/searxng/searxng.git
Merge pull request #816 from dalf/debian
[mod] the static and templates directories can be defined in the settings.yml
This commit is contained in:
commit
c0bb89fd46
|
@ -18,7 +18,7 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
|
||||||
import certifi
|
import certifi
|
||||||
import logging
|
import logging
|
||||||
from os import environ
|
from os import environ
|
||||||
from os.path import realpath, dirname, join, abspath
|
from os.path import realpath, dirname, join, abspath, isfile
|
||||||
from ssl import OPENSSL_VERSION_INFO, OPENSSL_VERSION
|
from ssl import OPENSSL_VERSION_INFO, OPENSSL_VERSION
|
||||||
try:
|
try:
|
||||||
from yaml import load
|
from yaml import load
|
||||||
|
@ -30,13 +30,24 @@ except:
|
||||||
searx_dir = abspath(dirname(__file__))
|
searx_dir = abspath(dirname(__file__))
|
||||||
engine_dir = dirname(realpath(__file__))
|
engine_dir = dirname(realpath(__file__))
|
||||||
|
|
||||||
# if possible set path to settings using the
|
|
||||||
# enviroment variable SEARX_SETTINGS_PATH
|
def check_settings_yml(file_name):
|
||||||
|
if isfile(file_name):
|
||||||
|
return file_name
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# find location of settings.yml
|
||||||
if 'SEARX_SETTINGS_PATH' in environ:
|
if 'SEARX_SETTINGS_PATH' in environ:
|
||||||
settings_path = environ['SEARX_SETTINGS_PATH']
|
# if possible set path to settings using the
|
||||||
# otherwise using default path
|
# enviroment variable SEARX_SETTINGS_PATH
|
||||||
|
settings_path = check_settings_yml(environ['SEARX_SETTINGS_PATH'])
|
||||||
else:
|
else:
|
||||||
settings_path = join(searx_dir, 'settings.yml')
|
# if not, get it from searx code base or last solution from /etc/searx
|
||||||
|
settings_path = check_settings_yml(join(searx_dir, 'settings.yml')) or check_settings_yml('/etc/searx/settings.yml')
|
||||||
|
|
||||||
|
if not settings_path:
|
||||||
|
raise Exception('settings.yml not found')
|
||||||
|
|
||||||
# load settings
|
# load settings
|
||||||
with open(settings_path) as settings_yaml:
|
with open(settings_path) as settings_yaml:
|
||||||
|
@ -67,7 +78,7 @@ else:
|
||||||
logging.basicConfig(level=logging.WARNING)
|
logging.basicConfig(level=logging.WARNING)
|
||||||
|
|
||||||
logger = logging.getLogger('searx')
|
logger = logging.getLogger('searx')
|
||||||
|
logger.debug('read configuration from %s', settings_path)
|
||||||
# Workaround for openssl versions <1.0.2
|
# Workaround for openssl versions <1.0.2
|
||||||
# https://github.com/certifi/python-certifi/issues/26
|
# https://github.com/certifi/python-certifi/issues/26
|
||||||
if OPENSSL_VERSION_INFO[0:3] < (1, 0, 2):
|
if OPENSSL_VERSION_INFO[0:3] < (1, 0, 2):
|
||||||
|
|
|
@ -16,7 +16,8 @@ server:
|
||||||
http_protocol_version : "1.0" # 1.0 and 1.1 are supported
|
http_protocol_version : "1.0" # 1.0 and 1.1 are supported
|
||||||
|
|
||||||
ui:
|
ui:
|
||||||
themes_path : "" # Custom ui themes path - leave it blank if you didn't change
|
static_path : "" # Custom static path - leave it blank if you didn't change
|
||||||
|
templates_path : "" # Custom templates path - leave it blank if you didn't change
|
||||||
default_theme : oscar # ui theme
|
default_theme : oscar # ui theme
|
||||||
default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section
|
default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,8 @@ server:
|
||||||
http_protocol_version : "1.0"
|
http_protocol_version : "1.0"
|
||||||
|
|
||||||
ui:
|
ui:
|
||||||
themes_path : ""
|
static_path : ""
|
||||||
|
templates_path : ""
|
||||||
default_theme : oscar
|
default_theme : oscar
|
||||||
default_locale : ""
|
default_locale : ""
|
||||||
|
|
||||||
|
|
|
@ -178,37 +178,39 @@ class UnicodeWriter:
|
||||||
self.writerow(row)
|
self.writerow(row)
|
||||||
|
|
||||||
|
|
||||||
def get_themes(root):
|
def get_resources_directory(searx_directory, subdirectory, resources_directory):
|
||||||
|
if not resources_directory:
|
||||||
|
resources_directory = os.path.join(searx_directory, subdirectory)
|
||||||
|
if not os.path.isdir(resources_directory):
|
||||||
|
raise Exception(directory + " is not a directory")
|
||||||
|
return resources_directory
|
||||||
|
|
||||||
|
|
||||||
|
def get_themes(templates_path):
|
||||||
"""Returns available themes list."""
|
"""Returns available themes list."""
|
||||||
|
themes = os.listdir(templates_path)
|
||||||
static_path = os.path.join(root, 'static')
|
|
||||||
templates_path = os.path.join(root, 'templates')
|
|
||||||
|
|
||||||
themes = os.listdir(os.path.join(static_path, 'themes'))
|
|
||||||
if '__common__' in themes:
|
if '__common__' in themes:
|
||||||
themes.remove('__common__')
|
themes.remove('__common__')
|
||||||
return static_path, templates_path, themes
|
return themes
|
||||||
|
|
||||||
|
|
||||||
def get_static_files(base_path):
|
def get_static_files(static_path):
|
||||||
base_path = os.path.join(base_path, 'static')
|
|
||||||
static_files = set()
|
static_files = set()
|
||||||
base_path_length = len(base_path) + 1
|
static_path_length = len(static_path) + 1
|
||||||
for directory, _, files in os.walk(base_path):
|
for directory, _, files in os.walk(static_path):
|
||||||
for filename in files:
|
for filename in files:
|
||||||
f = os.path.join(directory[base_path_length:], filename)
|
f = os.path.join(directory[static_path_length:], filename)
|
||||||
static_files.add(f)
|
static_files.add(f)
|
||||||
return static_files
|
return static_files
|
||||||
|
|
||||||
|
|
||||||
def get_result_templates(base_path):
|
def get_result_templates(templates_path):
|
||||||
base_path = os.path.join(base_path, 'templates')
|
|
||||||
result_templates = set()
|
result_templates = set()
|
||||||
base_path_length = len(base_path) + 1
|
templates_path_length = len(templates_path) + 1
|
||||||
for directory, _, files in os.walk(base_path):
|
for directory, _, files in os.walk(templates_path):
|
||||||
if directory.endswith('result_templates'):
|
if directory.endswith('result_templates'):
|
||||||
for filename in files:
|
for filename in files:
|
||||||
f = os.path.join(directory[base_path_length:], filename)
|
f = os.path.join(directory[templates_path_length:], filename)
|
||||||
result_templates.add(f)
|
result_templates.add(f)
|
||||||
return result_templates
|
return result_templates
|
||||||
|
|
||||||
|
|
|
@ -56,9 +56,9 @@ from searx.engines import (
|
||||||
categories, engines, engine_shortcuts, get_engines_stats, initialize_engines
|
categories, engines, engine_shortcuts, get_engines_stats, initialize_engines
|
||||||
)
|
)
|
||||||
from searx.utils import (
|
from searx.utils import (
|
||||||
UnicodeWriter, highlight_content, html_to_text, get_themes,
|
UnicodeWriter, highlight_content, html_to_text, get_resources_directory,
|
||||||
get_static_files, get_result_templates, gen_useragent, dict_subset,
|
get_static_files, get_result_templates, get_themes, gen_useragent,
|
||||||
prettify_url
|
dict_subset, prettify_url
|
||||||
)
|
)
|
||||||
from searx.version import VERSION_STRING
|
from searx.version import VERSION_STRING
|
||||||
from searx.languages import language_codes
|
from searx.languages import language_codes
|
||||||
|
@ -91,17 +91,25 @@ if sys.version_info[0] == 3:
|
||||||
from werkzeug.serving import WSGIRequestHandler
|
from werkzeug.serving import WSGIRequestHandler
|
||||||
WSGIRequestHandler.protocol_version = "HTTP/{}".format(settings['server'].get('http_protocol_version', '1.0'))
|
WSGIRequestHandler.protocol_version = "HTTP/{}".format(settings['server'].get('http_protocol_version', '1.0'))
|
||||||
|
|
||||||
static_path, templates_path, themes =\
|
# about static
|
||||||
get_themes(settings['ui']['themes_path']
|
static_path = get_resources_directory(searx_dir, 'static', settings['ui']['static_path'])
|
||||||
if settings['ui']['themes_path']
|
logger.debug('static directory is %s', static_path)
|
||||||
else searx_dir)
|
static_files = get_static_files(static_path)
|
||||||
|
|
||||||
|
# about templates
|
||||||
default_theme = settings['ui']['default_theme']
|
default_theme = settings['ui']['default_theme']
|
||||||
|
templates_path = get_resources_directory(searx_dir, 'templates', settings['ui']['templates_path'])
|
||||||
|
logger.debug('templates directory is %s', templates_path)
|
||||||
|
themes = get_themes(templates_path)
|
||||||
|
result_templates = get_result_templates(templates_path)
|
||||||
|
global_favicons = []
|
||||||
|
for indice, theme in enumerate(themes):
|
||||||
|
global_favicons.append([])
|
||||||
|
theme_img_path = os.path.join(static_path, 'themes', theme, 'img', 'icons')
|
||||||
|
for (dirpath, dirnames, filenames) in os.walk(theme_img_path):
|
||||||
|
global_favicons[indice].extend(filenames)
|
||||||
|
|
||||||
static_files = get_static_files(searx_dir)
|
# Flask app
|
||||||
|
|
||||||
result_templates = get_result_templates(searx_dir)
|
|
||||||
|
|
||||||
app = Flask(
|
app = Flask(
|
||||||
__name__,
|
__name__,
|
||||||
static_folder=static_path,
|
static_folder=static_path,
|
||||||
|
@ -120,13 +128,6 @@ babel = Babel(app)
|
||||||
rtl_locales = ['ar', 'arc', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'glk', 'he',
|
rtl_locales = ['ar', 'arc', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'glk', 'he',
|
||||||
'ku', 'mzn', 'pnb'', ''ps', 'sd', 'ug', 'ur', 'yi']
|
'ku', 'mzn', 'pnb'', ''ps', 'sd', 'ug', 'ur', 'yi']
|
||||||
|
|
||||||
global_favicons = []
|
|
||||||
for indice, theme in enumerate(themes):
|
|
||||||
global_favicons.append([])
|
|
||||||
theme_img_path = searx_dir + "/static/themes/" + theme + "/img/icons/"
|
|
||||||
for (dirpath, dirnames, filenames) in os.walk(theme_img_path):
|
|
||||||
global_favicons[indice].extend(filenames)
|
|
||||||
|
|
||||||
# used when translating category names
|
# used when translating category names
|
||||||
_category_names = (gettext('files'),
|
_category_names = (gettext('files'),
|
||||||
gettext('general'),
|
gettext('general'),
|
||||||
|
|
Loading…
Reference in New Issue