Parcourir la source

- search.engines_path : path of engines.yml

- search.include_engines_path : add engines definition (doesn't replace the content of engines.yml)

doi.yml, engines.yml, locales.yml are loaded directly into doi, engines, locales (not more prefix)
some bug fixes on oa_doi_rewrite
Alex il y a 6 ans
Parent
révision
53c1398537
8 fichiers modifiés avec 684 ajouts et 654 suppressions
  1. 45
    15
      searx/__init__.py
  2. 2
    3
      searx/doi.yml
  3. 593
    594
      searx/engines.yml
  4. 31
    32
      searx/locales.yml
  5. 6
    5
      searx/plugins/oa_doi_rewrite.py
  6. 1
    1
      searx/preferences.py
  7. 3
    1
      searx/settings.yml
  8. 3
    3
      searx/webapp.py

+ 45
- 15
searx/__init__.py Voir le fichier

@@ -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:

+ 2
- 3
searx/doi.yml Voir le fichier

@@ -1,6 +1,5 @@
1
-doi_resolvers :
1
+resolvers :
2 2
   oadoi.org : 'https://oadoi.org/'
3 3
   doi.org : 'https://doi.org/'
4 4
   doai.io  : 'http://doai.io/'
5
-
6
-default_doi_resolver : 'oadoi.org'
5
+default: 'oadoi.org'

+ 593
- 594
searx/engines.yml
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 31
- 32
searx/locales.yml Voir le fichier

@@ -1,32 +1,31 @@
1
-locales:
2
-    en : English
3
-    ar : العَرَبِيَّة (Arabic)
4
-    bg : Български (Bulgarian)
5
-    cs : Čeština (Czech)
6
-    da : Dansk (Danish)
7
-    de : Deutsch (German)
8
-    el_GR : Ελληνικά (Greek_Greece)
9
-    eo : Esperanto (Esperanto)
10
-    es : Español (Spanish)
11
-    fi : Suomi (Finnish)
12
-    fil : Wikang Filipino (Filipino)
13
-    fr : Français (French)
14
-    he : עברית (Hebrew)
15
-    hr : Hrvatski (Croatian)
16
-    hu : Magyar (Hungarian)
17
-    it : Italiano (Italian)
18
-    ja : 日本語 (Japanese)
19
-    nl : Nederlands (Dutch)
20
-    pl : Polszczyzna (Polish)
21
-    pt : Português (Portuguese)
22
-    pt_BR : Português (Portuguese_Brazil)
23
-    ro : Română (Romanian)
24
-    ru : Русский (Russian)
25
-    sk : Slovenčina (Slovak)
26
-    sl : Slovenski (Slovene)
27
-    sr : српски (Serbian)
28
-    sv : Svenska (Swedish)
29
-    tr : Türkçe (Turkish)
30
-    uk : українська мова (Ukrainian)
31
-    zh : 中文 (Chinese)
32
-    zh_TW : 國語 (Taiwanese Mandarin)
1
+  en : English
2
+  ar : العَرَبِيَّة (Arabic)
3
+  bg : Български (Bulgarian)
4
+  cs : Čeština (Czech)
5
+  da : Dansk (Danish)
6
+  de : Deutsch (German)
7
+  el_GR : Ελληνικά (Greek_Greece)
8
+  eo : Esperanto (Esperanto)
9
+  es : Español (Spanish)
10
+  fi : Suomi (Finnish)
11
+  fil : Wikang Filipino (Filipino)
12
+  fr : Français (French)
13
+  he : עברית (Hebrew)
14
+  hr : Hrvatski (Croatian)
15
+  hu : Magyar (Hungarian)
16
+  it : Italiano (Italian)
17
+  ja : 日本語 (Japanese)
18
+  nl : Nederlands (Dutch)
19
+  pl : Polszczyzna (Polish)
20
+  pt : Português (Portuguese)
21
+  pt_BR : Português (Portuguese_Brazil)
22
+  ro : Română (Romanian)
23
+  ru : Русский (Russian)
24
+  sk : Slovenčina (Slovak)
25
+  sl : Slovenski (Slovene)
26
+  sr : српски (Serbian)
27
+  sv : Svenska (Swedish)
28
+  tr : Türkçe (Turkish)
29
+  uk : українська мова (Ukrainian)
30
+  zh : 中文 (Chinese)
31
+  zh_TW : 國語 (Taiwanese Mandarin)

+ 6
- 5
searx/plugins/oa_doi_rewrite.py Voir le fichier

@@ -11,7 +11,7 @@ description = gettext('Avoid paywalls by redirecting to open-access versions of
11 11
 default_on = False
12 12
 preference_section = 'privacy'
13 13
 
14
-doi_resolvers = settings['doi_resolvers']
14
+doi_resolvers = settings['doi']['resolvers']
15 15
 
16 16
 
17 17
 def extract_doi(url):
@@ -26,19 +26,20 @@ def extract_doi(url):
26 26
 
27 27
 
28 28
 def get_doi_resolver(args, preference_doi_resolver):
29
-    doi_resolvers = settings['doi_resolvers']
29
+    doi_resolvers = settings['doi']['resolvers']
30 30
     doi_resolver = args.get('doi_resolver', preference_doi_resolver)[0]
31 31
     if doi_resolver not in doi_resolvers:
32
-        doi_resolvers = settings['default_doi_resolver']
32
+        doi_resolver = settings['doi']['default']
33 33
     return doi_resolver
34 34
 
35 35
 
36 36
 def on_result(request, search, result):
37 37
     doi = extract_doi(result['parsed_url'])
38 38
     if doi and len(doi) < 50:
39
-        for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'):
39
+        for suffix in ('/', '.pdf', '.full', '/pdf', '/full', '/meta', '/abstract'):
40 40
             if doi.endswith(suffix):
41 41
                 doi = doi[:-len(suffix)]
42
-        result['url'] = get_doi_resolver(request.args, request.preferences.get_value('doi_resolver')) + doi
42
+        base_url = doi_resolvers[get_doi_resolver(request.args, request.preferences.get_value('doi_resolver'))]
43
+        result['url'] = base_url + doi
43 44
         result['parsed_url'] = urlparse(result['url'])
44 45
     return True

+ 1
- 1
searx/preferences.py Voir le fichier

@@ -14,7 +14,7 @@ COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5  # 5 years
14 14
 LANGUAGE_CODES = [l[0] for l in languages]
15 15
 DISABLED = 0
16 16
 ENABLED = 1
17
-DOI_RESOLVERS = list(settings['doi_resolvers'])
17
+DOI_RESOLVERS = list(settings['doi']['resolvers'])
18 18
 
19 19
 
20 20
 class MissingArgumentException(Exception):

+ 3
- 1
searx/settings.yml Voir le fichier

@@ -6,7 +6,9 @@ search:
6 6
     safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict
7 7
     autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default
8 8
     language : "en-US"
9
-    engines:
9
+    engines_path: "" # engines.yml path - leave it blank if you don't change
10
+    include_engines_path: "" # user defined engines file path - leave it blank if you don't change
11
+    engines: # enabled engines with additional settings (timeout for example)
10 12
       - name: arch linux wiki
11 13
       #- name: archive is
12 14
       - name: arxiv

+ 3
- 3
searx/webapp.py Voir le fichier

@@ -716,7 +716,7 @@ def preferences():
716 716
                   shortcuts={y: x for x, y in engine_shortcuts.items()},
717 717
                   themes=themes,
718 718
                   plugins=plugins,
719
-                  doi_resolvers=settings['doi_resolvers'],
719
+                  doi_resolvers=settings['doi']['resolvers'],
720 720
                   current_doi_resolver=get_doi_resolver(request.args, request.preferences.get_value('doi_resolver')),
721 721
                   allowed_plugins=allowed_plugins,
722 722
                   theme=get_current_theme_name(),
@@ -863,8 +863,8 @@ def config():
863 863
                     'safe_search': settings['search']['safe_search'],
864 864
                     'default_theme': settings['ui']['default_theme'],
865 865
                     'version': VERSION_STRING,
866
-                    'doi_resolvers': [r for r in settings['doi_resolvers']],
867
-                    'default_doi_resolver': settings['default_doi_resolver'],
866
+                    'doi_resolvers': [r for r in settings['doi']['resolvers']],
867
+                    'default_doi_resolver': settings['doi']['default'],
868 868
                     })
869 869
 
870 870