Method: flask.config.Config.from_file
Calls: 3, Exceptions: 2, Paths: 3Back
Path 1: 1 calls (0.33)
'/Users/andrehora/Documents/git/projects-pathspotter/flask/tests/static/config.json' (1)
load def (1)
False (1)
True (1)
True (1)
1def from_file(
2 self,
3 filename: str | os.PathLike,
4 load: t.Callable[[t.IO[t.Any]], t.Mapping],
5 silent: bool = False,
6 text: bool = True,
7 ) -> bool:
8 """Update the values in the config from a file that is loaded
9 using the ``load`` parameter. The loaded data is passed to the
10 :meth:`from_mapping` method.
11
12 .. code-block:: python
13
14 import json
15 app.config.from_file("config.json", load=json.load)
16
17 import tomllib
18 app.config.from_file("config.toml", load=tomllib.load, text=False)
19
20 :param filename: The path to the data file. This can be an
21 absolute path or relative to the config root path.
22 :param load: A callable that takes a file handle and returns a
23 mapping of loaded data from the file.
24 :type load: ``Callable[[Reader], Mapping]`` where ``Reader``
25 implements a ``read`` method.
26 :param silent: Ignore the file if it doesn't exist.
27 :param text: Open the file in text or binary mode.
28 :return: ``True`` if the file was loaded successfully.
29
30 .. versionchanged:: 2.3
31 The ``text`` parameter was added.
32
33 .. versionadded:: 2.0
34 """
35 filename = os.path.join(self.root_path, filename)
36
37 try:
38 with open(filename, "r" if text else "rb") as f:
39 obj = load(f)
40 except OSError as e:
41 if silent and e.errno in (errno.ENOENT, errno.EISDIR):
42 return False
43
44 e.strerror = f"Unable to load configuration file ({e.strerror})"
45 raise
46
47 return self.from_mapping(obj)
Path 2: 1 calls (0.33)
'missing.json' (1)
load def (1)
False (1)
True (1)
FileNotFoundError (1)
1def from_file(
2 self,
3 filename: str | os.PathLike,
4 load: t.Callable[[t.IO[t.Any]], t.Mapping],
5 silent: bool = False,
6 text: bool = True,
7 ) -> bool:
8 """Update the values in the config from a file that is loaded
9 using the ``load`` parameter. The loaded data is passed to the
10 :meth:`from_mapping` method.
11
12 .. code-block:: python
13
14 import json
15 app.config.from_file("config.json", load=json.load)
16
17 import tomllib
18 app.config.from_file("config.toml", load=tomllib.load, text=False)
19
20 :param filename: The path to the data file. This can be an
21 absolute path or relative to the config root path.
22 :param load: A callable that takes a file handle and returns a
23 mapping of loaded data from the file.
24 :type load: ``Callable[[Reader], Mapping]`` where ``Reader``
25 implements a ``read`` method.
26 :param silent: Ignore the file if it doesn't exist.
27 :param text: Open the file in text or binary mode.
28 :return: ``True`` if the file was loaded successfully.
29
30 .. versionchanged:: 2.3
31 The ``text`` parameter was added.
32
33 .. versionadded:: 2.0
34 """
35 filename = os.path.join(self.root_path, filename)
36
37 try:
38 with open(filename, "r" if text else "rb") as f:
39 obj = load(f)
40 except OSError as e:
41 if silent and e.errno in (errno.ENOENT, errno.EISDIR):
42 return False
43
44 e.strerror = f"Unable to load configuration file ({e.strerror})"
45 raise
46
47 return self.from_mapping(obj)
Path 3: 1 calls (0.33)
'missing.json' (1)
load def (1)
True (1)
True (1)
False (1)
FileNotFoundError (1)
1def from_file(
2 self,
3 filename: str | os.PathLike,
4 load: t.Callable[[t.IO[t.Any]], t.Mapping],
5 silent: bool = False,
6 text: bool = True,
7 ) -> bool:
8 """Update the values in the config from a file that is loaded
9 using the ``load`` parameter. The loaded data is passed to the
10 :meth:`from_mapping` method.
11
12 .. code-block:: python
13
14 import json
15 app.config.from_file("config.json", load=json.load)
16
17 import tomllib
18 app.config.from_file("config.toml", load=tomllib.load, text=False)
19
20 :param filename: The path to the data file. This can be an
21 absolute path or relative to the config root path.
22 :param load: A callable that takes a file handle and returns a
23 mapping of loaded data from the file.
24 :type load: ``Callable[[Reader], Mapping]`` where ``Reader``
25 implements a ``read`` method.
26 :param silent: Ignore the file if it doesn't exist.
27 :param text: Open the file in text or binary mode.
28 :return: ``True`` if the file was loaded successfully.
29
30 .. versionchanged:: 2.3
31 The ``text`` parameter was added.
32
33 .. versionadded:: 2.0
34 """
35 filename = os.path.join(self.root_path, filename)
36
37 try:
38 with open(filename, "r" if text else "rb") as f:
39 obj = load(f)
40 except OSError as e:
41 if silent and e.errno in (errno.ENOENT, errno.EISDIR):
42 return False
43
44 e.strerror = f"Unable to load configuration file ({e.strerror})"
45 raise
46
47 return self.from_mapping(obj)