.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.botdetection.config.Config Class Reference

Public Member Functions

Config from_toml (cls, pathlib.Path schema_file, pathlib.Path cfg_file, dict deprecated)
 
 __init__ (self, typing.Dict cfg_schema, typing.Dict[str, str] deprecated)
 
Any __getitem__ (self, str key)
 
 validate (self, dict cfg)
 
 update (self, dict upd_cfg)
 
 default (self, str name)
 
Any get (self, str name, Any default=UNSET, bool replace=True)
 
 set (self, str name, val)
 
 path (self, str name, default=UNSET)
 
 pyobj (self, name, default=UNSET)
 

Public Attributes

 cfg_schema = cfg_schema
 
 deprecated = deprecated
 
 cfg = copy.deepcopy(cfg_schema)
 

Static Public Attributes

 UNSET = UNSET
 

Protected Member Functions

 _get_parent_dict (self, name)
 

Detailed Description

Base class used for configuration

Definition at line 54 of file config.py.

Constructor & Destructor Documentation

◆ __init__()

searx.botdetection.config.Config.__init__ ( self,
typing.Dict cfg_schema,
typing.Dict[str, str] deprecated )
Construtor of class Config.

:param cfg_schema: Schema of the configuration
:param deprecated: dictionary that maps deprecated configuration names to a messages

These values are needed for validation, see :py:obj:`validate`.

Definition at line 83 of file config.py.

83 def __init__(self, cfg_schema: typing.Dict, deprecated: typing.Dict[str, str]):
84 """Construtor of class Config.
85
86 :param cfg_schema: Schema of the configuration
87 :param deprecated: dictionary that maps deprecated configuration names to a messages
88
89 These values are needed for validation, see :py:obj:`validate`.
90
91 """
92 self.cfg_schema = cfg_schema
93 self.deprecated = deprecated
94 self.cfg = copy.deepcopy(cfg_schema)
95

Member Function Documentation

◆ __getitem__()

Any searx.botdetection.config.Config.__getitem__ ( self,
str key )

Definition at line 96 of file config.py.

96 def __getitem__(self, key: str) -> Any:
97 return self.get(key)
98

References searx.botdetection.config.Config.get(), searx.metrics.models.CounterStorage.get(), searx.metrics.models.HistogramStorage.get(), and searx.network.Request.get().

+ Here is the call graph for this function:

◆ _get_parent_dict()

searx.botdetection.config.Config._get_parent_dict ( self,
name )
protected

Definition at line 141 of file config.py.

141 def _get_parent_dict(self, name):
142 parent_name = '.'.join(name.split('.')[:-1])
143 if parent_name:
144 parent = value(parent_name, self.cfg)
145 else:
146 parent = self.cfg
147 if (parent is UNSET) or (not isinstance(parent, dict)):
148 raise KeyError(parent_name)
149 return parent
150

References searx.botdetection.config.Config.cfg, searx.favicons.cache.FaviconCacheMEM.cfg, searx.favicons.cache.FaviconCacheSQLite.cfg, and searx.botdetection.config.value().

Referenced by searx.botdetection.config.Config.get(), and searx.botdetection.config.Config.set().

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

◆ default()

searx.botdetection.config.Config.default ( self,
str name )
Returns default value of field ``name`` in ``self.cfg_schema``.

Definition at line 110 of file config.py.

110 def default(self, name: str):
111 """Returns default value of field ``name`` in ``self.cfg_schema``."""
112 return value(name, self.cfg_schema)
113

References searx.botdetection.config.Config.cfg_schema, and searx.botdetection.config.value().

Referenced by searx.settings_defaults.SettingsDirectoryValue.__call__(), and searx.settings_defaults.SettingsValue.__call__().

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

◆ from_toml()

Config searx.botdetection.config.Config.from_toml ( cls,
pathlib.Path schema_file,
pathlib.Path cfg_file,
dict deprecated )

Definition at line 60 of file config.py.

60 def from_toml(cls, schema_file: pathlib.Path, cfg_file: pathlib.Path, deprecated: dict) -> Config:
61
62 # init schema
63
64 log.debug("load schema file: %s", schema_file)
65 cfg = cls(cfg_schema=toml_load(schema_file), deprecated=deprecated)
66 if not cfg_file.exists():
67 log.warning("missing config file: %s", cfg_file)
68 return cfg
69
70 # load configuration
71
72 log.debug("load config file: %s", cfg_file)
73 upd_cfg = toml_load(cfg_file)
74
75 is_valid, issue_list = cfg.validate(upd_cfg)
76 for msg in issue_list:
77 log.error(str(msg))
78 if not is_valid:
79 raise TypeError(f"schema of {cfg_file} is invalid!")
80 cfg.update(upd_cfg)
81 return cfg
82

References searx.botdetection.config.toml_load().

+ Here is the call graph for this function:

◆ get()

Any searx.botdetection.config.Config.get ( self,
str name,
Any default = UNSET,
bool replace = True )
Returns the value to which ``name`` points in the configuration.

