.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()
 
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 'url_formatting': EnumStringSetting(
476 settings['ui']['url_formatting'],
477 choices=['pretty', 'full', 'host']
478 ),
479 # fmt: on
480 }
481
482 self.engines = EnginesSetting('engines', engines=engines.values())
483 self.plugins = PluginsSetting('plugins', plugins=plugins)
484 self.tokens = SetSetting('tokens')
485 self.client = client or ClientPref()
486

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 487 of file preferences.py.

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

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 559 of file preferences.py.

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

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

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 516 of file preferences.py.

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

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 508 of file preferences.py.

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

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 530 of file preferences.py.

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

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, and searx.preferences.Preferences.tokens.

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 566 of file preferences.py.

566 def save(self, resp: flask.Response):
567 """Save cookie in the HTTP response object"""
568 for user_setting_name, user_setting in self.key_value_settings.items():
569 # pylint: disable=unnecessary-dict-index-lookup
570 if self.key_value_settings[user_setting_name].locked:
571 continue
572 user_setting.save(user_setting_name, resp)
573 self.engines.save(resp)
574 self.plugins.save(resp)
575 self.tokens.save('tokens', resp)
576 return resp
577

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, and searx.preferences.Preferences.tokens.

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 578 of file preferences.py.

578 def validate_token(self, engine):
579 valid = True
580 if hasattr(engine, 'tokens') and engine.tokens:
581 valid = False
582 for token in self.tokens.values:
583 if token in engine.tokens:
584 valid = True
585 break
586
587 return valid
588
589

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

Member Data Documentation

◆ client

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

Definition at line 485 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


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