.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[str, str] deprecated)
 __init__ (self, dict[str, typing.Any] cfg_schema, dict[str, str] deprecated)
typing.Any __getitem__ (self, str key)
 validate (self, dict[str, typing.Any] cfg)
 update (self, dict[str, typing.Any] upd_cfg)
 default (self, str name)
typing.Any get (self, str name, typing.Any default=UNSET, bool replace=True)
 set (self, str name, typing.Any val)
 path (self, str name, typing.Any default=UNSET)
 pyobj (self, str name, typing.Any default=UNSET)

Public Attributes

dict[str, typing.Any] cfg_schema = cfg_schema
dict[str, str] deprecated = deprecated
dict[str, typing.Any] cfg = copy.deepcopy(cfg_schema)

Static Public Attributes

object UNSET = UNSET

Protected Member Functions

dict[str, typing.Any] _get_parent_dict (self, str name)

Detailed Description

Base class used for configuration

Definition at line 69 of file config.py.

Constructor & Destructor Documentation

◆ __init__()

searx.botdetection.config.Config.__init__ ( self,
dict[str, typing.Any] cfg_schema,
dict[str, str] deprecated )
Constructor 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 98 of file config.py.

98 def __init__(self, cfg_schema: dict[str, typing.Any], deprecated: dict[str, str]):
99 """Constructor of class Config.
100
101 :param cfg_schema: Schema of the configuration
102 :param deprecated: dictionary that maps deprecated configuration names to a messages
103
104 These values are needed for validation, see :py:obj:`validate`.
105
106 """
107 self.cfg_schema: dict[str, typing.Any] = cfg_schema
108 self.deprecated: dict[str, str] = deprecated
109 self.cfg: dict[str, typing.Any] = copy.deepcopy(cfg_schema)
110

Member Function Documentation

◆ __getitem__()

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

Definition at line 111 of file config.py.

111 def __getitem__(self, key: str) -> typing.Any:
112 return self.get(key)
113

References get().

Here is the call graph for this function:

◆ _get_parent_dict()

dict[str, typing.Any] searx.botdetection.config.Config._get_parent_dict ( self,
str name )
protected

Definition at line 156 of file config.py.

156 def _get_parent_dict(self, name: str) -> dict[str, typing.Any]:
157 parent_name = '.'.join(name.split('.')[:-1])
158 if parent_name:
159 parent: dict[str, typing.Any] = value(parent_name, self.cfg)
160 else:
161 parent = self.cfg
162 if (parent is UNSET) or (not isinstance(parent, dict)):
163 raise KeyError(parent_name)
164 return parent
165

References cfg, and searx.botdetection.config.value().

Referenced by get(), and 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 125 of file config.py.

125 def default(self, name: str):
126 """Returns default value of field ``name`` in ``self.cfg_schema``."""
127 return value(name, self.cfg_schema)
128

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

Here is the call graph for this function:

◆ from_toml()

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

Definition at line 75 of file config.py.

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

References searx.botdetection.config.toml_load().

Here is the call graph for this function:

◆ get()

typing.Any searx.botdetection.config.Config.get ( self,
str name,
typing.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 129 of file config.py.

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

References _get_parent_dict().

Referenced by __getitem__(), searx.result_types._base.LegacyResult.__init__(), searx.result_types._base.LegacyResult.defaults_from(), path(), pyobj(), and searx.answerers._core.AnswerStorage.register().

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,
typing.Any default = UNSET )
Get a :py:class:`pathlib.Path` object from a config string.

Definition at line 166 of file config.py.

166 def path(self, name: str, default: typing.Any = UNSET):
167 """Get a :py:class:`pathlib.Path` object from a config string."""
168
169 val = self.get(name, default)
170 if val is UNSET:
171 if default is UNSET:
172 raise KeyError(name)
173 return default
174 return pathlib.Path(str(val))
175

References get().

Here is the call graph for this function:

◆ pyobj()

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

Definition at line 176 of file config.py.

176 def pyobj(self, name: str, default: typing.Any = UNSET):
177 """Get python object referred by full qualiffied name (FQN) in the config
178 string."""
179
180 fqn = self.get(name, default)
181 if fqn is UNSET:
182 if default is UNSET:
183 raise KeyError(name)
184 return default
185 (modulename, name) = str(fqn).rsplit('.', 1)
186 m = __import__(modulename, {}, {}, [name], 0)
187 return getattr(m, name)
188
189

References get().

Here is the call graph for this function:

◆ set()

searx.botdetection.config.Config.set ( self,
str name,
typing.Any 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 147 of file config.py.

147 def set(self, name: str, val: typing.Any):
148 """Set the value to which ``name`` points in the configuration.
149
150 If there is no such ``name`` in the config, a :py:obj:`KeyError` is
151 raised.
152 """
153 parent = self._get_parent_dict(name)
154 parent[name.split('.')[-1]] = val
155

References _get_parent_dict().

Here is the call graph for this function:

◆ update()

searx.botdetection.config.Config.update ( self,
dict[str, typing.Any] upd_cfg )
Update this configuration by ``upd_cfg``.

Definition at line 120 of file config.py.

120 def update(self, upd_cfg: dict[str, typing.Any]):
121 """Update this configuration by ``upd_cfg``."""
122
123 dict_deepupdate(self.cfg, upd_cfg)
124

References cfg, and searx.botdetection.config.dict_deepupdate().

Here is the call graph for this function:

◆ validate()

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

Definition at line 114 of file config.py.

114 def validate(self, cfg: dict[str, typing.Any]):
115 """Validation of dictionary ``cfg`` on :py:obj:`Config.SCHEMA`.
116 Validation is done by :py:obj:`validate`."""
117
118 return validate(self.cfg_schema, cfg, self.deprecated)
119

References cfg_schema, deprecated, and validate().

Referenced by 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

Definition at line 107 of file config.py.

Referenced by default(), and validate().

◆ deprecated

searx.botdetection.config.Config.deprecated = deprecated

Definition at line 108 of file config.py.

Referenced by validate().

◆ UNSET

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

Definition at line 72 of file config.py.

Referenced by searx.result_types._base.LegacyResult.__getattr__().


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/botdetection/config.py