瀏覽代碼

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

Alexandre Flament 8 年之前
父節點
當前提交
ee080feaed
共有 4 個文件被更改,包括 40 次插入35 次删除
  1. 2
    1
      searx/settings.yml
  2. 2
    1
      searx/settings_robot.yml
  3. 17
    15
      searx/utils.py
  4. 19
    18
      searx/webapp.py

+ 2
- 1
searx/settings.yml 查看文件

16
     http_protocol_version : "1.0"  # 1.0 and 1.1 are supported
16
     http_protocol_version : "1.0"  # 1.0 and 1.1 are supported
17
 
17
 
18
 ui:
18
 ui:
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
20
     default_theme : oscar # ui theme
21
     default_theme : oscar # ui theme
21
     default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section
22
     default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section
22
 
23
 

+ 2
- 1
searx/settings_robot.yml 查看文件

16
     http_protocol_version : "1.0"
16
     http_protocol_version : "1.0"
17
 
17
 
18
 ui:
18
 ui:
19
-    themes_path : ""
19
+    static_path : ""
20
+    templates_path : ""
20
     default_theme : oscar
21
     default_theme : oscar
21
     default_locale : ""
22
     default_locale : ""
22
 
23
 

+ 17
- 15
searx/utils.py 查看文件

178
             self.writerow(row)
178
             self.writerow(row)
179
 
179
 
180
 
180
 
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
183
 
187
 
184
-    static_path = os.path.join(root, 'static')
185
-    templates_path = os.path.join(root, 'templates')
186
 
188
 
189
+def get_themes(static_path):
190
+    """Returns available themes list."""
187
     themes = os.listdir(os.path.join(static_path, 'themes'))
191
     themes = os.listdir(os.path.join(static_path, 'themes'))
188
     if '__common__' in themes:
192
     if '__common__' in themes:
189
         themes.remove('__common__')
193
         themes.remove('__common__')
190
-    return static_path, templates_path, themes
194
+    return themes
191
 
195
 
192
 
196
 
193
-def get_static_files(base_path):
194
-    base_path = os.path.join(base_path, 'static')
197
+def get_static_files(static_path):
195
     static_files = set()
198
     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):
198
         for filename in files:
201
         for filename in files:
199
-            f = os.path.join(directory[base_path_length:], filename)
202
+            f = os.path.join(directory[static_path_length:], filename)
200
             static_files.add(f)
203
             static_files.add(f)
201
     return static_files
204
     return static_files
202
 
205
 
203
 
206
 
204
-def get_result_templates(base_path):
205
-    base_path = os.path.join(base_path, 'templates')
207
+def get_result_templates(templates_path):
206
     result_templates = set()
208
     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):
209
         if directory.endswith('result_templates'):
211
         if directory.endswith('result_templates'):
210
             for filename in files:
212
             for filename in files:
211
-                f = os.path.join(directory[base_path_length:], filename)
213
+                f = os.path.join(directory[templates_path_length:], filename)
212
                 result_templates.add(f)
214
                 result_templates.add(f)
213
     return result_templates
215
     return result_templates
214
 
216
 

+ 19
- 18
searx/webapp.py 查看文件

56
     categories, engines, engine_shortcuts, get_engines_stats, initialize_engines
56
     categories, engines, engine_shortcuts, get_engines_stats, initialize_engines
57
 )
57
 )
58
 from searx.utils import (
58
 from 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
62
 )
62
 )
63
 from searx.version import VERSION_STRING
63
 from searx.version import VERSION_STRING
64
 from searx.languages import language_codes
64
 from searx.languages import language_codes
91
 from werkzeug.serving import WSGIRequestHandler
91
 from werkzeug.serving import WSGIRequestHandler
92
 WSGIRequestHandler.protocol_version = "HTTP/{}".format(settings['server'].get('http_protocol_version', '1.0'))
92
 WSGIRequestHandler.protocol_version = "HTTP/{}".format(settings['server'].get('http_protocol_version', '1.0'))
93
 
93
 
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)
98
 
98
 
99
+# about templates
99
 default_theme = settings['ui']['default_theme']
100
 default_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(static_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)
100
 
111
 
101
-static_files = get_static_files(searx_dir)
102
-
103
-result_templates = get_result_templates(searx_dir)
104
-
112
+# Flask app
105
 app = Flask(
113
 app = Flask(
106
     __name__,
114
     __name__,
107
     static_folder=static_path,
115
     static_folder=static_path,
120
 rtl_locales = ['ar', 'arc', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'glk', 'he',
128
 rtl_locales = ['ar', 'arc', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'glk', 'he',
121
                'ku', 'mzn', 'pnb'', ''ps', 'sd', 'ug', 'ur', 'yi']
129
                'ku', 'mzn', 'pnb'', ''ps', 'sd', 'ug', 'ur', 'yi']
122
 
130
 
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
-
130
 # used when translating category names
131
 # used when translating category names
131
 _category_names = (gettext('files'),
132
 _category_names = (gettext('files'),
132
                    gettext('general'),
133
                    gettext('general'),