Skip to content

Commit ef085ec

Browse files
authored
Restrict init file gen to package directories and fix docstring blank lines (#596)
* Restrict init file gen to package directories * Add helper method to remove trailing spaces from docstrings
1 parent 8e7d3ee commit ef085ec

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ private void writeSharedOperationInit(PythonWriter writer, OperationShape operat
168168
writer.write("""
169169
:param plugins: A list of callables that modify the configuration dynamically.
170170
Changes made by these plugins only apply for the duration of the operation
171-
execution and will not affect any other operation invocations.
172-
""");
171+
execution and will not affect any other operation invocations.""");
173172

174173
});
175174

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/ConfigGenerator.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,7 @@ def __init__(
351351
*,
352352
${C|}
353353
):
354-
\"""Constructor.
355-
356354
${C|}
357-
\"""
358355
${C|}
359356
""",
360357
configSymbol.getName(),
@@ -382,17 +379,20 @@ private void writeInitParams(PythonWriter writer, Collection<ConfigProperty> pro
382379
}
383380

384381
private void documentProperties(PythonWriter writer, Collection<ConfigProperty> properties) {
385-
var iter = properties.iterator();
386-
while (iter.hasNext()) {
387-
var property = iter.next();
388-
var docs = writer.formatDocs(String.format(":param %s: %s", property.name(), property.documentation()));
382+
writer.writeDocs(() ->{
383+
var iter = properties.iterator();
384+
writer.write("\nConstructor.\n");
385+
while (iter.hasNext()) {
386+
var property = iter.next();
387+
var docs = writer.formatDocs(String.format(":param %s: %s", property.name(), property.documentation()));
388+
389+
if (iter.hasNext()) {
390+
docs += "\n";
391+
}
389392

390-
if (iter.hasNext()) {
391-
docs += "\n";
393+
writer.write(docs);
392394
}
393-
394-
writer.write(docs);
395-
}
395+
});
396396
}
397397

398398
private void initializeProperties(PythonWriter writer, Collection<ConfigProperty> properties) {

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/InitGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.nio.file.Path;
88
import java.nio.file.Paths;
9+
import java.util.Set;
910
import java.util.stream.Collectors;
1011
import software.amazon.smithy.python.codegen.GenerationContext;
1112
import software.amazon.smithy.utils.SmithyInternalApi;
@@ -15,6 +16,8 @@
1516
*/
1617
@SmithyInternalApi
1718
public final class InitGenerator implements Runnable {
19+
// Set of directories that need __init__.py files
20+
private static final Set<String> PACKAGE_DIRECTORIES = Set.of("src", "tests");
1821

1922
private final GenerationContext context;
2023

@@ -31,6 +34,7 @@ public void run() {
3134
.stream()
3235
.map(Paths::get)
3336
.filter(path -> !path.getParent().equals(context.fileManifest().getBaseDir()))
37+
.filter(path -> PACKAGE_DIRECTORIES.contains(path.getName(0).toString()))
3438
.collect(Collectors.groupingBy(Path::getParent, Collectors.toSet()));
3539
for (var entry : directories.entrySet()) {
3640
var initPath = entry.getKey().resolve("__init__.py");

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/StructureGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ private void writeProperties() {
242242
${?useField}\
243243
field(${?sensitive}repr=False, ${/sensitive}${defaultKey:L}=${defaultValue:L})\
244244
${/useField}
245-
${?docs}""\"${docs:L}""\"${/docs}""", memberName, symbolProvider.toSymbol(member));
245+
${?docs}""\"${docs:L}""\"${/docs}
246+
""", memberName, symbolProvider.toSymbol(member));
246247
writer.popState();
247248
}
248249
}

codegen/core/src/main/java/software/amazon/smithy/python/codegen/writer/PythonWriter.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ public PythonWriter writeDocs(Runnable runnable) {
127127
pushState();
128128
writeInline("\"\"\"");
129129
runnable.run();
130+
trimTrailingWhitespaces();
131+
ensureNewline();
130132
write("\"\"\"");
131133
popState();
132134
return this;
@@ -143,6 +145,35 @@ public PythonWriter writeDocs(String docs) {
143145
return this;
144146
}
145147

148+
/**
149+
* Trims all trailing whitespace from the writer buffer.
150+
*
151+
* @return Returns the writer.
152+
*/
153+
public PythonWriter trimTrailingWhitespaces() {
154+
// Disable the writer formatting config to ensure toString()
155+
// returns the unmodified state of the underlying StringBuilder
156+
trimBlankLines(-1);
157+
trimTrailingSpaces(false);
158+
159+
String current = super.toString();
160+
int end = current.length() - 1;
161+
while (end >= 0 && Character.isWhitespace(current.charAt(end))) {
162+
end--;
163+
}
164+
165+
String trailing = current.substring(end + 1);
166+
if (!trailing.isEmpty()) {
167+
unwrite(trailing);
168+
}
169+
170+
// Re-enable the formatting config
171+
trimBlankLines();
172+
trimTrailingSpaces(true);
173+
174+
return this;
175+
}
176+
146177
private static final int MAX_LINE_LENGTH = CodegenUtils.MAX_PREFERRED_LINE_LENGTH - 8;
147178

148179
/**

0 commit comments

Comments
 (0)