refactor-utils 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- refactor_utils-2.0.0/PKG-INFO +212 -0
- refactor_utils-2.0.0/README.md +195 -0
- refactor_utils-2.0.0/pyproject.toml +43 -0
- refactor_utils-2.0.0/setup.cfg +4 -0
- refactor_utils-2.0.0/src/refactor_utils/__init__.py +6 -0
- refactor_utils-2.0.0/src/refactor_utils/cli.py +271 -0
- refactor_utils-2.0.0/src/refactor_utils/core.py +487 -0
- refactor_utils-2.0.0/src/refactor_utils/data/README.md +25 -0
- refactor_utils-2.0.0/src/refactor_utils/data/config.ini +5 -0
- refactor_utils-2.0.0/src/refactor_utils/data/example.csv +4 -0
- refactor_utils-2.0.0/src/refactor_utils/data/sample.json +6 -0
- refactor_utils-2.0.0/src/refactor_utils/discovery.py +392 -0
- refactor_utils-2.0.0/src/refactor_utils/imports.py +105 -0
- refactor_utils-2.0.0/src/refactor_utils/logging_setup.py +15 -0
- refactor_utils-2.0.0/src/refactor_utils/models.py +111 -0
- refactor_utils-2.0.0/src/refactor_utils/ordering.py +78 -0
- refactor_utils-2.0.0/src/refactor_utils/parser.py +831 -0
- refactor_utils-2.0.0/src/refactor_utils/resolver.py +760 -0
- refactor_utils-2.0.0/src/refactor_utils/writer.py +100 -0
- refactor_utils-2.0.0/src/refactor_utils.egg-info/PKG-INFO +212 -0
- refactor_utils-2.0.0/src/refactor_utils.egg-info/SOURCES.txt +24 -0
- refactor_utils-2.0.0/src/refactor_utils.egg-info/dependency_links.txt +1 -0
- refactor_utils-2.0.0/src/refactor_utils.egg-info/entry_points.txt +2 -0
- refactor_utils-2.0.0/src/refactor_utils.egg-info/requires.txt +13 -0
- refactor_utils-2.0.0/src/refactor_utils.egg-info/top_level.txt +1 -0
- refactor_utils-2.0.0/tests/test_consolidate.py +666 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: refactor_utils
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Consolidate a small/medium Python module, library or project into a single, dependency-ordered file.
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: loguru
|
|
8
|
+
Requires-Dist: tree-sitter
|
|
9
|
+
Requires-Dist: tree_sitter_python
|
|
10
|
+
Provides-Extra: format
|
|
11
|
+
Requires-Dist: black; extra == "format"
|
|
12
|
+
Provides-Extra: progress
|
|
13
|
+
Requires-Dist: tqdm; extra == "progress"
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest; extra == "dev"
|
|
16
|
+
Requires-Dist: ruff; extra == "dev"
|
|
17
|
+
|
|
18
|
+
# refactor_utils
|
|
19
|
+
|
|
20
|
+
Consolidate a small/medium Python module, library or project into **one**
|
|
21
|
+
dependency-ordered file.
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install -e .
|
|
25
|
+
pyrefactor path/to/package # writes ./output/<current-directory-name>.py
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
A run without flags produces exactly one generated file:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
$ cd myproject
|
|
32
|
+
$ pyrefactor src/mypackage
|
|
33
|
+
output/
|
|
34
|
+
myproject.py # imports -> constants -> classes -> functions -> entry points
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The file name is `Path.cwd().name` (override with `--name`). Data files that
|
|
38
|
+
ship next to the code (`.json`, `.yaml`, `.csv`, `.ini`, …) are copied with
|
|
39
|
+
their relative paths unless `--no-copy-data` is given.
|
|
40
|
+
|
|
41
|
+
## What it does
|
|
42
|
+
|
|
43
|
+
- Parses every file with **tree-sitter** in parallel worker processes; skips
|
|
44
|
+
tests, `conftest.py`, `setup.py`, `__main__.py` and `iwgfyrkugf.py`
|
|
45
|
+
(re-include anything with `--include`, skip more with `-x/--exclude`).
|
|
46
|
+
- Orders blocks by import-time dependencies (base classes, decorators,
|
|
47
|
+
defaults, class bodies, module-level calls), preferring constants → classes →
|
|
48
|
+
functions. Cycles are reported with a warning and emitted in dependency order.
|
|
49
|
+
- Resolves same-named objects across modules using a **content hash**: bodies
|
|
50
|
+
that are byte-identical (ignoring blank lines/trailing spaces) are merged into
|
|
51
|
+
a single definition; bodies that differ keep both definitions and the
|
|
52
|
+
shallower module keeps the plain name while the others become
|
|
53
|
+
`<module_path>_<name>`. Every reference (`from .x import y as z`, `mod.attr`,
|
|
54
|
+
annotations, decorators, defaults) is rewritten accordingly.
|
|
55
|
+
- Merges, prunes, de-duplicates and conflict-aliases imports
|
|
56
|
+
(`--imports prune|keep|strict`), grouping `__future__` / stdlib / third-party.
|
|
57
|
+
Names starting with `_` sort before alphanumeric names.
|
|
58
|
+
- `if TYPE_CHECKING:` blocks are merged like the head import section: the
|
|
59
|
+
imports inside join the normal import section (deduplicated against imports
|
|
60
|
+
already present at the top of the file), and constants/classes/functions
|
|
61
|
+
defined under `TYPE_CHECKING` are extracted as ordinary blocks.
|
|
62
|
+
- `__all__` is never emitted as code. `__all__ = […]`, `__all__ += […]`,
|
|
63
|
+
`__all__.append(…)` and annotated forms are only read as hints and dropped;
|
|
64
|
+
the exported surface is recomputed from the surviving definitions.
|
|
65
|
+
- `main` functions and `if __name__ == "__main__":` guards go to the end.
|
|
66
|
+
When several modules have their own guard, they are **merged into a single
|
|
67
|
+
guard** whose body runs each original entry point, annotated with the module
|
|
68
|
+
it came from — stacked guards would make every one but the first dead code.
|
|
69
|
+
Control which entries survive with `--entry MODULE` / `--entry-keep-all`, and
|
|
70
|
+
the names with `--main-name NAME` / `--generate-guard`.
|
|
71
|
+
- **Validation is mandatory**: the generated module is compiled and smoke
|
|
72
|
+
imported before anything is written; a broken result is never emitted. A
|
|
73
|
+
source file that cannot be parsed aborts the run instead of degrading output.
|
|
74
|
+
- Errors and warnings are logged with **loguru** (`--log-level`, `--log-file`).
|
|
75
|
+
|
|
76
|
+
## CLI
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
usage: pyrefactor [-h] [-o OUTPUT] [--name NAME] [-j WORKERS]
|
|
80
|
+
[--executor {process,thread}] [--progress] [-x GLOB]
|
|
81
|
+
[--include GLOB] [--copy-data | --no-copy-data]
|
|
82
|
+
[--data-glob GLOB] [--entry MODULE] [--entry-keep-all]
|
|
83
|
+
[--main-name NAME] [--generate-guard]
|
|
84
|
+
[--imports {prune,keep,strict}]
|
|
85
|
+
[--keep-docstrings | --no-keep-docstrings]
|
|
86
|
+
[--keep-comments | --no-keep-comments] [--dry-run] [--check]
|
|
87
|
+
[--stdout] [--diff] [--force] [--config PATH] [--format]
|
|
88
|
+
[--formatter {black,ruff}]
|
|
89
|
+
[--log-level {TRACE,DEBUG,INFO,WARNING,ERROR}]
|
|
90
|
+
[--log-file LOG_FILE] [--version]
|
|
91
|
+
source
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
| Flag | Default | Effect |
|
|
95
|
+
|------|---------|--------|
|
|
96
|
+
| `source` | — | package directory, project root or single `.py` file |
|
|
97
|
+
| `-o, --output` | `output` | output directory |
|
|
98
|
+
| `--name NAME` | current directory name | name of the generated file |
|
|
99
|
+
| `-j, --workers` | CPU count | parallel parsing workers |
|
|
100
|
+
| `--executor` | `process` | `process` or `thread` executor |
|
|
101
|
+
| `--progress` | off | tqdm progress bar (falls back to log) |
|
|
102
|
+
| `-x, --exclude GLOB` | — | extra globs to skip (repeatable), source-relative |
|
|
103
|
+
| `--include GLOB` | — | re-include files/dirs excluded by default (`tests/*`, `conftest.py`, …) |
|
|
104
|
+
| `--copy-data / --no-copy-data` | on | copy data files preserving relative paths |
|
|
105
|
+
| `--data-glob GLOB` | — | only copy matching data files (repeatable) |
|
|
106
|
+
| `--entry MODULE` | — | keep only the `__main__` guard of this module (repeatable) |
|
|
107
|
+
| `--entry-keep-all` | off | keep every `__main__` guard |
|
|
108
|
+
| `--main-name NAME` | — | rename `main` to `NAME` |
|
|
109
|
+
| `--generate-guard` | off | synthesise `if __name__ == "__main__": main()` |
|
|
110
|
+
| `--imports` | `prune` | `prune` = drop unused, `keep` = keep all, `strict` = prune + warn |
|
|
111
|
+
| `--keep-docstrings / --no-keep-docstrings` | on | keep docstrings |
|
|
112
|
+
| `--keep-comments / --no-keep-comments` | on | keep comments |
|
|
113
|
+
| `--dry-run` | off | print the plan, never write |
|
|
114
|
+
| `--check` | off | fail (exit 2) if the output would change |
|
|
115
|
+
| `--stdout` | off | print the generated module instead of writing |
|
|
116
|
+
| `--diff` | off | print a unified diff against the existing output |
|
|
117
|
+
| `--force` | off | silence the overwrite warning |
|
|
118
|
+
| `--config PATH` | auto | `pyproject.toml` with a `[tool.pyrefactor]` section |
|
|
119
|
+
| `--format` | off | run the formatter on the output |
|
|
120
|
+
| `--formatter` | `black` | `black` or `ruff` |
|
|
121
|
+
| `--log-level`, `--log-file`, `--version` | | logging and version |
|
|
122
|
+
|
|
123
|
+
Removed in 2.0: `--mode`, `--validate`, `--validate-import`,
|
|
124
|
+
`--allow-syntax-errors`, `--max-skipped`, `--source-map`, `--preserve-modules`,
|
|
125
|
+
`--namespace`, `--all`, `--respect-all`, `--rename-strategy`, `--dedupe`,
|
|
126
|
+
`--no-parallel`, `--jobs`, `--progress-bar`, `--overwrite`, `--keep-all-mains`,
|
|
127
|
+
`--keep-unused-imports`, `--post-format`, `--pyproject`, `--setup-cfg`,
|
|
128
|
+
`--strict`, `--fail-on-warning`, `--respect-all`. There is one layout, one
|
|
129
|
+
dedupe strategy (content hash), one rename strategy (`<module>_<name>`) and
|
|
130
|
+
validation always runs.
|
|
131
|
+
|
|
132
|
+
## Examples
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
# consolidate the current project into ./output/<cwd>.py
|
|
136
|
+
pyrefactor .
|
|
137
|
+
|
|
138
|
+
# keep the file name stable in CI
|
|
139
|
+
pyrefactor src/mypackage --name mypackage --check
|
|
140
|
+
|
|
141
|
+
# preview without writing anything
|
|
142
|
+
pyrefactor src/mypackage --dry-run
|
|
143
|
+
|
|
144
|
+
# skip vendored code, keep its data files
|
|
145
|
+
pyrefactor . -x "vendor/*"
|
|
146
|
+
|
|
147
|
+
# include the tests directory that is skipped by default
|
|
148
|
+
pyrefactor . --include "tests/*"
|
|
149
|
+
|
|
150
|
+
# format the result and drop comments
|
|
151
|
+
pyrefactor . --format --formatter ruff --no-keep-comments
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Configuration
|
|
155
|
+
|
|
156
|
+
`pyproject.toml` next to the source (or `--config PATH`):
|
|
157
|
+
|
|
158
|
+
```toml
|
|
159
|
+
[tool.pyrefactor]
|
|
160
|
+
exclude = ["tests/*", "iwgfyrkugf.py"]
|
|
161
|
+
include = []
|
|
162
|
+
entry = []
|
|
163
|
+
imports = "prune"
|
|
164
|
+
format = false
|
|
165
|
+
copy_data = true
|
|
166
|
+
data_glob = ["*.json"]
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
CLI values win where they are explicit.
|
|
170
|
+
|
|
171
|
+
## Architecture
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
cli.py → core.py orchestrates
|
|
175
|
+
→ discovery.py (SourceFile + DataFile, pyproject awareness)
|
|
176
|
+
→ parser.py (tree-sitter, parallel, TYPE_CHECKING flattening, __all__ detection)
|
|
177
|
+
→ resolver.py (content-hash dedupe, collision renames, imports, ordering input)
|
|
178
|
+
→ ordering.py (topo sort, deterministic tie-breaks)
|
|
179
|
+
→ writer.py (single consolidated file)
|
|
180
|
+
→ imports.py (import merging/sorting: `_private` names first)
|
|
181
|
+
logging via logging_setup.py
|
|
182
|
+
models.py — picklable dataclasses (Block, ModuleInfo, SourceFile, DataFile, ImportSpec, Occ)
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
The parser only emits `ModuleInfo`/`Block`/`Occ`, so worker payloads stay
|
|
186
|
+
picklable and the resolver never touches tree-sitter nodes.
|
|
187
|
+
|
|
188
|
+
## Known limits
|
|
189
|
+
|
|
190
|
+
- Renaming is name-based: parameters and class bodies are scoped, other locals
|
|
191
|
+
are not (a full scope analysis is future work).
|
|
192
|
+
- `__file__`/`__name__`/`__package__` semantics change in a single file
|
|
193
|
+
(warning logged; `importlib.resources` users may need `files()`).
|
|
194
|
+
- Guarded imports (`try/except ImportError`, `if sys.platform …`) are kept
|
|
195
|
+
verbatim and may have side effects.
|
|
196
|
+
- Dynamic `importlib`/`__import__` dependencies are warned about, not resolved.
|
|
197
|
+
|
|
198
|
+
## Development
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
python -m venv .venv && .venv/bin/pip install -e .[dev]
|
|
202
|
+
.venv/bin/python -m pytest tests -q
|
|
203
|
+
.venv/bin/ruff check src tests
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
# repo address
|
|
207
|
+
|
|
208
|
+
i4147/refactor_utils
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
MIT
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# refactor_utils
|
|
2
|
+
|
|
3
|
+
Consolidate a small/medium Python module, library or project into **one**
|
|
4
|
+
dependency-ordered file.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
pip install -e .
|
|
8
|
+
pyrefactor path/to/package # writes ./output/<current-directory-name>.py
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
A run without flags produces exactly one generated file:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
$ cd myproject
|
|
15
|
+
$ pyrefactor src/mypackage
|
|
16
|
+
output/
|
|
17
|
+
myproject.py # imports -> constants -> classes -> functions -> entry points
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The file name is `Path.cwd().name` (override with `--name`). Data files that
|
|
21
|
+
ship next to the code (`.json`, `.yaml`, `.csv`, `.ini`, …) are copied with
|
|
22
|
+
their relative paths unless `--no-copy-data` is given.
|
|
23
|
+
|
|
24
|
+
## What it does
|
|
25
|
+
|
|
26
|
+
- Parses every file with **tree-sitter** in parallel worker processes; skips
|
|
27
|
+
tests, `conftest.py`, `setup.py`, `__main__.py` and `iwgfyrkugf.py`
|
|
28
|
+
(re-include anything with `--include`, skip more with `-x/--exclude`).
|
|
29
|
+
- Orders blocks by import-time dependencies (base classes, decorators,
|
|
30
|
+
defaults, class bodies, module-level calls), preferring constants → classes →
|
|
31
|
+
functions. Cycles are reported with a warning and emitted in dependency order.
|
|
32
|
+
- Resolves same-named objects across modules using a **content hash**: bodies
|
|
33
|
+
that are byte-identical (ignoring blank lines/trailing spaces) are merged into
|
|
34
|
+
a single definition; bodies that differ keep both definitions and the
|
|
35
|
+
shallower module keeps the plain name while the others become
|
|
36
|
+
`<module_path>_<name>`. Every reference (`from .x import y as z`, `mod.attr`,
|
|
37
|
+
annotations, decorators, defaults) is rewritten accordingly.
|
|
38
|
+
- Merges, prunes, de-duplicates and conflict-aliases imports
|
|
39
|
+
(`--imports prune|keep|strict`), grouping `__future__` / stdlib / third-party.
|
|
40
|
+
Names starting with `_` sort before alphanumeric names.
|
|
41
|
+
- `if TYPE_CHECKING:` blocks are merged like the head import section: the
|
|
42
|
+
imports inside join the normal import section (deduplicated against imports
|
|
43
|
+
already present at the top of the file), and constants/classes/functions
|
|
44
|
+
defined under `TYPE_CHECKING` are extracted as ordinary blocks.
|
|
45
|
+
- `__all__` is never emitted as code. `__all__ = […]`, `__all__ += […]`,
|
|
46
|
+
`__all__.append(…)` and annotated forms are only read as hints and dropped;
|
|
47
|
+
the exported surface is recomputed from the surviving definitions.
|
|
48
|
+
- `main` functions and `if __name__ == "__main__":` guards go to the end.
|
|
49
|
+
When several modules have their own guard, they are **merged into a single
|
|
50
|
+
guard** whose body runs each original entry point, annotated with the module
|
|
51
|
+
it came from — stacked guards would make every one but the first dead code.
|
|
52
|
+
Control which entries survive with `--entry MODULE` / `--entry-keep-all`, and
|
|
53
|
+
the names with `--main-name NAME` / `--generate-guard`.
|
|
54
|
+
- **Validation is mandatory**: the generated module is compiled and smoke
|
|
55
|
+
imported before anything is written; a broken result is never emitted. A
|
|
56
|
+
source file that cannot be parsed aborts the run instead of degrading output.
|
|
57
|
+
- Errors and warnings are logged with **loguru** (`--log-level`, `--log-file`).
|
|
58
|
+
|
|
59
|
+
## CLI
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
usage: pyrefactor [-h] [-o OUTPUT] [--name NAME] [-j WORKERS]
|
|
63
|
+
[--executor {process,thread}] [--progress] [-x GLOB]
|
|
64
|
+
[--include GLOB] [--copy-data | --no-copy-data]
|
|
65
|
+
[--data-glob GLOB] [--entry MODULE] [--entry-keep-all]
|
|
66
|
+
[--main-name NAME] [--generate-guard]
|
|
67
|
+
[--imports {prune,keep,strict}]
|
|
68
|
+
[--keep-docstrings | --no-keep-docstrings]
|
|
69
|
+
[--keep-comments | --no-keep-comments] [--dry-run] [--check]
|
|
70
|
+
[--stdout] [--diff] [--force] [--config PATH] [--format]
|
|
71
|
+
[--formatter {black,ruff}]
|
|
72
|
+
[--log-level {TRACE,DEBUG,INFO,WARNING,ERROR}]
|
|
73
|
+
[--log-file LOG_FILE] [--version]
|
|
74
|
+
source
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
| Flag | Default | Effect |
|
|
78
|
+
|------|---------|--------|
|
|
79
|
+
| `source` | — | package directory, project root or single `.py` file |
|
|
80
|
+
| `-o, --output` | `output` | output directory |
|
|
81
|
+
| `--name NAME` | current directory name | name of the generated file |
|
|
82
|
+
| `-j, --workers` | CPU count | parallel parsing workers |
|
|
83
|
+
| `--executor` | `process` | `process` or `thread` executor |
|
|
84
|
+
| `--progress` | off | tqdm progress bar (falls back to log) |
|
|
85
|
+
| `-x, --exclude GLOB` | — | extra globs to skip (repeatable), source-relative |
|
|
86
|
+
| `--include GLOB` | — | re-include files/dirs excluded by default (`tests/*`, `conftest.py`, …) |
|
|
87
|
+
| `--copy-data / --no-copy-data` | on | copy data files preserving relative paths |
|
|
88
|
+
| `--data-glob GLOB` | — | only copy matching data files (repeatable) |
|
|
89
|
+
| `--entry MODULE` | — | keep only the `__main__` guard of this module (repeatable) |
|
|
90
|
+
| `--entry-keep-all` | off | keep every `__main__` guard |
|
|
91
|
+
| `--main-name NAME` | — | rename `main` to `NAME` |
|
|
92
|
+
| `--generate-guard` | off | synthesise `if __name__ == "__main__": main()` |
|
|
93
|
+
| `--imports` | `prune` | `prune` = drop unused, `keep` = keep all, `strict` = prune + warn |
|
|
94
|
+
| `--keep-docstrings / --no-keep-docstrings` | on | keep docstrings |
|
|
95
|
+
| `--keep-comments / --no-keep-comments` | on | keep comments |
|
|
96
|
+
| `--dry-run` | off | print the plan, never write |
|
|
97
|
+
| `--check` | off | fail (exit 2) if the output would change |
|
|
98
|
+
| `--stdout` | off | print the generated module instead of writing |
|
|
99
|
+
| `--diff` | off | print a unified diff against the existing output |
|
|
100
|
+
| `--force` | off | silence the overwrite warning |
|
|
101
|
+
| `--config PATH` | auto | `pyproject.toml` with a `[tool.pyrefactor]` section |
|
|
102
|
+
| `--format` | off | run the formatter on the output |
|
|
103
|
+
| `--formatter` | `black` | `black` or `ruff` |
|
|
104
|
+
| `--log-level`, `--log-file`, `--version` | | logging and version |
|
|
105
|
+
|
|
106
|
+
Removed in 2.0: `--mode`, `--validate`, `--validate-import`,
|
|
107
|
+
`--allow-syntax-errors`, `--max-skipped`, `--source-map`, `--preserve-modules`,
|
|
108
|
+
`--namespace`, `--all`, `--respect-all`, `--rename-strategy`, `--dedupe`,
|
|
109
|
+
`--no-parallel`, `--jobs`, `--progress-bar`, `--overwrite`, `--keep-all-mains`,
|
|
110
|
+
`--keep-unused-imports`, `--post-format`, `--pyproject`, `--setup-cfg`,
|
|
111
|
+
`--strict`, `--fail-on-warning`, `--respect-all`. There is one layout, one
|
|
112
|
+
dedupe strategy (content hash), one rename strategy (`<module>_<name>`) and
|
|
113
|
+
validation always runs.
|
|
114
|
+
|
|
115
|
+
## Examples
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# consolidate the current project into ./output/<cwd>.py
|
|
119
|
+
pyrefactor .
|
|
120
|
+
|
|
121
|
+
# keep the file name stable in CI
|
|
122
|
+
pyrefactor src/mypackage --name mypackage --check
|
|
123
|
+
|
|
124
|
+
# preview without writing anything
|
|
125
|
+
pyrefactor src/mypackage --dry-run
|
|
126
|
+
|
|
127
|
+
# skip vendored code, keep its data files
|
|
128
|
+
pyrefactor . -x "vendor/*"
|
|
129
|
+
|
|
130
|
+
# include the tests directory that is skipped by default
|
|
131
|
+
pyrefactor . --include "tests/*"
|
|
132
|
+
|
|
133
|
+
# format the result and drop comments
|
|
134
|
+
pyrefactor . --format --formatter ruff --no-keep-comments
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Configuration
|
|
138
|
+
|
|
139
|
+
`pyproject.toml` next to the source (or `--config PATH`):
|
|
140
|
+
|
|
141
|
+
```toml
|
|
142
|
+
[tool.pyrefactor]
|
|
143
|
+
exclude = ["tests/*", "iwgfyrkugf.py"]
|
|
144
|
+
include = []
|
|
145
|
+
entry = []
|
|
146
|
+
imports = "prune"
|
|
147
|
+
format = false
|
|
148
|
+
copy_data = true
|
|
149
|
+
data_glob = ["*.json"]
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
CLI values win where they are explicit.
|
|
153
|
+
|
|
154
|
+
## Architecture
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
cli.py → core.py orchestrates
|
|
158
|
+
→ discovery.py (SourceFile + DataFile, pyproject awareness)
|
|
159
|
+
→ parser.py (tree-sitter, parallel, TYPE_CHECKING flattening, __all__ detection)
|
|
160
|
+
→ resolver.py (content-hash dedupe, collision renames, imports, ordering input)
|
|
161
|
+
→ ordering.py (topo sort, deterministic tie-breaks)
|
|
162
|
+
→ writer.py (single consolidated file)
|
|
163
|
+
→ imports.py (import merging/sorting: `_private` names first)
|
|
164
|
+
logging via logging_setup.py
|
|
165
|
+
models.py — picklable dataclasses (Block, ModuleInfo, SourceFile, DataFile, ImportSpec, Occ)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
The parser only emits `ModuleInfo`/`Block`/`Occ`, so worker payloads stay
|
|
169
|
+
picklable and the resolver never touches tree-sitter nodes.
|
|
170
|
+
|
|
171
|
+
## Known limits
|
|
172
|
+
|
|
173
|
+
- Renaming is name-based: parameters and class bodies are scoped, other locals
|
|
174
|
+
are not (a full scope analysis is future work).
|
|
175
|
+
- `__file__`/`__name__`/`__package__` semantics change in a single file
|
|
176
|
+
(warning logged; `importlib.resources` users may need `files()`).
|
|
177
|
+
- Guarded imports (`try/except ImportError`, `if sys.platform …`) are kept
|
|
178
|
+
verbatim and may have side effects.
|
|
179
|
+
- Dynamic `importlib`/`__import__` dependencies are warned about, not resolved.
|
|
180
|
+
|
|
181
|
+
## Development
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
python -m venv .venv && .venv/bin/pip install -e .[dev]
|
|
185
|
+
.venv/bin/python -m pytest tests -q
|
|
186
|
+
.venv/bin/ruff check src tests
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
# repo address
|
|
190
|
+
|
|
191
|
+
i4147/refactor_utils
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "refactor_utils"
|
|
7
|
+
version = "2.0.0"
|
|
8
|
+
description = "Consolidate a small/medium Python module, library or project into a single, dependency-ordered file."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"loguru",
|
|
13
|
+
"tree-sitter",
|
|
14
|
+
"tree_sitter_python",
|
|
15
|
+
]
|
|
16
|
+
[project.scripts]
|
|
17
|
+
pyrefactor = "refactor_utils.cli:main"
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
format = ["black"]
|
|
21
|
+
progress = ["tqdm"]
|
|
22
|
+
dev = ["pytest", "ruff"]
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.packages.find]
|
|
25
|
+
where = ["src"]
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.package-data]
|
|
28
|
+
refactor_utils = ["data/*", "data/**/*"]
|
|
29
|
+
|
|
30
|
+
[tool.pyrefactor]
|
|
31
|
+
# Example config — can be overridden per-project; this is for refactor_utils itself
|
|
32
|
+
exclude = ["tests/*", "test_*.py", "iwgfyrkugf.py"]
|
|
33
|
+
include = []
|
|
34
|
+
entry = []
|
|
35
|
+
imports = "prune"
|
|
36
|
+
format = false
|
|
37
|
+
copy_data = true
|
|
38
|
+
|
|
39
|
+
[tool.black]
|
|
40
|
+
line-length = 88
|
|
41
|
+
|
|
42
|
+
[tool.ruff]
|
|
43
|
+
line-length = 88
|