basstler-rules 4.2.0__py3-none-any.whl
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.
- basstler/code_modification/__init__.py +7 -0
- basstler/code_modification/actions.py +171 -0
- basstler/code_modification/exceptions.py +127 -0
- basstler/code_modification/file_actions.py +141 -0
- basstler/code_modification/module_relocation.py +256 -0
- basstler/code_modification/patterns.py +86 -0
- basstler/code_modification/positions.py +39 -0
- basstler/code_modification/references.py +45 -0
- basstler/code_modification/source_file.py +27 -0
- basstler/code_modification/source_tree.py +107 -0
- basstler/code_modification/specification.py +41 -0
- basstler/code_modification/syntax.py +209 -0
- basstler/code_rules/__init__.py +4 -0
- basstler/code_rules/address_building.py +94 -0
- basstler/code_rules/bash_by_name.py +103 -0
- basstler/code_rules/checker.py +163 -0
- basstler/code_rules/command_line.py +165 -0
- basstler/code_rules/commands.py +47 -0
- basstler/code_rules/docstring_presence.py +255 -0
- basstler/code_rules/exceptions.py +34 -0
- basstler/code_rules/fixes.py +307 -0
- basstler/code_rules/instructions.py +80 -0
- basstler/code_rules/locale_encoding.py +315 -0
- basstler/code_rules/package_boundaries.py +344 -0
- basstler/code_rules/prose_length.py +220 -0
- basstler/code_rules/rule.py +158 -0
- basstler/code_rules/rule_module.py +84 -0
- basstler/code_rules/rules/__init__.py +3 -0
- basstler/code_rules/rules/code_quality.py +848 -0
- basstler/code_rules/rules/project.py +171 -0
- basstler/code_rules/subprocess_function.py +26 -0
- basstler/code_rules/syntax_tree_checker.py +38 -0
- basstler/code_rules/text_file_usage.py +146 -0
- basstler/code_rules/tree_removal.py +64 -0
- basstler/templates/agents.md.jinja +16 -0
- basstler_rules-4.2.0.dist-info/METADATA +15 -0
- basstler_rules-4.2.0.dist-info/RECORD +40 -0
- basstler_rules-4.2.0.dist-info/WHEEL +4 -0
- basstler_rules-4.2.0.dist-info/licenses/LICENSE +202 -0
- basstler_rules-4.2.0.dist-info/licenses/NOTICE +13 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reversible changes to Python source: specifications of what one place must become, and
|
|
3
|
+
the actions and plans that make it so and undo it exactly.
|
|
4
|
+
|
|
5
|
+
The design of specifications, actions and plans is taken from krrood's code generation
|
|
6
|
+
:cite:`krrood`, which was never released.
|
|
7
|
+
"""
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Actions that change a module or a source tree and know their exact inverse, and plans
|
|
3
|
+
composing them.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from abc import ABC, abstractmethod
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
|
|
11
|
+
import libcst
|
|
12
|
+
from libcst.codemod import CodemodContext
|
|
13
|
+
from libcst.codemod.visitors import AddImportsVisitor
|
|
14
|
+
from libcst.metadata import CodeRange
|
|
15
|
+
|
|
16
|
+
from basstler.code_modification.exceptions import ActionTargetMissingError
|
|
17
|
+
from basstler.code_modification.positions import PositionedModule
|
|
18
|
+
|
|
19
|
+
# %% actions
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Action[Target](ABC):
|
|
23
|
+
"""
|
|
24
|
+
A change to a target that can be undone exactly.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def apply(self, target: Target) -> AppliedAction[Target]:
|
|
29
|
+
"""
|
|
30
|
+
:param target: The target to change, which is left as it is.
|
|
31
|
+
:return: The changed target, and the action that turns it back into *target*.
|
|
32
|
+
:raises ActionTargetMissingError: If *target* lacks what the action changes.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class AppliedAction[Target]:
|
|
38
|
+
"""
|
|
39
|
+
The outcome of applying an action.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
result: Target
|
|
43
|
+
"""
|
|
44
|
+
The changed target.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
inverse: Action[Target]
|
|
48
|
+
"""
|
|
49
|
+
The action that turns :attr:`result` back into the target the action was applied to.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True)
|
|
54
|
+
class NodeReplacement(Action[libcst.Module]):
|
|
55
|
+
"""
|
|
56
|
+
Replaces the node at one location by another.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
location: CodeRange
|
|
60
|
+
"""
|
|
61
|
+
The code range of :attr:`original` in the module the action is applied to.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
original: libcst.CSTNode
|
|
65
|
+
"""
|
|
66
|
+
The node replaced, which the module must hold at :attr:`location`.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
replacement: libcst.CSTNode
|
|
70
|
+
"""
|
|
71
|
+
The node put in its place.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
def apply(self, target: libcst.Module) -> AppliedAction[libcst.Module]:
|
|
75
|
+
positions = PositionedModule(target).positions
|
|
76
|
+
original = next(
|
|
77
|
+
(
|
|
78
|
+
node
|
|
79
|
+
for node, code_range in positions.items()
|
|
80
|
+
if code_range == self.location and node.deep_equals(self.original)
|
|
81
|
+
),
|
|
82
|
+
None,
|
|
83
|
+
)
|
|
84
|
+
if original is None:
|
|
85
|
+
raise ActionTargetMissingError(self.location, type(self.original).__name__)
|
|
86
|
+
replaced = libcst.ensure_type(
|
|
87
|
+
target.deep_replace(original, self.replacement), libcst.Module
|
|
88
|
+
)
|
|
89
|
+
replacement_location = PositionedModule(replaced).positions[self.replacement]
|
|
90
|
+
return AppliedAction(
|
|
91
|
+
replaced,
|
|
92
|
+
NodeReplacement(replacement_location, self.replacement, self.original),
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# %% imports
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class ModuleTransformation(Action, ABC):
|
|
100
|
+
"""
|
|
101
|
+
Changes a whole module, which the action that restores the module undoes.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
@abstractmethod
|
|
105
|
+
def transform(self, module: libcst.Module) -> libcst.Module:
|
|
106
|
+
"""
|
|
107
|
+
:param module: The module the action is applied to.
|
|
108
|
+
:return: The module with the change made.
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
def apply(self, module: libcst.Module) -> AppliedAction:
|
|
112
|
+
return AppliedAction(self.transform(module), ModuleReplacement(module))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@dataclass(frozen=True)
|
|
116
|
+
class ModuleReplacement(ModuleTransformation):
|
|
117
|
+
"""
|
|
118
|
+
Replaces a module with another one.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
replacement: libcst.Module
|
|
122
|
+
"""
|
|
123
|
+
The module that takes the place of the one the action is applied to.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
def transform(self, module: libcst.Module) -> libcst.Module:
|
|
127
|
+
return self.replacement
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@dataclass(frozen=True)
|
|
131
|
+
class ImportInsertion(ModuleTransformation):
|
|
132
|
+
"""
|
|
133
|
+
Imports a class in a module, merged into an import of its module where there is one
|
|
134
|
+
and left out where the module imports the class already.
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
imported: type
|
|
138
|
+
"""
|
|
139
|
+
The class to import.
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
def transform(self, module: libcst.Module) -> libcst.Module:
|
|
143
|
+
context = CodemodContext()
|
|
144
|
+
AddImportsVisitor.add_needed_import(
|
|
145
|
+
context, self.imported.__module__, self.imported.__name__
|
|
146
|
+
)
|
|
147
|
+
return AddImportsVisitor(context).transform_module(module)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
# %% plans
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@dataclass(frozen=True)
|
|
154
|
+
class ActionPlan[Target](Action[Target]):
|
|
155
|
+
"""
|
|
156
|
+
Actions applied in order, each to the target the one before it produced, and undone
|
|
157
|
+
in the reverse order.
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
actions: tuple[Action[Target], ...] = ()
|
|
161
|
+
"""
|
|
162
|
+
The actions, in the order they are applied.
|
|
163
|
+
"""
|
|
164
|
+
|
|
165
|
+
def apply(self, target: Target) -> AppliedAction[Target]:
|
|
166
|
+
inverses: list[Action[Target]] = []
|
|
167
|
+
for action in self.actions:
|
|
168
|
+
applied = action.apply(target)
|
|
169
|
+
target = applied.result
|
|
170
|
+
inverses.append(applied.inverse)
|
|
171
|
+
return AppliedAction(target, ActionPlan(tuple(reversed(inverses))))
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The errors raised while applying an action to a module or a source tree.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from enum import StrEnum
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from libcst.metadata import CodeRange
|
|
12
|
+
|
|
13
|
+
from basstler.exceptions import DataclassException
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class ActionTargetMissingError(DataclassException, LookupError):
|
|
18
|
+
"""
|
|
19
|
+
Raised when the module an action is applied to has no node where the action expects
|
|
20
|
+
the node it changes.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
location: CodeRange
|
|
24
|
+
"""
|
|
25
|
+
Where the action expected the node.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
node_type: str
|
|
29
|
+
"""
|
|
30
|
+
The name of the expected node's type.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def error_message(self) -> str:
|
|
34
|
+
start, end = self.location.start, self.location.end
|
|
35
|
+
return (
|
|
36
|
+
f"no {self.node_type} from {start.line}:{start.column} to "
|
|
37
|
+
f"{end.line}:{end.column} matches the node the action changes"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
def suggest_correction(self) -> str:
|
|
41
|
+
return (
|
|
42
|
+
"Apply the action to the module it was made for, or to the module the "
|
|
43
|
+
"action before it in its plan produced."
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class FileExpectation(StrEnum):
|
|
48
|
+
"""
|
|
49
|
+
What a file action expects of the file it changes.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
PRESENT = "present"
|
|
53
|
+
"""
|
|
54
|
+
The tree holds the file.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
ABSENT = "absent"
|
|
58
|
+
"""
|
|
59
|
+
The tree holds no file at the path.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
CONTENT = "holding the content the action names"
|
|
63
|
+
"""
|
|
64
|
+
The tree holds the file with exactly the content the action names.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@dataclass
|
|
69
|
+
class FileActionTargetError(DataclassException, LookupError):
|
|
70
|
+
"""
|
|
71
|
+
Raised when the tree a file action is applied to does not hold its file as the action
|
|
72
|
+
expects.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
path: Path
|
|
76
|
+
"""
|
|
77
|
+
The file the action changes, relative to the tree's root.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
expectation: FileExpectation
|
|
81
|
+
"""
|
|
82
|
+
What the action expected of the file.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
def error_message(self) -> str:
|
|
86
|
+
return f"{self.path.as_posix()} is not {self.expectation}"
|
|
87
|
+
|
|
88
|
+
def suggest_correction(self) -> str:
|
|
89
|
+
return (
|
|
90
|
+
"Apply the action to the tree it was made for, or to the tree the action "
|
|
91
|
+
"before it in its plan produced."
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@dataclass
|
|
96
|
+
class UnrewritableImportError(DataclassException, ValueError):
|
|
97
|
+
"""
|
|
98
|
+
Raised when a relocation would have to rewrite an import whose rewrite changes the
|
|
99
|
+
name the statement binds.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
path: Path
|
|
103
|
+
"""
|
|
104
|
+
The module holding the import, relative to the tree's root.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
line: int
|
|
108
|
+
"""
|
|
109
|
+
The line the import starts on, counted from one.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
module: str
|
|
113
|
+
"""
|
|
114
|
+
The dotted name of the moved module the statement imports.
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
def error_message(self) -> str:
|
|
118
|
+
return (
|
|
119
|
+
f"{self.path.as_posix()}:{self.line} imports {self.module} by its dotted "
|
|
120
|
+
"name, which binds a name a moved module no longer has"
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
def suggest_correction(self) -> str:
|
|
124
|
+
return (
|
|
125
|
+
f"Import it as 'from <package> import <module>' or 'import {self.module} "
|
|
126
|
+
"as <name>' before relocating it."
|
|
127
|
+
)
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Actions that change the files of a source tree, each with its exact inverse.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import libcst
|
|
11
|
+
|
|
12
|
+
from basstler.code_modification.actions import Action, AppliedAction
|
|
13
|
+
from basstler.code_modification.exceptions import (
|
|
14
|
+
FileActionTargetError,
|
|
15
|
+
FileExpectation,
|
|
16
|
+
)
|
|
17
|
+
from basstler.code_modification.source_tree import SourceTree
|
|
18
|
+
|
|
19
|
+
# %% moving, creating and deleting files
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class FileMove(Action[SourceTree]):
|
|
24
|
+
"""
|
|
25
|
+
Moves one file to a path no file of the tree holds.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
source: Path
|
|
29
|
+
"""
|
|
30
|
+
The file moved, relative to the tree's root.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
destination: Path
|
|
34
|
+
"""
|
|
35
|
+
Where it goes, relative to the tree's root.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def apply(self, target: SourceTree) -> AppliedAction[SourceTree]:
|
|
39
|
+
if self.source not in target.files:
|
|
40
|
+
raise FileActionTargetError(self.source, FileExpectation.PRESENT)
|
|
41
|
+
if self.destination in target.files:
|
|
42
|
+
raise FileActionTargetError(self.destination, FileExpectation.ABSENT)
|
|
43
|
+
return AppliedAction(
|
|
44
|
+
target.with_files(
|
|
45
|
+
{self.destination: target.files[self.source]},
|
|
46
|
+
(self.source,),
|
|
47
|
+
(self.destination,) if self.source in target.executable else (),
|
|
48
|
+
),
|
|
49
|
+
FileMove(self.destination, self.source),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True)
|
|
54
|
+
class FileCreation(Action[SourceTree]):
|
|
55
|
+
"""
|
|
56
|
+
Creates one file at a path no file of the tree holds.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
path: Path
|
|
60
|
+
"""
|
|
61
|
+
The file created, relative to the tree's root.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
content: bytes
|
|
65
|
+
"""
|
|
66
|
+
What the file holds.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
is_executable: bool = False
|
|
70
|
+
"""
|
|
71
|
+
Whether the file is executable.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
def apply(self, target: SourceTree) -> AppliedAction[SourceTree]:
|
|
75
|
+
if self.path in target.files:
|
|
76
|
+
raise FileActionTargetError(self.path, FileExpectation.ABSENT)
|
|
77
|
+
return AppliedAction(
|
|
78
|
+
target.with_files(
|
|
79
|
+
{self.path: self.content},
|
|
80
|
+
(),
|
|
81
|
+
(self.path,) if self.is_executable else (),
|
|
82
|
+
),
|
|
83
|
+
FileDeletion(self.path, self.content),
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass(frozen=True)
|
|
88
|
+
class FileDeletion(Action[SourceTree]):
|
|
89
|
+
"""
|
|
90
|
+
Deletes one file holding exactly the content named.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
path: Path
|
|
94
|
+
"""
|
|
95
|
+
The file deleted, relative to the tree's root.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
content: bytes
|
|
99
|
+
"""
|
|
100
|
+
What the file must hold, and what the inverse creates it with again.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
def apply(self, target: SourceTree) -> AppliedAction[SourceTree]:
|
|
104
|
+
if target.files.get(self.path) != self.content:
|
|
105
|
+
raise FileActionTargetError(self.path, FileExpectation.CONTENT)
|
|
106
|
+
return AppliedAction(
|
|
107
|
+
target.with_files({}, (self.path,)),
|
|
108
|
+
FileCreation(self.path, self.content, self.path in target.executable),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# %% changing a module of the tree
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@dataclass(frozen=True)
|
|
116
|
+
class ModuleEdit(Action[SourceTree]):
|
|
117
|
+
"""
|
|
118
|
+
Applies an action to one Python module of the tree.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
path: Path
|
|
122
|
+
"""
|
|
123
|
+
The module changed, relative to the tree's root; read and written as UTF-8.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
action: Action[libcst.Module]
|
|
127
|
+
"""
|
|
128
|
+
The change made to it.
|
|
129
|
+
"""
|
|
130
|
+
|
|
131
|
+
def apply(self, target: SourceTree) -> AppliedAction[SourceTree]:
|
|
132
|
+
if self.path not in target.files:
|
|
133
|
+
raise FileActionTargetError(self.path, FileExpectation.PRESENT)
|
|
134
|
+
module = libcst.parse_module(target.files[self.path].decode(encoding="utf-8"))
|
|
135
|
+
applied = self.action.apply(module)
|
|
136
|
+
return AppliedAction(
|
|
137
|
+
target.with_files(
|
|
138
|
+
{self.path: applied.result.code.encode(encoding="utf-8")}
|
|
139
|
+
),
|
|
140
|
+
ModuleEdit(self.path, applied.inverse),
|
|
141
|
+
)
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Moving modules within a source tree together with every import the move would break.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from collections.abc import Iterator, Mapping
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from functools import cached_property
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import libcst
|
|
13
|
+
from libcst.helpers import get_full_name_for_node
|
|
14
|
+
from libcst.metadata import CodeRange
|
|
15
|
+
|
|
16
|
+
from basstler.code_modification.actions import (
|
|
17
|
+
Action,
|
|
18
|
+
ActionPlan,
|
|
19
|
+
AppliedAction,
|
|
20
|
+
NodeReplacement,
|
|
21
|
+
)
|
|
22
|
+
from basstler.code_modification.exceptions import UnrewritableImportError
|
|
23
|
+
from basstler.code_modification.file_actions import FileMove, ModuleEdit
|
|
24
|
+
from basstler.code_modification.positions import PositionedModule
|
|
25
|
+
from basstler.code_modification.source_tree import SourceTree
|
|
26
|
+
|
|
27
|
+
# %% module names
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class ImportRoots:
|
|
32
|
+
"""
|
|
33
|
+
The directories of a tree that modules are imported from, as on ``sys.path``.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
directories: tuple[Path, ...]
|
|
37
|
+
"""
|
|
38
|
+
Each directory, relative to the tree's root; a file is named from the deepest one
|
|
39
|
+
holding it.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def module_name(self, path: Path) -> str | None:
|
|
43
|
+
"""
|
|
44
|
+
:param path: A file of the tree, relative to its root.
|
|
45
|
+
:return: The dotted name *path* is imported by; none for a file that is not a
|
|
46
|
+
Python module under one of :attr:`directories`.
|
|
47
|
+
"""
|
|
48
|
+
roots = [root for root in self.directories if path.is_relative_to(root)]
|
|
49
|
+
if path.suffix != ".py" or not roots:
|
|
50
|
+
return None
|
|
51
|
+
root = max(roots, key=lambda directory: len(directory.parts))
|
|
52
|
+
parts = path.relative_to(root).with_suffix("").parts
|
|
53
|
+
return ".".join(parts[:-1] if parts[-1] == "__init__" else parts)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class ModuleLocation:
|
|
58
|
+
"""
|
|
59
|
+
Where one module of the tree lies, before and after a relocation.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
old_name: str
|
|
63
|
+
"""
|
|
64
|
+
The dotted name it is imported by before the relocation.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
new_name: str
|
|
68
|
+
"""
|
|
69
|
+
The dotted name it is imported by after the relocation.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
is_package: bool
|
|
73
|
+
"""
|
|
74
|
+
Whether the module is a package's ``__init__``, from which relative imports start.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def base_of(self, statement: libcst.ImportFrom, importer_name: str) -> str:
|
|
78
|
+
"""
|
|
79
|
+
:param statement: An import in this module.
|
|
80
|
+
:param importer_name: This module's name, before or after the relocation.
|
|
81
|
+
:return: The dotted name of the module *statement* imports from.
|
|
82
|
+
"""
|
|
83
|
+
written = get_full_name_for_node(statement.module) if statement.module else ""
|
|
84
|
+
level = len(statement.relative)
|
|
85
|
+
if not level:
|
|
86
|
+
return written
|
|
87
|
+
package = importer_name.split(".")
|
|
88
|
+
if not self.is_package:
|
|
89
|
+
package = package[:-1]
|
|
90
|
+
parts = package[: len(package) - level + 1]
|
|
91
|
+
return ".".join([*parts, written] if written else parts)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# %% the relocation
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@dataclass(frozen=True)
|
|
98
|
+
class ModuleRelocation(Action[SourceTree]):
|
|
99
|
+
"""
|
|
100
|
+
Moves files of a tree and rewrites, as an absolute import, every import that would
|
|
101
|
+
no longer reach the module it imported.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
moves: tuple[FileMove, ...]
|
|
105
|
+
"""
|
|
106
|
+
The files moved.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
import_roots: ImportRoots
|
|
110
|
+
"""
|
|
111
|
+
The directories the tree's modules are imported from, before and after the moves.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
def apply(self, target: SourceTree) -> AppliedAction[SourceTree]:
|
|
115
|
+
rewrites = tuple(self.import_rewrites(target))
|
|
116
|
+
return ActionPlan((*self.moves, *rewrites)).apply(target)
|
|
117
|
+
|
|
118
|
+
@cached_property
|
|
119
|
+
def destinations(self) -> Mapping[Path, Path]:
|
|
120
|
+
"""
|
|
121
|
+
Where each moved file goes, by where it was.
|
|
122
|
+
"""
|
|
123
|
+
return {move.source: move.destination for move in self.moves}
|
|
124
|
+
|
|
125
|
+
def locations(self, tree: SourceTree) -> dict[str, ModuleLocation]:
|
|
126
|
+
"""
|
|
127
|
+
:param tree: The tree before the moves.
|
|
128
|
+
:return: Where each module of *tree* lies before and after the moves, by its
|
|
129
|
+
name before them.
|
|
130
|
+
"""
|
|
131
|
+
locations = {}
|
|
132
|
+
for path in tree.files:
|
|
133
|
+
old_name = self.import_roots.module_name(path)
|
|
134
|
+
new_name = self.import_roots.module_name(self.destinations.get(path, path))
|
|
135
|
+
if old_name is not None and new_name is not None:
|
|
136
|
+
locations[old_name] = ModuleLocation(
|
|
137
|
+
old_name, new_name, path.name == "__init__.py"
|
|
138
|
+
)
|
|
139
|
+
return locations
|
|
140
|
+
|
|
141
|
+
def import_rewrites(self, tree: SourceTree) -> Iterator[ModuleEdit]:
|
|
142
|
+
"""
|
|
143
|
+
:param tree: The tree before the moves.
|
|
144
|
+
:return: An edit of each module, at its path after the moves, rewriting the
|
|
145
|
+
imports the moves break.
|
|
146
|
+
"""
|
|
147
|
+
locations = self.locations(tree)
|
|
148
|
+
for path, content in tree.files.items():
|
|
149
|
+
importer = locations.get(self.import_roots.module_name(path) or "")
|
|
150
|
+
if importer is None:
|
|
151
|
+
continue
|
|
152
|
+
module = PositionedModule(
|
|
153
|
+
libcst.parse_module(content.decode(encoding="utf-8"))
|
|
154
|
+
)
|
|
155
|
+
replacements = sorted(
|
|
156
|
+
ImportRewriter(path, importer, locations).replacements(module),
|
|
157
|
+
key=lambda replacement: (
|
|
158
|
+
replacement.location.start.line,
|
|
159
|
+
replacement.location.start.column,
|
|
160
|
+
),
|
|
161
|
+
reverse=True,
|
|
162
|
+
)
|
|
163
|
+
if replacements:
|
|
164
|
+
yield ModuleEdit(
|
|
165
|
+
self.destinations.get(path, path), ActionPlan(tuple(replacements))
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
# %% rewriting one module's imports
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@dataclass(frozen=True)
|
|
173
|
+
class ImportRewriter:
|
|
174
|
+
"""
|
|
175
|
+
Finds the imports of one module that a relocation breaks, and their rewrites.
|
|
176
|
+
"""
|
|
177
|
+
|
|
178
|
+
path: Path
|
|
179
|
+
"""
|
|
180
|
+
The module's path before the relocation, relative to the tree's root.
|
|
181
|
+
"""
|
|
182
|
+
|
|
183
|
+
importer: ModuleLocation
|
|
184
|
+
"""
|
|
185
|
+
Where the module lies before and after the relocation.
|
|
186
|
+
"""
|
|
187
|
+
|
|
188
|
+
locations: Mapping[str, ModuleLocation]
|
|
189
|
+
"""
|
|
190
|
+
Where every module of the tree lies, by its name before the relocation.
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
def replacements(self, module: PositionedModule) -> Iterator[NodeReplacement]:
|
|
194
|
+
"""
|
|
195
|
+
:param module: The module, as it is before the relocation.
|
|
196
|
+
:return: A replacement of each import of it that the relocation breaks.
|
|
197
|
+
:raises UnrewritableImportError: If a broken import binds a name no rewrite of
|
|
198
|
+
the statement keeps.
|
|
199
|
+
"""
|
|
200
|
+
for node, location in module.positions.items():
|
|
201
|
+
if isinstance(node, libcst.ImportFrom):
|
|
202
|
+
yield from self.from_import_replacements(node, location)
|
|
203
|
+
if isinstance(node, libcst.Import):
|
|
204
|
+
yield from self.import_replacements(node, location)
|
|
205
|
+
|
|
206
|
+
def from_import_replacements(
|
|
207
|
+
self, statement: libcst.ImportFrom, location: CodeRange
|
|
208
|
+
) -> Iterator[NodeReplacement]:
|
|
209
|
+
"""
|
|
210
|
+
:param statement: A ``from`` import of the module.
|
|
211
|
+
:param location: Where it lies in the module.
|
|
212
|
+
:return: Its rewrite as an absolute import of where its module went, when it no
|
|
213
|
+
longer reaches that module where it is written.
|
|
214
|
+
"""
|
|
215
|
+
imported = self.locations.get(
|
|
216
|
+
self.importer.base_of(statement, self.importer.old_name)
|
|
217
|
+
)
|
|
218
|
+
if imported is None:
|
|
219
|
+
return
|
|
220
|
+
if self.importer.base_of(statement, self.importer.new_name) == (
|
|
221
|
+
imported.new_name
|
|
222
|
+
):
|
|
223
|
+
return
|
|
224
|
+
yield NodeReplacement(
|
|
225
|
+
location,
|
|
226
|
+
statement,
|
|
227
|
+
statement.with_changes(
|
|
228
|
+
module=libcst.parse_expression(imported.new_name), relative=()
|
|
229
|
+
),
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
def import_replacements(
|
|
233
|
+
self, statement: libcst.Import, location: CodeRange
|
|
234
|
+
) -> Iterator[NodeReplacement]:
|
|
235
|
+
"""
|
|
236
|
+
:param statement: A plain import in the module.
|
|
237
|
+
:param location: Where it lies in the module.
|
|
238
|
+
:return: Its rewrite naming each moved module it imports where it went.
|
|
239
|
+
:raises UnrewritableImportError: If it imports a moved module without an alias.
|
|
240
|
+
"""
|
|
241
|
+
names = []
|
|
242
|
+
for alias in statement.names:
|
|
243
|
+
name = get_full_name_for_node(alias.name)
|
|
244
|
+
imported = self.locations.get(name)
|
|
245
|
+
if imported is None or imported.new_name == name:
|
|
246
|
+
names.append(alias)
|
|
247
|
+
continue
|
|
248
|
+
if alias.asname is None:
|
|
249
|
+
raise UnrewritableImportError(self.path, location.start.line, name)
|
|
250
|
+
names.append(
|
|
251
|
+
alias.with_changes(name=libcst.parse_expression(imported.new_name))
|
|
252
|
+
)
|
|
253
|
+
if names != list(statement.names):
|
|
254
|
+
yield NodeReplacement(
|
|
255
|
+
location, statement, statement.with_changes(names=names)
|
|
256
|
+
)
|