.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.preferences.Preferences Class Reference

Public Member Functions

 __init__ (self, List[str] themes, List[str] categories, Dict[str, Engine] engines, Iterable[Plugin] plugins, Optional[ClientPref] client=None)
 
 get_as_url_params (self)
 
 parse_encoded_data (self, str input_data)
 
 parse_dict (self, Dict[str, str] input_data)
 
 parse_form (self, Dict[str, str] input_data)
 
 get_value (self, str user_setting_name)
 
 save (self, flask.Response resp)
 
 validate_token (self, engine)
 

Public Attributes

dict key_value_settings
 
 engines = EnginesSetting('engines', engines=engines.values())
 
 plugins = PluginsSetting('plugins', plugins=plugins)
 
 tokens = SetSetting('tokens')
 
 client = client or ClientPref()
 
dict unknown_params = {}
 
str key_value_settings = 'disabled_engines':
 

Detailed Description

Validates and saves preferences to cookies

Definition at line 373 of file preferences.py.

Constructor & Destructor Documentation

◆ __init__()

searx.preferences.Preferences.__init__ ( self,
List[str] themes,
List[str] categories,
Dict[str, Engine] engines,
Iterable[Plugin] plugins,
Optional[ClientPref] client = None )

Definition at line 376 of file preferences.py.

383 ):
384
385 super().__init__()
386
387 self.key_value_settings: Dict[str, Setting] = {
388 # fmt: off
389 'categories': MultipleChoiceSetting(
390 ['general'],
391 locked=is_locked('categories'),
392 choices=categories + ['none']
393 ),
394 'language': SearchLanguageSetting(
395 settings['search']['default_lang'],
396 locked=is_locked('language'),
397 choices=settings['search']['languages'] + ['']
398 ),
399 'locale': EnumStringSetting(
400 settings['ui']['default_locale'],
401 locked=is_locked('locale'),
402 choices=list(LOCALE_NAMES.keys()) + ['']
403 ),
404 'autocomplete': EnumStringSetting(
405 settings['search']['autocomplete'],
406 locked=is_locked('autocomplete'),
407 choices=list(autocomplete.backends.keys()) + ['']
408 ),
409 'favicon_resolver': EnumStringSetting(
410 settings['search']['favicon_resolver'],
411 locked=is_locked('favicon_resolver'),
412 choices=list(favicons.proxy.CFG.resolver_map.keys()) + ['']
413 ),
414 'image_proxy': BooleanSetting(
415 settings['server']['image_proxy'],
416 locked=is_locked('image_proxy')
417 ),
418 'method': EnumStringSetting(
419 settings['server']['method'],
420 locked=is_locked('method'),
421 choices=('GET', 'POST')
422 ),
423 'safesearch': MapSetting(
424 settings['search']['safe_search'],
425 locked=is_locked('safesearch'),
426 map={
427 '0': 0,
428 '1': 1,
429 '2': 2
430 }
431 ),
432 'theme': EnumStringSetting(
433 settings['ui']['default_theme'],
434 locked=is_locked('theme'),
435 choices=themes
436 ),
437 'results_on_new_tab': BooleanSetting(
438 settings['ui']['results_on_new_tab'],
439 locked=is_locked('results_on_new_tab')
440 ),
441 'doi_resolver': MultipleChoiceSetting(
442 [settings['default_doi_resolver'], ],
443 locked=is_locked('doi_resolver'),
444 choices=DOI_RESOLVERS
445 ),
446 'simple_style': EnumStringSetting(
447 settings['ui']['theme_args']['simple_style'],
448 locked=is_locked('simple_style'),
449 choices=['', 'auto', 'light', 'dark', 'black']
450 ),
451 'center_alignment': BooleanSetting(
452 settings['ui']['center_alignment'],
453 locked=is_locked('center_alignment')
454 ),
455 'advanced_search': BooleanSetting(
456 settings['ui']['advanced_search'],
457 locked=is_locked('advanced_search')
458 ),
459 'query_in_title': BooleanSetting(
460 settings['ui']['query_in_title'],
461 locked=is_locked('query_in_title')
462 ),
463 'infinite_scroll': BooleanSetting(
464 settings['ui']['infinite_scroll'],
465 locked=is_locked('infinite_scroll')
466 ),
467 'search_on_category_select': BooleanSetting(
468 settings['ui']['search_on_category_select'],
469 locked=is_locked('search_on_category_select')
470 ),
471 'hotkeys': EnumStringSetting(
472 settings['ui']['hotkeys'],
473 choices=['default', 'vim']
474 ),
475 # fmt: on
476 }
477
478 self.engines = EnginesSetting('engines', engines=engines.values())
479 self.plugins = PluginsSetting('plugins', plugins=plugins)
480 self.tokens = SetSetting('tokens')
481 self.client = client or ClientPref()
482 self.unknown_params: Dict[str, str] = {}
483

