Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private AwsPythonDependency() {}
*/
public static final PythonDependency SMITHY_AWS_CORE = new PythonDependency(
"smithy_aws_core",
"~=0.1.0",
"~=0.2.0",
PythonDependency.Type.DEPENDENCY,
false);
}
2 changes: 1 addition & 1 deletion codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

allprojects {
group = "software.amazon.smithy.python"
version = "0.0.1"
version = "0.1.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ private void generateService(PythonWriter writer) {
}
}

writer.addDependency(SmithyPythonDependency.SMITHY_CORE);
writer.addImport("smithy_core.retries", "RetryStrategyResolver");
writer.write("""
def __init__(self, config: $1T | None = None, plugins: list[$2T] | None = None):
self._config = config or $1T()
Expand All @@ -95,6 +97,8 @@ def __init__(self, config: $1T | None = None, plugins: list[$2T] | None = None):

for plugin in client_plugins:
plugin(self._config)

self._retry_strategy_resolver = RetryStrategyResolver()
""", configSymbol, pluginSymbol, writer.consumer(w -> writeDefaultPlugins(w, defaultPlugins)));

var topDownIndex = TopDownIndex.of(model);
Expand Down Expand Up @@ -168,8 +172,7 @@ private void writeSharedOperationInit(PythonWriter writer, OperationShape operat
writer.write("""
:param plugins: A list of callables that modify the configuration dynamically.
Changes made by these plugins only apply for the duration of the operation
execution and will not affect any other operation invocations.
""");
execution and will not affect any other operation invocations.""");

});

Expand All @@ -188,6 +191,8 @@ private void writeSharedOperationInit(PythonWriter writer, OperationShape operat
writer.addImport("smithy_core.types", "TypedProperties");
writer.addImport("smithy_core.aio.client", "RequestPipeline");
writer.addImport("smithy_core.exceptions", "ExpectationNotMetError");
writer.addImport("smithy_core.retries", "RetryStrategyOptions");
writer.addImport("smithy_core.interfaces.retries", "RetryStrategy");
writer.addStdlibImport("copy", "deepcopy");

writer.write("""
Expand All @@ -201,6 +206,24 @@ private void writeSharedOperationInit(PythonWriter writer, OperationShape operat
plugin(config)
if config.protocol is None or config.transport is None:
raise ExpectationNotMetError("protocol and transport MUST be set on the config to make calls.")

# Resolve retry strategy from config
if isinstance(config.retry_strategy, RetryStrategy):
retry_strategy = config.retry_strategy
elif isinstance(config.retry_strategy, RetryStrategyOptions):
retry_strategy = await self._retry_strategy_resolver.resolve_retry_strategy(
options=config.retry_strategy
)
elif config.retry_strategy is None:
retry_strategy = await self._retry_strategy_resolver.resolve_retry_strategy(
options=RetryStrategyOptions()
)
else:
raise TypeError(
f"retry_strategy must be RetryStrategy, RetryStrategyOptions, or None, "
f"got {type(config.retry_strategy).__name__}"
)

pipeline = RequestPipeline(
protocol=config.protocol,
transport=config.transport
Expand All @@ -213,7 +236,7 @@ raise ExpectationNotMetError("protocol and transport MUST be set on the config t
auth_scheme_resolver=config.auth_scheme_resolver,
supported_auth_schemes=config.auth_schemes,
endpoint_resolver=config.endpoint_resolver,
retry_strategy=config.retry_strategy,
retry_strategy=retry_strategy,
)
""", writer.consumer(w -> writeDefaultPlugins(w, defaultPlugins)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ def __init__(self, request: HTTPRequest):
class $3L:
""\"An asynchronous HTTP client solely for testing purposes.""\"

TIMEOUT_EXCEPTIONS = ()

def __init__(self, *, client_config: HTTPClientConfiguration | None = None):
self._client_config = client_config

Expand All @@ -644,6 +646,8 @@ async def send(
class $4L:
""\"An asynchronous HTTP client solely for testing purposes.""\"

TIMEOUT_EXCEPTIONS = ()

def __init__(
self,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class SmithyPythonDependency {
*/
public static final PythonDependency SMITHY_CORE = new PythonDependency(
"smithy_core",
"~=0.1.0",
"~=0.2.0",
Type.DEPENDENCY,
false);

Expand All @@ -33,7 +33,7 @@ public final class SmithyPythonDependency {
*/
public static final PythonDependency SMITHY_HTTP = new PythonDependency(
"smithy_http",
"~=0.2.0",
"~=0.3.0",
Type.DEPENDENCY,
false);

Expand All @@ -60,7 +60,7 @@ public final class SmithyPythonDependency {
*/
public static final PythonDependency SMITHY_JSON = new PythonDependency(
"smithy_json",
"~=0.1.0",
"~=0.2.0",
Type.DEPENDENCY,
false);

Expand All @@ -69,7 +69,7 @@ public final class SmithyPythonDependency {
*/
public static final PythonDependency SMITHY_AWS_EVENT_STREAM = new PythonDependency(
"smithy_aws_event_stream",
"~=0.1.0",
"~=0.2.0",
Type.DEPENDENCY,
false);

Expand All @@ -78,7 +78,7 @@ public final class SmithyPythonDependency {
*/
public static final PythonDependency SMITHY_AWS_CORE = new PythonDependency(
"smithy_aws_core",
"~=0.1.0",
"~=0.2.0",
Type.DEPENDENCY,
false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Locale;
import java.util.TreeSet;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.model.knowledge.EventStreamIndex;
import software.amazon.smithy.model.knowledge.ServiceIndex;
Expand Down Expand Up @@ -54,17 +55,20 @@ public final class ConfigGenerator implements Runnable {
ConfigProperty.builder()
.name("retry_strategy")
.type(Symbol.builder()
.name("RetryStrategy")
.namespace("smithy_core.interfaces.retries", ".")
.addDependency(SmithyPythonDependency.SMITHY_CORE)
.name("RetryStrategy | RetryStrategyOptions")
.addReference(Symbol.builder()
.name("RetryStrategy")
.namespace("smithy_core.interfaces.retries", ".")
.addDependency(SmithyPythonDependency.SMITHY_CORE)
.build())
.addReference(Symbol.builder()
.name("RetryStrategyOptions")
.namespace("smithy_core.retries", ".")
.addDependency(SmithyPythonDependency.SMITHY_CORE)
.build())
.build())
.documentation("The retry strategy for issuing retry tokens and computing retry delays.")
.nullable(false)
.initialize(writer -> {
writer.addDependency(SmithyPythonDependency.SMITHY_CORE);
writer.addImport("smithy_core.retries", "SimpleRetryStrategy");
writer.write("self.retry_strategy = retry_strategy or SimpleRetryStrategy()");
})
.documentation(
"The retry strategy or options for configuring retry behavior. Can be either a configured RetryStrategy or RetryStrategyOptions to create one.")
.build(),
ConfigProperty.builder()
.name("endpoint_uri")
Expand Down Expand Up @@ -331,6 +335,11 @@ private void generateConfig(GenerationContext context, PythonWriter writer) {
}

var finalProperties = List.copyOf(properties);
final String serviceId = context.settings()
.service(context.model())
.getTrait(ServiceTrait.class)
.map(ServiceTrait::getSdkId)
.orElse(context.settings().service().getName());
writer.pushState(new ConfigSection(finalProperties));
writer.addStdlibImport("dataclasses", "dataclass");
writer.write("""
Expand All @@ -345,14 +354,11 @@ def __init__(
*,
${C|}
):
\"""Constructor.

${C|}
\"""
${C|}
""",
configSymbol.getName(),
context.settings().service().getName(),
serviceId,
writer.consumer(w -> writePropertyDeclarations(w, finalProperties)),
writer.consumer(w -> writeInitParams(w, finalProperties)),
writer.consumer(w -> documentProperties(w, finalProperties)),
Expand All @@ -376,17 +382,20 @@ private void writeInitParams(PythonWriter writer, Collection<ConfigProperty> pro
}

private void documentProperties(PythonWriter writer, Collection<ConfigProperty> properties) {
var iter = properties.iterator();
while (iter.hasNext()) {
var property = iter.next();
var docs = writer.formatDocs(String.format(":param %s: %s", property.name(), property.documentation()));
writer.writeDocs(() -> {
var iter = properties.iterator();
writer.write("\nConstructor.\n");
while (iter.hasNext()) {
var property = iter.next();
var docs = writer.formatDocs(String.format(":param %s: %s", property.name(), property.documentation()));

if (iter.hasNext()) {
docs += "\n";
}

if (iter.hasNext()) {
docs += "\n";
writer.write(docs);
}

writer.write(docs);
}
});
}

private void initializeProperties(PythonWriter writer, Collection<ConfigProperty> properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

private final GenerationContext context;

Expand All @@ -31,6 +34,7 @@ public void run() {
.stream()
.map(Paths::get)
.filter(path -> !path.getParent().equals(context.fileManifest().getBaseDir()))
.filter(path -> PACKAGE_DIRECTORIES.contains(path.getName(0).toString()))
.collect(Collectors.groupingBy(Path::getParent, Collectors.toSet()));
for (var entry : directories.entrySet()) {
var initPath = entry.getKey().resolve("__init__.py");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ private void writeProperties() {
${?useField}\
field(${?sensitive}repr=False, ${/sensitive}${defaultKey:L}=${defaultValue:L})\
${/useField}
${?docs}""\"${docs:L}""\"${/docs}""", memberName, symbolProvider.toSymbol(member));
${?docs}""\"${docs:L}""\"${/docs}
""", memberName, symbolProvider.toSymbol(member));
writer.popState();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public PythonWriter writeDocs(Runnable runnable) {
pushState();
writeInline("\"\"\"");
runnable.run();
trimTrailingWhitespaces();
ensureNewline();
write("\"\"\"");
popState();
return this;
Expand All @@ -143,6 +145,35 @@ public PythonWriter writeDocs(String docs) {
return this;
}

/**
* Trims all trailing whitespace from the writer buffer.
*
* @return Returns the writer.
*/
public PythonWriter trimTrailingWhitespaces() {
// Disable the writer formatting config to ensure toString()
// returns the unmodified state of the underlying StringBuilder
trimBlankLines(-1);
trimTrailingSpaces(false);

String current = super.toString();
int end = current.length() - 1;
while (end >= 0 && Character.isWhitespace(current.charAt(end))) {
end--;
}

String trailing = current.substring(end + 1);
if (!trailing.isEmpty()) {
unwrite(trailing);
}

// Re-enable the formatting config
trimBlankLines();
trimTrailingSpaces(true);

return this;
}

private static final int MAX_LINE_LENGTH = CodegenUtils.MAX_PREFERRED_LINE_LENGTH - 8;

/**
Expand Down
8 changes: 4 additions & 4 deletions codegen/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[versions]
junit5 = "6.0.0"
smithy = "1.63.0"
junit5 = "6.0.1"
smithy = "1.64.0"
test-logger-plugin = "4.0.0"
spotbugs = "6.0.22"
spotless = "8.0.0"
spotless = "8.1.0"
smithy-gradle-plugins = "1.3.0"
dep-analysis = "3.4.0"
dep-analysis = "3.4.1"
jsoup = "1.21.2"
commonmark = "0.17.0"

Expand Down
20 changes: 20 additions & 0 deletions packages/smithy-aws-core/.changes/0.2.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"changes": [
{
"type": "dependency",
"description": "Bump `smithy-json` from `~=0.1.0` to `~=0.2.0`."
},
{
"type": "dependency",
"description": "Bump `smithy-core` from `~=0.1.0` to `~=0.2.0`."
},
{
"type": "dependency",
"description": "Bump `smithy-aws-event-stream` from `~=0.1.0` to `~=0.2.0`."
},
{
"type": "dependency",
"description": "Bump `smithy-http` from `~=0.2.0` to `~=0.3.0`."
}
]
}
8 changes: 8 additions & 0 deletions packages/smithy-aws-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.2.0

### Dependencies
* Bump `smithy-json` from `~=0.1.0` to `~=0.2.0`.
* Bump `smithy-core` from `~=0.1.0` to `~=0.2.0`.
* Bump `smithy-aws-event-stream` from `~=0.1.0` to `~=0.2.0`.
* Bump `smithy-http` from `~=0.2.0` to `~=0.3.0`.

## v0.1.1

### Dependencies
Expand Down
Loading
Loading