MCPcopy
hub / github.com/pallets/flask / from_pyfile

Method from_pyfile

src/flask/config.py:187–216  ·  view source on GitHub ↗

Updates the values in the config from a Python file. This function behaves as if the file was imported as module with the :meth:`from_object` function. :param filename: the filename of the config. This can either be an absolute filename or a filena

(
        self, filename: str | os.PathLike[str], silent: bool = False
    )

Source from the content-addressed store, hash-verified

185 return True
186
187 def from_pyfile(
188 self, filename: str | os.PathLike[str], silent: bool = False
189 ) -> bool:
190 """Updates the values in the config from a Python file. This function
191 behaves as if the file was imported as module with the
192 :meth:`from_object` function.
193
194 :param filename: the filename of the config. This can either be an
195 absolute filename or a filename relative to the
196 root path.
197 :param silent: set to ``True`` if you want silent failure for missing
198 files.
199 :return: ``True`` if the file was loaded successfully.
200
201 .. versionadded:: 0.7
202 `silent` parameter.
203 """
204 filename = os.path.join(self.root_path, filename)
205 d = types.ModuleType("config")
206 d.__file__ = filename
207 try:
208 with open(filename, mode="rb") as config_file:
209 exec(compile(config_file.read(), filename, "exec"), d.__dict__)
210 except OSError as e:
211 if silent and e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR):
212 return False
213 e.strerror = f"Unable to load configuration file ({e.strerror})"
214 raise
215 self.from_object(d)
216 return True
217
218 def from_object(self, obj: object | str) -> None:
219 """Updates the values from the given object. An object can be of one

Callers 5

from_envvarMethod · 0.95
test_config_from_pyfileFunction · 0.80
test_config_missingFunction · 0.80
create_appFunction · 0.80

Calls 1

from_objectMethod · 0.95

Tested by 3

test_config_from_pyfileFunction · 0.64
test_config_missingFunction · 0.64