If there is no such ``name`` in the config and the ``default`` is
:py:obj:`UNSET`, a :py:obj:`KeyError` is raised.

Definition at line 114 of file config.py.

114 def get(self, name: str, default: Any = UNSET, replace: bool = True) -> Any:
115 """Returns the value to which ``name`` points in the configuration.
116
117 If there is no such ``name`` in the config and the ``default`` is
118 :py:obj:`UNSET`, a :py:obj:`KeyError` is raised.
119 """
120
121 parent = self._get_parent_dict(name)
122 val = parent.get(name.split('.')[-1], UNSET)
123 if val is UNSET:
124 if default is UNSET:
125 raise KeyError(name)
126 val = default
127
128 if replace and isinstance(val, str):
129 val = val % self
130 return val
131

References searx.botdetection.config.Config._get_parent_dict().

Referenced by searx.botdetection.config.Config.__getitem__(), searx.botdetection.config.Config.path(), and searx.botdetection.config.Config.pyobj().

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

◆ path()

searx.botdetection.config.Config.path ( self,
str name,
default = UNSET )
Get a :py:class:`pathlib.Path` object from a config string.

Definition at line 151 of file config.py.

151 def path(self, name: str, default=UNSET):
152 """Get a :py:class:`pathlib.Path` object from a config string."""
153
154 val = self.get(name, default)
155 if val is UNSET:
156 if default is UNSET:
157 raise KeyError(name)
158 return default
159 return pathlib.Path(str(val))
160

References searx.botdetection.config.Config.get(), searx.metrics.models.CounterStorage.get(), searx.metrics.models.HistogramStorage.get(), and searx.network.Request.get().

+ Here is the call graph for this function:

◆ pyobj()

searx.botdetection.config.Config.pyobj ( self,
name,
default = UNSET )
Get python object refered by full qualiffied name (FQN) in the config
string.

Definition at line 161 of file config.py.

161 def pyobj(self, name, default=UNSET):
162 """Get python object refered by full qualiffied name (FQN) in the config
163 string."""
164
165 fqn = self.get(name, default)
166 if fqn is UNSET:
167 if default is UNSET:
168 raise KeyError(name)
169 return default
170 (modulename, name) = str(fqn).rsplit('.', 1)
171 m = __import__(modulename, {}, {}, [name], 0)
172 return getattr(m, name)
173
174

References searx.botdetection.config.Config.get(), searx.metrics.models.CounterStorage.get(), searx.metrics.models.HistogramStorage.get(), and searx.network.Request.get().

+ Here is the call graph for this function:

◆ set()

searx.botdetection.config.Config.set ( self,
str name,
val )
Set the value to which ``name`` points in the configuration.

If there is no such ``name`` in the config, a :py:obj:`KeyError` is
raised.

Definition at line 132 of file config.py.

132 def set(self, name: str, val):
133 """Set the value to which ``name`` points in the configuration.
134
135 If there is no such ``name`` in the config, a :py:obj:`KeyError` is
136 raised.
137 """
138 parent = self._get_parent_dict(name)
139 parent[name.split('.')[-1]] = val
140

References searx.botdetection.config.Config._get_parent_dict().

+ Here is the call graph for this function:

◆ update()

searx.botdetection.config.Config.update ( self,
dict upd_cfg )
Update this configuration by ``upd_cfg``.

Definition at line 105 of file config.py.

105 def update(self, upd_cfg: dict):
106 """Update this configuration by ``upd_cfg``."""
107
108 dict_deepupdate(self.cfg, upd_cfg)
109

References searx.botdetection.config.Config.cfg, searx.favicons.cache.FaviconCacheMEM.cfg, searx.favicons.cache.FaviconCacheSQLite.cfg, and searx.botdetection.config.dict_deepupdate().

+ Here is the call graph for this function:

◆ validate()

searx.botdetection.config.Config.validate ( self,
dict cfg )
Validation of dictionary ``cfg`` on :py:obj:`Config.SCHEMA`.
Validation is done by :py:obj:`validate`.

Definition at line 99 of file config.py.

99 def validate(self, cfg: dict):
100 """Validation of dictionary ``cfg`` on :py:obj:`Config.SCHEMA`.
101 Validation is done by :py:obj:`validate`."""
102
103 return validate(self.cfg_schema, cfg, self.deprecated)
104

References searx.botdetection.config.Config.cfg_schema, searx.botdetection.config.Config.deprecated, and searx.botdetection.config.Config.validate().

Referenced by searx.botdetection.config.Config.validate().

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

Member Data Documentation

◆ cfg

◆ cfg_schema

searx.botdetection.config.Config.cfg_schema = cfg_schema

◆ deprecated

searx.botdetection.config.Config.deprecated = deprecated

Definition at line 93 of file config.py.

Referenced by searx.botdetection.config.Config.validate().

◆ UNSET

searx.botdetection.config.Config.UNSET = UNSET
static

Definition at line 57 of file config.py.


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