Skip to content

Commit e2ac986

Browse files
committed
Add PrettyResolver implementation and its own core module
1 parent 68cb4f2 commit e2ac986

File tree

20 files changed

+1057
-161
lines changed

20 files changed

+1057
-161
lines changed

.idea/compiler.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/spring-boot-python-executor-common/src/main/java/io/w4t3rcs/python/resolver/PythonResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public interface PythonResolver {
3838
/**
3939
* Resolves the given Python script by applying transformations or expression resolution.
4040
*
41-
* @param script the Python script content to resolve (non-{@code null})
42-
* @param arguments a map of variables for resolution (non-{@code null}, can be empty)
41+
* @param pythonScript the Python script content to resolve (non-{@code null})
42+
* @param arguments a map of variables for resolution (non-{@code null}, can be empty)
4343
* @return the transformed Python script with resolved expressions (never {@code null})
4444
*/
45-
PythonScript resolve(PythonScript script, Map<String, Object> arguments);
45+
PythonScript resolve(PythonScript pythonScript, Map<String, Object> arguments);
4646
}

common/spring-boot-python-executor-common/src/main/java/io/w4t3rcs/python/script/PythonScript.java

Lines changed: 43 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@
55
import java.util.ArrayList;
66
import java.util.List;
77
import java.util.Objects;
8-
import java.util.function.BiConsumer;
9-
import java.util.function.Consumer;
10-
import java.util.function.Function;
11-
import java.util.regex.MatchResult;
12-
import java.util.regex.Matcher;
13-
import java.util.regex.Pattern;
148
import java.util.stream.Collectors;
159
import java.util.stream.Stream;
1610