References searx.preferences.Preferences.__init__().

Referenced by searx.preferences.Preferences.__init__().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Function Documentation

◆ get_as_url_params()

searx.preferences.Preferences.get_as_url_params ( self)
Return preferences as URL parameters

Definition at line 484 of file preferences.py.

484 def get_as_url_params(self):
485 """Return preferences as URL parameters"""
486 settings_kv = {}
487 for k, v in self.key_value_settings.items():
488 if v.locked:
489 continue
490 if isinstance(v, MultipleChoiceSetting):
491 settings_kv[k] = ','.join(v.get_value())
492 else:
493 settings_kv[k] = v.get_value()
494
495 settings_kv['disabled_engines'] = ','.join(self.engines.disabled)
496 settings_kv['enabled_engines'] = ','.join(self.engines.enabled)
497
498 settings_kv['disabled_plugins'] = ','.join(self.plugins.disabled)
499 settings_kv['enabled_plugins'] = ','.join(self.plugins.enabled)
500
501 settings_kv['tokens'] = ','.join(self.tokens.values)
502
503 return urlsafe_b64encode(compress(urlencode(settings_kv).encode())).decode()
504

References searx.preferences.Preferences.engines, searx.search.checker.background.CheckerOk.engines, searx.preferences.Preferences.key_value_settings, searx.plugins.PluginStore.plugins, searx.preferences.Preferences.plugins, searx.enginelib.Engine.tokens, and searx.preferences.Preferences.tokens.

◆ get_value()

searx.preferences.Preferences.get_value ( self,
str user_setting_name )
Returns the value for ``user_setting_name``

Definition at line 561 of file preferences.py.

561 def get_value(self, user_setting_name: str):
562 """Returns the value for ``user_setting_name``"""
563 ret_val = None
564 if user_setting_name in self.key_value_settings:
565 ret_val = self.key_value_settings[user_setting_name].get_value()
566 if user_setting_name in self.unknown_params:
567 ret_val = self.unknown_params[user_setting_name]
568 return ret_val
569

References searx.preferences.Preferences.get_value(), searx.preferences.Preferences.key_value_settings, and searx.preferences.Preferences.unknown_params.

Referenced by searx.preferences.Preferences.get_value().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_dict()

searx.preferences.Preferences.parse_dict ( self,
Dict[str, str] input_data )
parse preferences from request (``flask.request.form``)

Definition at line 513 of file preferences.py.

513 def parse_dict(self, input_data: Dict[str, str]):
514 """parse preferences from request (``flask.request.form``)"""
515 for user_setting_name, user_setting in input_data.items():
516 if user_setting_name in self.key_value_settings:
517 if self.key_value_settings[user_setting_name].locked:
518 continue
519 self.key_value_settings[user_setting_name].parse(user_setting)
520 elif user_setting_name == 'disabled_engines':
521 self.engines.parse_cookie(input_data.get('disabled_engines', ''), input_data.get('enabled_engines', ''))
522 elif user_setting_name == 'disabled_plugins':
523 self.plugins.parse_cookie(input_data.get('disabled_plugins', ''), input_data.get('enabled_plugins', ''))
524 elif user_setting_name == 'tokens':
525 self.tokens.parse(user_setting)
526 elif not any(
527 user_setting_name.startswith(x) for x in ['enabled_', 'disabled_', 'engine_', 'category_', 'plugin_']
528 ):
529 self.unknown_params[user_setting_name] = user_setting
530

Referenced by searx.preferences.Preferences.parse_encoded_data().

+ Here is the caller graph for this function:

◆ parse_encoded_data()

searx.preferences.Preferences.parse_encoded_data ( self,
str input_data )
parse (base64) preferences from request (``flask.request.form['preferences']``)

Definition at line 505 of file preferences.py.

505 def parse_encoded_data(self, input_data: str):
506 """parse (base64) preferences from request (``flask.request.form['preferences']``)"""
507 bin_data = decompress(urlsafe_b64decode(input_data))
508 dict_data = {}
509 for x, y in parse_qs(bin_data.decode('ascii'), keep_blank_values=True).items():
510 dict_data[x] = y[0]
511 self.parse_dict(dict_data)
512

