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: object

Object encapsulating the config dict and the source whence it is read from.

Variables:
  • conf_dict (dict[str, Any]) – The combination of the tool.line_profiler tables 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 path conf_dict can be found.

conf_dict: Dict[str, Any]
path: Path
subtable: List[str]
copy()[source]
Returns:

Copy of the object.

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 a KeyError.

  • copy (bool) – If true, create a (deep) copy of the subtable in self for the new instance’s conf_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 copy is 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 (Union[str, PurePath, bool, None]) – Optional path to a specific TOML file; if a (string) path, skip lookup and just try to read from that file; if None or True, use lookup to resolve to the correct file; if False, skip lookup and just use the default configs

  • read_env (bool) – Whether to read the environment variable LINE_PROFILER_RC for a config file (instead of moving straight onto environment-based lookup) if config is not provided.

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:

  • tool and tool.line_profiler

  • tool.line_profiler.kernprof, .cli, .setup, .write, and .show

  • tool.line_profiler.show.column_widths

If this is not the case:

  • If config is provided, a ValueError is raised.

  • Otherwise, the looked-up file is considered invalid and ignored.