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 (str | os.PathLike[str] | bool | None) – Optional path to a specific TOML file; if a (string) path, try to read from that file; if None or True, look up and resolve to the correct file; if False, just return a copy of the default config (see from_default()).

  • read_env (bool) – How to look up the config file if not provided (i.e. config = None or equivalently True):

    True

    Try to read the environment variable LINE_PROFILER_RC as the path to a config file; if that fails, fall back to the default configuation (see from_default()).

    False

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

    • 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.

  • 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()).