1711
public class PythonScript {
1812
public static final String FILE_FORMAT = ".py";
1913
public static final int START_INDEX = 0;
14+
private final PythonScriptBuilder builder;
2015
private final List<PythonImportLine> importLines;
2116
private final List<String> codeLines;
2217
private final boolean isFile;
@@ -25,6 +20,7 @@ public class PythonScript {
2520
private String body;
2621

2722
public PythonScript() {
23+
this.builder = new PythonScriptBuilder(this);
2824
this.importLines = new ArrayList<>();
2925
this.codeLines = new ArrayList<>();
3026
this.isFile = false;
@@ -36,158 +32,79 @@ public PythonScript(String script) {
3632
}
3733

3834
public PythonScript(List<PythonImportLine> importLines, List<String> codeLines, String script) {
35+
this.builder = new PythonScriptBuilder(this);
3936
this.importLines = importLines;
4037
this.codeLines = codeLines;
4138
this.source = script;
39+
this.body = null;
4240
if (script.endsWith(FILE_FORMAT)) {
4341
this.isFile = true;
4442
} else {
4543
this.isFile = false;
46-
this.appendAll(script);
44+
this.builder.appendAll(script);
4745
}
4846
}
4947

50-
public <T> PythonScript iterate(Iterable<T> iterable, Consumer<T> action) {
51-
return this.iterate(iterable, action, true);
52-
}
53-
54-
public <T> PythonScript iterate(Iterable<T> iterable, Consumer<T> action, boolean condition) {
55-
if (condition) iterable.forEach(action);
56-
return this;
57-
}
58-
59-
public PythonScript perform(Runnable action) {
60-
return this.perform(action, true);
61-
}
62-
63-
public PythonScript perform(Runnable action, boolean condition) {
64-
if (condition) action.run();
65-
return this;
66-
}
67-
68-
public PythonScript appendAll(String script) {
69-
script.lines().forEach(line -> {
70-
if (line.matches(PythonImportLine.IMPORT_REGEX)) {
71-
this.appendImport(line);
72-
} else {
73-
this.appendCode(line);
74-
}
75-
});
76-
return this;
77-
}
78-
79-
public PythonScript removeAll(String regex, int start, int end, Consumer<String> actionOnRemove) {
80-
this.replaceAll(regex, start, end, group -> {
81-
actionOnRemove.accept(group);
82-
return "";
83-
});
84-
return this;
85-
}
86-
87-
public PythonScript replaceAll(String regex, int start, int end, Function<String, String> groupFunction) {
88-
this.replaceAll(regex, matchResult -> {
89-
String group = matchResult.group();
90-
String substring = group.substring(start, group.length() - end);
91-
return groupFunction.apply(substring);
92-
});
93-
return this;
48+
protected void clearBody() {
49+
this.body = null;
9450
}
9551

96-
public PythonScript replaceAll(String regex, int start, int end, BiConsumer<String, StringBuilder> resultBuilderFunction) {
97-
this.replaceAll(regex, matchResult -> {
98-
String group = matchResult.group();
99-
String substring = group.substring(start, group.length() - end);
100-
StringBuilder result = new StringBuilder();
101-
resultBuilderFunction.accept(substring, result);
102-
return result.toString();
103-
});
104-
return this;
52+
public int getImportsSize() {
53+
return this.getImportLines().size();
10554
}
10655

107-
public PythonScript replaceAll(String regex, Function<MatchResult, String> function) {
108-
this.body = null;
109-
Pattern pattern = Pattern.compile(regex);
110-
List<String> codeLines = this.getCodeLines();
111-
for (int i = 0; i < codeLines.size(); i++) {
112-
String codeLine = codeLines.get(i);
113-
Matcher matcher = pattern.matcher(codeLine);
114-
codeLine = matcher.replaceAll(function);
115-
this.setCode(codeLine, i);
116-
}
117-
return this;
56+
public int getCodeSize() {
57+
return this.getCodeLines().size();
11858
}
11959

120-
public PythonScript appendImport(String importLine) {
121-
this.body = null;
60+
public boolean containsImport(String importLine) {
12261
PythonImportLine line = new PythonImportLine(importLine);
123-
if (importLines.contains(line)) return this;
124-
this.getImportLines().add(line);
125-
return this;
62+
return this.containsImport(line);
12663
}
12764

128-
public PythonScript setCode(String codeLine, int index) {
129-
this.body = null;
130-
this.getCodeLines().set(index, codeLine);
131-
return this;
65+
public boolean containsImport(PythonImportLine importLine) {
66+
return this.getImportLines().contains(importLine);
13267
}
13368

134-
public PythonScript insertCode(String codeLine, int index) {
135-
this.body = null;
136-
this.getCodeLines().add(index, codeLine);
137-
return this;
69+
public boolean containsCode(String codeLine) {
70+
return !this.isCodeEmpty() && this.getCodeLines().contains(codeLine);
13871
}
13972

140-
public PythonScript appendCode(String... codeLines) {
141-
String joined = String.join("", codeLines);
142-
this.appendCode(joined);
143-
return this;
73+
public boolean containsDeepImport(String importLine) {
74+
return this.getImportLines()
75+
.stream()
76+
.anyMatch(line -> line.getLine().contains(importLine));
14477
}
14578

146-
public PythonScript appendCode(String codeLine) {
147-
this.body = null;
148-
this.getCodeLines().add(codeLine);
149-
return this;
79+
public boolean containsDeepCode(String codeLine) {
80+
return this.getCodeLines()
81+
.stream()
82+
.anyMatch(line -> line.contains(codeLine));
15083
}
15184

152-
public PythonScript prependCode(String... codeLines) {
153-
String joined = String.join("", codeLines);
154-
this.prependCode(joined);
155-
return this;
85+
public boolean startsWithCode(String codeLine) {
86+
return !this.isCodeEmpty() && this.getCodeLines().get(START_INDEX).equals(codeLine);
15687
}
15788

158-
public PythonScript prependCode(String codeLine) {
159-
this.body = null;
160-
this.insertCode(codeLine, START_INDEX);
161-
return this;
89+
public boolean endsWithCode(String codeLine) {
90+
int lastElement = this.getCodeSize() - 1;
91+
return !this.isCodeEmpty() && this.getCodeLines().get(lastElement).equals(codeLine);
16292
}
16393

164-
public boolean isEmpty() {
94+
public boolean isImportEmpty() {
16595
return this.getCodeLines().isEmpty();
16696
}
16797

168-
public boolean containsImport(String importLine) {
169-
PythonImportLine line = new PythonImportLine(importLine);
170-
return this.getImportLines().contains(line);
171-
}
172-
173-
public boolean containsCode(String codeLine) {
174-
return this.getCodeLines().contains(codeLine);
175-
}
176-
177-
public boolean containsDeepImport(String importLine) {
178-
return this.getImportLines()
179-
.stream()
180-
.anyMatch(line -> line.getLine().contains(importLine));
98+
public boolean isCodeEmpty() {
99+
return this.getCodeLines().isEmpty();
181100
}
182101

183-
public boolean containsDeepCode(String codeLine) {
184-
return this.getCodeLines()
185-
.stream()
186-
.anyMatch(line -> line.contains(codeLine));
102+
public PythonImportLine getImport(int index) {
103+
return this.getImportLines().get(index);
187104
}
188105

189-
public boolean startsWithCode(String codeLine) {
190-
return this.getCodeLines().get(START_INDEX).equals(codeLine);
106+
public String getCodeLine(int index) {
107+
return this.getCodeLines().get(index);
191108
}
192109

193110
public int getImportIndex(String importLine) {
@@ -222,23 +139,25 @@ public String getSource() {
222139
return source;
223140
}
224141

142+
public PythonScriptBuilder getBuilder() {
143+
return builder;
144+
}
145+
225146
@Override
226147
public boolean equals(Object o) {
227148
if (!(o instanceof PythonScript that)) return false;
228149
return Objects.equals(this.getImportLines(), that.getImportLines())
229-
&& Objects.equals(this.getCodeLines(), that.getCodeLines())
230-
&& Objects.equals(this.body, that.body);
150+
&& Objects.equals(this.getCodeLines(), that.getCodeLines());
231151
}
232152

233153
@Override
234154
public int hashCode() {
235-
return Objects.hash(this.getImportLines(), this.getCodeLines(), this.body);
155+
return Objects.hash(this.getImportLines(), this.getCodeLines());
236156
}
237157

238158
@Override
239159
public String toString() {
240160
if (this.body == null || this.body.isBlank()) {
241-
if (!this.startsWithCode("")) this.prependCode("");
242161
this.body = Stream.concat(this.getImportLines().stream().map(PythonImportLine::getLine), this.getCodeLines().stream())
243162
.collect(Collectors.joining("\n"));
244163
}

0 commit comments

Comments
 (0)