.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 upd_cfg)
 default (self, str name)
typing.Any get (self, str name, typing.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 67 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 96 of file config.py.

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

Member Function Documentation

◆ __getitem__()

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

Definition at line 109 of file config.py.

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

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

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

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

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

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

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

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

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

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

Definition at line 164 of file config.py.

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

References get().

Here is the call graph for this function:

◆ pyobj()

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

Definition at line 174 of file config.py.

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

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

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

References _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 118 of file config.py.

118 def update(self, upd_cfg: dict):
119 """Update this configuration by ``upd_cfg``."""
120
121 dict_deepupdate(self.cfg, upd_cfg)
122

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

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

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

Referenced by default(), and validate().

◆ deprecated

searx.botdetection.config.Config.deprecated = deprecated

Definition at line 106 of file config.py.

Referenced by validate().

◆ UNSET

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

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