References searx.preferences.Preferences.parse_dict().

+ Here is the call graph for this function:

◆ parse_form()

searx.preferences.Preferences.parse_form ( self,
Dict[str, str] input_data )
Parse formular (``<input>``) data from a ``flask.request.form``

Definition at line 531 of file preferences.py.

531 def parse_form(self, input_data: Dict[str, str]):
532 """Parse formular (``<input>``) data from a ``flask.request.form``"""
533 disabled_engines = []
534 enabled_categories = []
535 disabled_plugins = []
536
537 # boolean preferences are not sent by the form if they're false,
538 # so we have to add them as false manually if they're not sent (then they would be true)
539 for key, setting in self.key_value_settings.items():
540 if key not in input_data.keys() and isinstance(setting, BooleanSetting):
541 input_data[key] = 'False'
542
543 for user_setting_name, user_setting in input_data.items():
544 if user_setting_name in self.key_value_settings:
545 self.key_value_settings[user_setting_name].parse(user_setting)
546 elif user_setting_name.startswith('engine_'):
547 disabled_engines.append(user_setting_name)
548 elif user_setting_name.startswith('category_'):
549 enabled_categories.append(user_setting_name[len('category_') :])
550 elif user_setting_name.startswith('plugin_'):
551 disabled_plugins.append(user_setting_name)
552 elif user_setting_name == 'tokens':
553 self.tokens.parse_form(user_setting)
554 else:
555 self.unknown_params[user_setting_name] = user_setting
556 self.key_value_settings['categories'].parse_form(enabled_categories)
557 self.engines.parse_form(disabled_engines)
558 self.plugins.parse_form(disabled_plugins)
559

References searx.preferences.Preferences.engines, searx.search.checker.background.CheckerOk.engines, searx.preferences.Preferences.key_value_settings, searx.preferences.Preferences.parse_form(), searx.plugins.PluginStore.plugins, searx.preferences.Preferences.plugins, searx.enginelib.Engine.tokens, searx.preferences.Preferences.tokens, and searx.preferences.Preferences.unknown_params.

Referenced by searx.preferences.Preferences.parse_form().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ save()

searx.preferences.Preferences.save ( self,
flask.Response resp )
Save cookie in the HTTP response object

Definition at line 570 of file preferences.py.

570 def save(self, resp: flask.Response):
571 """Save cookie in the HTTP response object"""
572 for user_setting_name, user_setting in self.key_value_settings.items():
573 # pylint: disable=unnecessary-dict-index-lookup
574 if self.key_value_settings[user_setting_name].locked:
575 continue
576 user_setting.save(user_setting_name, resp)
577 self.engines.save(resp)
578 self.plugins.save(resp)
579 self.tokens.save('tokens', resp)
580 for k, v in self.unknown_params.items():
581 resp.set_cookie(k, v, max_age=COOKIE_MAX_AGE)
582 return resp
583

References searx.preferences.Preferences.engines, searx.search.checker.background.CheckerOk.engines, searx.preferences.Preferences.key_value_settings, searx.plugins.PluginStore.plugins, searx.preferences.Preferences.plugins, searx.preferences.Preferences.save(), searx.enginelib.Engine.tokens, searx.preferences.Preferences.tokens, and searx.preferences.Preferences.unknown_params.

Referenced by searx.preferences.Preferences.save().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validate_token()

searx.preferences.Preferences.validate_token ( self,
engine )

Definition at line 584 of file preferences.py.

584 def validate_token(self, engine):
585 valid = True
586 if hasattr(engine, 'tokens') and engine.tokens:
587 valid = False
588 for token in self.tokens.values:
589 if token in engine.tokens:
590 valid = True
591 break
592
593 return valid
594
595

References searx.enginelib.Engine.tokens, and searx.preferences.Preferences.tokens.

Member Data Documentation

◆ client

searx.preferences.Preferences.client = client or ClientPref()

Definition at line 481 of file preferences.py.

◆ engines

searx.preferences.Preferences.engines = EnginesSetting('engines', engines=engines.values())

◆ key_value_settings [1/2]

◆ key_value_settings [2/2]

str searx.preferences.Preferences.key_value_settings = 'disabled_engines':

◆ plugins

◆ tokens

◆ unknown_params

dict searx.preferences.Preferences.unknown_params = {}

The documentation for this class was generated from the following file: