Commit 49f8658
ENH: read TOML files for configurations (#335)
* Basic TOML facilities
line_profiler/line_profiler_rc.toml
Default configuration file
line_profiler/toml_config.py[i]
New module for reading TOML config files
* Packaging updates
MANIFEST.in, setup.py
Configured to include `line_profiler/line_profiler_rc.toml` in
source and wheel distributions
requirements/runtime.txt
Added dependency `tomli` for Python < 3.11 (to stand in for
`tomllib`)
* WIP: `kernprof` refactoring
kernprof.py
- Made all `line_profiler` imports unconditional (the ship has
sailed, there's already an unconditional import for
`line_profiler.profiler_mixin.ByCountProfilerMixin`)
- For each boolean option (e.g. `--view`):
- Added a corresponding negative option (e.g. `--no-view`)
- Changed the default value from `False` to `None`, so that we can
distinguish between cases where the negative option is passed
and no option is passed (and in that case read from the config
(TODO))
main(), find_module_script(), find_script()
Added argument `exit_on_error` to optionally prevent parsing errors
from killing the interpretor
* `kernprof` refactoring (reading configs)
kernprof.py
__doc__
Updated with newest `kernprof --help` output
short_string_path()
New helper function for abbreviating paths
_python_command()
- Replaced string comparison between paths with
`os.path.samefile()`
- Updated to use abbreviated paths where possible
main()
- Updated description to include documentation for the negative
options
- Added option `--config` for loading config from a specific
file instead of going through lookup
- Updated `const` value for the bare `-i`/`--output-intereval`
option (the old value 0, equivalent to not specifying the
option, doesn't really make sense)
- Grouped options into argument groups for better organization
- Updated help texts for options to be more stylistically
consistent and to show the default values
- Updated help texts for the `-p`/`--prof-mod` option to show an
example
- Updated help texts for the `--prof-imports` to be more in line
with what it actually does (see docstring of
`line_profiler.autoprofile.ast_tree_profiler.AstTreeProfiler`)
- Added option resolution: values of un-specified flags now
taken from the specified/looked-up config file
* Feature: read config file from the env
line_profiler/toml_config.py[i]
find_and_read_config_file()
New argument `env_var` for controlling whether/whence to read
the config from the environment if not specified via `config`
get_config()
New arguemnt `read_env` for controlling whether to read the
config from the environment variable `${LINE_PROFILER_RC}` if
specified via `config`
* Reorganized code: `line_profiler.cli_utils`
kernprof.py
Moved common utilities to `line_profiler/cli_utils.py`
line_profiler/cli_utils.py[i]
New module for common utilities shared by `kernprof` and
`python -m line_profiler`
* Made `line_profiler.line_profiler` configurable
kernprof.py::main()
Now passing the received `-c`/`--config` onto
`LineProfiler.print_stats()`
line_profiler/line_profiler.py[i]
<Overall>
Formatting changes (avoid using hanging indents where suitable,
to be more consistent with the rest of the codebase)
LineProfiler.print_stats(), show_func(), show_text()
- Added optional argument `config` to allow for specifying the
config file
- Now reading output column widths from the
`tool.line_profiler.show.column_widths` table in config files
main()
- Refactored to use the `.cli_utils`
- Added description for the CLI application
- Added negative options to the boolean options
- Added option `-c`/`--config` to read default values for the
options from the `tool.line_profiler.cli` table
line_profiler/line_profiler_rc.toml
- Added documentation for the environment variable
`${LINE_PROFILER_RC}`
- Added subtables `tool.line_profiler.cli` and
`tool.line_profiler.show.column_widths`
* Refactored `line_profiler.toml_config`
line_profiler.toml_config.py[i]
get_subtable()
- Added doctest
- Added type check that the returned object is a mapping
get_headers()
New function for getting the subtable headers from a table
get_config()
- Updated docs with reference to the new
`tool.line_profiler.cli` and `.show.column_widths` tables
- Added check for subtable existence
- Fixed traceback and error message when the table is malformed
* Moved code around
kernprof.py::_python_command
Migrated definition to
`line_profiler/cli_utils.py::get_python_executable()`
line_profiler/cli_utils.py::get_python_executable()
New function used by both `kernprof` and
`line_profiler.explicit_profiler`
* Made `.explicit_profiler` configurable
line_profiler/explicit_profiler.py
GlobalProfiler
__doc__
Updated
__init__()
- Added argument `config` to allow for explicitly providing
a config file
- Now reading `.{setup,write,show}_config` and
`.output_prefix` from the config file, instead of using
hard-coded values
show()
Minor refatoring and reformatting
_python_command()
Now using `.cli_utils.get_python_executable()`
line_profiler/line_profiler_rc.toml::[tool.line_profiler.write]
Added item `output_prefix`, corresponding to
`line_profiler.explicit_profiler.GlobalProfiler.output_prefix`
* TOML tests
line_profiler/toml_config.py::get_config()
- Now promoting `config` to `pathlib.Path` objects early so as to
catch bad arg types
- Now raising `ValueError` or `FileNotFoundError` if a `config` is
specified and loading fails
tests/test_toml_config.py
New test module for tests related to `line_profiler.toml_config`:
- test_environment_isolation()
Test that the fixture we use suffices to isolate the tests from
the environment
- test_default_config_deep_copy()
Test that `get_default_config()` returns fresh, deep copies
- test_table_normalization()
Test that `get_config()` always normalizes the config entires to
supply missing entires and remove spurious ones
- test_malformed_table()
Test that we get a `ValueError` from malformed TOML files
- test_config_lookup_hierarchy()
Test the hierarchy according to which we resolve which TOML to
read the configs from
* Config test in `tests/test_explicit_profile.py`
tests/test_explicit_profile.py
test_*()
Updated to use `tempfile.TemporaryDirectory()` instead of
`tempfile.mkdtemp()` so that the tmpdirs are cleaned up
regardless of the test outcome
test_explicit_profile_with_customized_config()
New test for customizing explicit profiling with a user-supplied
TOML config file
* Config test in `tests/test_autoprofile.py`
tests/test_autoprofile.py
test_*()
Updated to use `tempfile.TemporaryDirectory()` instead of
`tempfile.mkdtemp()` so that the tmpdirs are cleaned up
regardless of the test outcome
test_autoprofile_with_customized_config()
New test for customizing `kernprof` auto-profiling and
`python -m line_profiler` output formatting with a user-supplied
TOML config file
* Added changelog entry
* CI fixes
* `line_profiler_rc.toml` -> `line_profiler.toml`
line_profiler/explicit_profiler.py::GlobalProfiler
Updated docstring
line_profiler/toml_config.py::targets
Changed lookup target from `line_profiler_rc.toml` ->
`line_profiler.toml`
tests/test_toml_config.py::test_config_lookup_hierarchy()
Updated test to use `line_profiler.toml` instead of
`line_profiler_rc.toml`
* Updated comments in TOML file
* Reduce code run during import-time
line_profiler/line_profiler.py[i]
minimum_column_widths
Removed global constant
get_minimum_column_widths()
New cached callable for getting the above value
* Added the `--no-config` flag
kernprof.py
__doc__
Updated with new `kernprof --help` output
main()
Added a `--no-config` flag for disabling the loading of
non-default configs
line_profiler/cli_utils.py[i]::get_cli_config()
- Added the explicit named argument `config`
- Added processing for boolean values of `config` (true -> default
behavior, false -> fall back for `get_default_config()`)
line_profiler/line_profiler.py[i]
LineProfiler.print_stats(), show_func(), show_text()
Added handling for boolean values of `config`
main()
Added a `--no-config` flag for disabling the loading of
non-default configs
tests/test_autoprofile.py
test_autoprofile_with_customized_config()
Fixed malformed indentation
test_autoprofile_with_no_config()
New test for disabling config lookup
* Centralized code for `config = <bool>`
line_profiler/cli_utils.py[i]::get_cli_config()
Rolled back last commit
line_profiler/explicit_profiler.py[i]::GlobalProfiler
- Updated docstring
- Added missing `config` argument to `.__init__()` in the stub file
line_profiler/line_profiler.py
Removed wrapper function around
`line_profiler.toml_config.get_config()`
line_profiler/toml_config.py[i]::get_config()
Added handling for `config = <bool>`:
- `False`: don't look up or load any user-supplied config and just
use the default
- `True`: same as `None` (default behavior)
tests/test_toml_config.py::test_config_lookup_hierarchy()
Now also testing `get_config(True)` and `get_config(False)`
* Fixed MANIFEST
* Better boolean-option parsing
line_profiler/cli_utils.py[i]
<General>
Updated docstring to use double backticks for inlined code to be
friendlier towards sphinx/RST
add_argument()
- Updated call signature:
- `parser_like` now positional-only
- Added `arg` between `parser_like` and `*args` to indicate
that at least one positional argument should be passed to
`parser_like.add_argument()`
- Now adding separate actions for long and short boolean flags
so that the long option can be specified in both no-arg and
single-arg forms
- Now skipping the generation of the complementary falsy action
instead of raising an `AssertionError` when the boolean flag
doesn't have a long form
boolean()
New function for parsing strings into booleans
* `line_profiler` minor refactoring
line_profiler/explicit_profiler.py::GlobalProfiler._implicit_setup()
Updated parsing of environment variables to use
`line_profiler.cli_utils.boolean()`
line_profiler/line_profiler.py::main()
- Removed note on boolean options in the parser description
- Removed parenthetical remark "boolean option" from option help
texts
* Refactoring in `kernprof.py`
kernprof.py
__doc__
Updated with the latest `kernprof --help` output, and with a
narrower window so that the lines aren't too long
__doc__
RepeatedTimer.__doc__
Added linter-friendly `noqa` comment for long lines in
docstrings meant for `sphinx` consumption
RepeatedTimer.start()
main()
Wrapped certain long lines of code
main()
- Removed note on boolean options in parser description
- Removed parenthetical remark "boolean option" in option help
texts
- Removed redundant instantiation of `RepeatedTimer`
- Refactored chained if-elif-else when executing code in
non-autoprofiling mode
* Tests for `line_profiler.cli_utils.add_argument`
tests/test_cli.py
parser()
New fixture (example parser)
test_boolean_argument_help_text()
New test for the help texts for boolean options generated by
`line_profiler.cli_utils.add_argument()`
test_boolean_argument_parsing()
New test for the parsing of long and short boolean options
* Lint
* .
* Bug fixes
kernprof.py
__doc__
Updated
_add_core_parser_arguments()
Updated help text of `-p`/`--prof-mod`
_parse_arguments()
- Fixed bug where the config file is stil looked up despite
passing `--no-config`
- Added logging output for the loading of configs
_normalize_profiling_targets()
Now allow for an empty string to invalidate previous targets
(so that e.g. `--prof-mod ''` can be used to drop profiling
targets specified in the `--config`)
main(), _write_tempfile(), _write_preimports(), _pre_profile()
Now using `short_string_path()` to abbreviate filenames in
output
* Add example of using TOML config to docs
* Fix malformed f-string
Co-authored-by: Jon Crall <erotemic@gmail.com>
* Use `global` for accessing global vars
Co-authored-by: Jon Crall <erotemic@gmail.com>
* format and install fixes
* small tweak
* Refactoring of `line_profiler.toml_config`
kernprof.py
Updated implementations
line_profiler/cli_utils.py[i]
<General>
Reformatted docstrings
get_cli_config()
Now returning a `ConfigSource`
boolean()
Added doctest
line_profiler/explicit_profiler.py::GlobalProfiler
- Reformatted docstring
- Updated implementation
line_profiler/line_profiler.py[i]
get_column_widths()
Refactored from `get_minimum_column_widths()`
show_text()
main()
Updated implementations
line_profiler/toml_config.py[i]
<General>
Reformatted docstrings
NAMESPACE, TARGETS, ENV_VAR
Renamed from lowercased constants
ConfigSource
New data class refactored from `get_config()` and
`get_default_config()`
tests/test_toml_config.py
Updated implementations
* Doc fixes
- Fixed typos and links
- Added doc pages for `line_profiler.toml_config` and
`line_profiler.cli_utils`
* CLI fixes
kernprof.py
Added flag `--summarize` as an analog for
`python -m line_profiler --summarize`
line_profiler/line_profiler.py
Fixed bug where the wrong defaults are shown in the help texts of
the `--sort` and `--summarize` flags
line_profiler/rc/line_profiler.toml::[tool.line_profiler.kernprof]
Added new boolean value `summarize`
* `kernprof` fixes
kernprof.py::_post_profile()
- Added diagnostic debug message for the call to
`LineProfiler.print_stats()`
- Fixed bug where `options.summarize` is not passed to
`.print_stats()`
---------
Co-authored-by: joncrall <erotemic@gmail.com>1 parent 9e09196 commit 49f8658
24 files changed
Lines changed: 2752 additions & 837 deletions
File tree
- docs/source
- auto
- manual/examples
- line_profiler
- rc
- requirements
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| 26 | + | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
0 commit comments