.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
 
 deprecated
 
 cfg
 

Static Public Attributes

 UNSET = UNSET
 

Protected Member Functions

 _get_parent_dict (self, name)
 

Detailed Description

Base class used for configuration

Definition at line 64 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 93 of file config.py.

93 def __init__(self, cfg_schema: typing.Dict, deprecated: typing.Dict[str, str]):
94 """Construtor of class Config.
95
96 :param cfg_schema: Schema of the configuration
97 :param deprecated: dictionary that maps deprecated configuration names to a messages
98
99 These values are needed for validation, see :py:obj:`validate`.
100
101 """
102 self.cfg_schema = cfg_schema
103 self.deprecated = deprecated
104 self.cfg = copy.deepcopy(cfg_schema)
105

Member Function Documentation

◆ __getitem__()

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

Definition at line 106 of file config.py.

106 def __getitem__(self, key: str) -> Any:
107 return self.get(key)
108

References searx.metrics.models.HistogramStorage.get(), searx.metrics.models.CounterStorage.get(), searx.botdetection.config.Config.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 151 of file config.py.

151 def _get_parent_dict(self, name):
152 parent_name = '.'.join(name.split('.')[:-1])
153 if parent_name:
154 parent = value(parent_name, self.cfg)
155 else:
156 parent = self.cfg
157 if (parent is UNSET) or (not isinstance(parent, dict)):
158 raise KeyError(parent_name)
159 return parent
160

References searx.botdetection.config.Config.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 120 of file config.py.

120 def default(self, name: str):
121 """Returns default value of field ``name`` in ``self.cfg_schema``."""
122 return value(name, self.cfg_schema)
123

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

Referenced by searx.settings_defaults.SettingsValue.__call__(), and searx.settings_defaults.SettingsDirectoryValue.__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 70 of file config.py.

70 def from_toml(cls, schema_file: pathlib.Path, cfg_file: pathlib.Path, deprecated: dict) -> Config:
71
72 # init schema
73
74 log.debug("load schema file: %s", schema_file)
75 cfg = cls(cfg_schema=toml_load(schema_file), deprecated=deprecated)
76 if not cfg_file.exists():
77 log.warning("missing config file: %s", cfg_file)
78 return cfg
79
80 # load configuration
81
82 log.debug("load config file: %s", cfg_file)
83 upd_cfg = toml_load(cfg_file)
84
85 is_valid, issue_list = cfg.validate(upd_cfg)
86 for msg in issue_list:
87 log.error(str(msg))
88 if not is_valid:
89 raise TypeError(f"schema of {cfg_file} is invalid!")
90 cfg.update(upd_cfg)
91 return cfg
92

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 124 of file config.py.

124 def get(self, name: str, default: Any = UNSET, replace: bool = True) -> Any:
125 """Returns the value to which ``name`` points in the configuration.
126
127 If there is no such ``name`` in the config and the ``default`` is
128 :py:obj:`UNSET`, a :py:obj:`KeyError` is raised.
129 """
130
131 parent = self._get_parent_dict(name)
132 val = parent.get(name.split('.')[-1], UNSET)
133 if val is UNSET:
134 if default is UNSET:
135 raise KeyError(name)
136 val = default
137
138 if replace and isinstance(val, str):
139 val = val % self
140 return val
141

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 161 of file config.py.

161 def path(self, name: str, default=UNSET):
162 """Get a :py:class:`pathlib.Path` object from a config string."""
163
164 val = self.get(name, default)
165 if val is UNSET:
166 if default is UNSET:
167 raise KeyError(name)
168 return default
169 return pathlib.Path(str(val))
170

References searx.metrics.models.HistogramStorage.get(), searx.metrics.models.CounterStorage.get(), searx.botdetection.config.Config.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 171 of file config.py.

171 def pyobj(self, name, default=UNSET):
172 """Get python object refered by full qualiffied name (FQN) in the config
173 string."""
174
175 fqn = self.get(name, default)
176 if fqn is UNSET:
177 if default is UNSET:
178 raise KeyError(name)
179 return default
180 (modulename, name) = str(fqn).rsplit('.', 1)
181 m = __import__(modulename, {}, {}, [name], 0)
182 return getattr(m, name)
183
184

References searx.metrics.models.HistogramStorage.get(), searx.metrics.models.CounterStorage.get(), searx.botdetection.config.Config.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 142 of file config.py.

142 def set(self, name: str, val):
143 """Set the value to which ``name`` points in the configuration.
144
145 If there is no such ``name`` in the config, a :py:obj:`KeyError` is
146 raised.
147 """
148 parent = self._get_parent_dict(name)
149 parent[name.split('.')[-1]] = val
150

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 115 of file config.py.

115 def update(self, upd_cfg: dict):
116 """Update this configuration by ``upd_cfg``."""
117
118 dict_deepupdate(self.cfg, upd_cfg)
119

References searx.botdetection.config.Config.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 109 of file config.py.

109 def validate(self, cfg: dict):
110 """Validation of dictionary ``cfg`` on :py:obj:`Config.SCHEMA`.
111 Validation is done by :py:obj:`validate`."""
112
113 return validate(self.cfg_schema, cfg, self.deprecated)
114

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

searx.botdetection.config.Config.cfg

◆ cfg_schema

searx.botdetection.config.Config.cfg_schema

◆ deprecated

searx.botdetection.config.Config.deprecated

Definition at line 103 of file config.py.

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

◆ UNSET

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

Definition at line 67 of file config.py.


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