line_profiler.toml_config module¶
Read and resolve user-supplied TOML files and combine them with the default to generate configurations.
- class line_profiler.toml_config.ConfigSource(conf_dict: Dict[str, Any], path: Path, subtable: List[str])[source]¶
Bases:
objectObject encapsulating the config dict and the source whence it is read from.
- Variables:
conf_dict (dict[str, Any]) – The combination of the
tool.line_profilertables of the provided/looked-up config file (if any) and the default as a dictionary.path (pathlib.Path) – Absolute path to the config file whence the config options are loaded.
subtable (list[str]) – Sequence of table headers under which in
pathconf_dictcan be found.
- get_subconfig(*headers, allow_absence=False, copy=False)[source]¶
- Parameters:
headers (str) – Table headers.
allow_absence (bool) – If true, allow for the keys to be absent (and return an instance with an empty
conf_dict); otherwise, raise aKeyError.copy (bool) – If true, create a (deep) copy of the subtable in
selffor the new instance’sconf_dict; otherwise, just refer to the existing subtable.
- Returns:
New instance which consists of the required subtable of the existing one.
Example
>>> default = ConfigSource.from_default() >>> display_widths = default.get_subconfig( ... 'show', 'column_widths') >>> assert display_widths.path == default.path >>> assert (display_widths.subtable ... == default.subtable + ['show', 'column_widths']) >>> assert (display_widths.conf_dict ... is default.conf_dict['show']['column_widths'])
- classmethod from_default(*, copy=True)[source]¶
Get the default TOML configuration that ships with the package.
- Parameters:
copy (bool) – Whether to make a copy.
- Returns:
New instance if
copyis true, the global default instance otherwise.
- classmethod from_config(config=None, *, read_env=True)[source]¶
Create an instance by loading from a config file.
- Parameters:
config (str | os.PathLike[str] | bool | None) – Optional path to a specific TOML file; if a (string) path, try to read from that file; if
NoneorTrue, look up and resolve to the correct file; ifFalse, just return a copy of the default config (seefrom_default()).read_env (bool) – How to look up the config file if not provided (i.e.
config = Noneor equivalentlyTrue):TrueTry to read the environment variable
LINE_PROFILER_RCas the path to a config file; if that fails, fall back to the default configuation (seefrom_default()).FalseUse path-based lookup (see Note) to resolve to a config file.
- Returns:
New instance
Note
For the config TOML file, it is required that each of the following keys either is absent or maps to a table:
toolandtool.line_profilertool.line_profiler.kernprof,.cli,.setup,.write, and.showtool.line_profiler.show.column_widths
If this is not the case:
If
configis provided, aValueErroris raised.Otherwise, the looked-up file is considered invalid and ignored.
When performing path-based lookup:
The current directory is checked first to see if it has a valid, readable TOML file named
line_profiler.toml.If not, check if there is a valid, readable TOML file named
pyproject.toml.If not, check the parent directory, and so on.
If we reached the file-system root without finding a valid, readable TOML file, fall back to the default configuration (see
from_default()).