Helpful codemods based on LibCST created for use in OctoPrint development.
Provided as-is for documentational purposes.
pip3 install .
See
codemod_* --help
E.g.
$ codemod_not_in --help
usage: codemod_not_in [-h] [--before] [--after] [--dryrun] [--ignore IGNORE]
[--verbose] [--test]
bases [bases ...]
Converts 'not foo in bar' to 'foo not in bar' constructs.
positional arguments:
bases Files and directories (recursive) including python files to
be modified.
optional arguments:
-h, --help show this help message and exit
--before Write the CST of the original file to file.cst.before
--after Write the CST of the transformed file to file.cst.after
--dryrun Only perform a dry run without writing back the transformed
file
--ignore IGNORE Paths to ignore, add multiple as required
--verbose Generate output for all processed files, not juse for those
with replacements
--test Run in test mode: first path is input file, second path is
file with expected output.
To run in test mode, use --test
and supply two files, input and expected output, e.g.:
$ codemod_not_in --test tests/input/not_in.py tests/expected/not_in.py
tests/input/not_in.py:4:0:
not foo in bar
✨ Test successful, contents identical
For running multiple codemods on the same inputs, it is recommended to use codemod_batch
:
$ codemod_batch --check not_in --check remove_float_conversion tests/input/file.py
tests/input/not_in.py:4:0:
not foo in bar
tests/input/not_in.py: 1 replacements done
This repository can be used with pre-commit.
- repo: https://github.com/OctoPrint/codemods
rev: "0.6.3"
hooks:
- id: codemod_not_in
Additional arguments can also be specified:
- repo: https://github.com/OctoPrint/codemods
rev: "0.6.3"
hooks:
- id: codemod_not_in
args: ["--ignore", "lib/vendor"]
If more than one command should be run, use the batch
command for better performance:
- repo: https://github.com/OctoPrint/codemods
rev: "0.6.3"
hooks:
- id: codemod_batch
args:
[
"--ignore",
"lib/vendor",
"--check",
"not_in",
"--check",
"remove_float_conversion"
]
🛑 Heads-up
Mods overlapping with pyupgrade have been removed in version 0.6.0.
Converts not foo in bar
to foo not in bar
constructs.
Removes from builtins import ...
and import builtins
.
Use with Python 3 source only.
Removes unnecessary float conversions and .0
s in division and multiplication.
Use with Python 3 source only, unless from __future__ import division
is used.
Detects from past... import ...
& import past...
.
Use with Python 3 source only.
Checkout out the source. Install source and requirements, in editable mode:
pip install -e . -r requirements.txt
All existing tests can be run with pytest
.
Individual tests can be run with codemod_{codemod} --test tests/input/{codemod}.py tests/expected/{codemod}.py
(replacing {codemod}
with the codemod to test).
When adding new codemods or checks, add implementation to octoprint_codemods
(be sure to inherit from octoprint_codemods.Codemod
or octoprint_codemods.Codecheck
and implement main
using octoprint_codemods.runner
, see existing code).
--before
and --after
can be used to generated dumps of the CST before and after transformation. --dryrun
helps to keep input unmodified during development.
MIT