Refactor: Attribute.class used instead of helper methods in PythonSer… (#1475)

* Refactor: Attribute.class used instead of helper methods in PythonServerAttributeUtils

Helper methods in PythonServerAttributeUtils have been deprecated in favor of using Attribute.class directly

Co-authored-by: Matteo Bigoi <1781140+crisidev@users.noreply.github.com>

* Smithy Python: PyException removed from enum

Smithy enums do not have ErrorTrait so there is no need to check and generate code for pyo3::exceptions::PyException

Co-authored-by: Fahad Zubair <fahadzub@amazon.com>
Co-authored-by: Matteo Bigoi <1781140+crisidev@users.noreply.github.com>
This commit is contained in:
Fahad Zubair 2022-06-21 17:17:21 +01:00 committed by GitHub
parent a91b813e43
commit a3c7902d7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 49 deletions

View File

@ -1,43 +0,0 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.server.python.smithy.generators
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.traits.ErrorTrait
import software.amazon.smithy.rust.codegen.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.rustlang.asType
import software.amazon.smithy.rust.codegen.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerCargoDependency
import software.amazon.smithy.rust.codegen.util.hasTrait
/**
* This module contains utilities to render PyO3 attributes.
*
* TODO(https://github.com/awslabs/smithy-rs/issues/1465): Switch to `Attribute.Custom` and get rid of this class.
*/
private val codegenScope = arrayOf(
"pyo3" to PythonServerCargoDependency.PyO3.asType(),
)
// Renders #[pyo3::pyclass] attribute, inheriting from `Exception` if the shape has the `ErrorTrait` attached.
fun RustWriter.renderPyClass(shape: Shape) {
if (shape.hasTrait<ErrorTrait>()) {
rustTemplate("##[#{pyo3}::pyclass(extends = #{pyo3}::exceptions::PyException)]", *codegenScope)
} else {
rustTemplate("##[#{pyo3}::pyclass]", *codegenScope)
}
}
// Renders #[pyo3::pymethods] attribute.
fun RustWriter.renderPyMethods() {
rustTemplate("##[#{pyo3}::pymethods]", *codegenScope)
}
// Renders #[pyo3(get, set)] attribute.
fun RustWriter.renderPyGetterSetter() {
rustTemplate("##[#{pyo3}(get, set)]", *codegenScope)
}

View File

@ -8,8 +8,11 @@ package software.amazon.smithy.rust.codegen.server.python.smithy.generators
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.traits.EnumTrait
import software.amazon.smithy.rust.codegen.rustlang.Attribute
import software.amazon.smithy.rust.codegen.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.rustlang.asType
import software.amazon.smithy.rust.codegen.rustlang.rust
import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerCargoDependency
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerEnumGenerator
import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider
@ -28,19 +31,26 @@ class PythonServerEnumGenerator(
runtimeConfig: RuntimeConfig,
) : ServerEnumGenerator(model, symbolProvider, writer, shape, enumTrait, runtimeConfig) {
private val pyo3Symbols = listOf(PythonServerCargoDependency.PyO3.asType())
override fun render() {
writer.renderPyClass(shape)
renderPyClass()
super.render()
renderPyO3Methods()
}
private fun renderPyClass() {
Attribute.Custom("pyo3::pyclass", symbols = pyo3Symbols).render(writer)
}
override fun renderFromForStr() {
writer.renderPyClass(shape)
renderPyClass()
super.renderFromForStr()
}
private fun renderPyO3Methods() {
writer.renderPyMethods()
Attribute.Custom("pyo3::pymethods", symbols = pyo3Symbols).render(writer)
writer.rust(
"""
impl $enumName {

View File

@ -10,12 +10,15 @@ import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.ErrorTrait
import software.amazon.smithy.rust.codegen.rustlang.Attribute
import software.amazon.smithy.rust.codegen.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.rustlang.Writable
import software.amazon.smithy.rust.codegen.rustlang.asType
import software.amazon.smithy.rust.codegen.rustlang.render
import software.amazon.smithy.rust.codegen.rustlang.rust
import software.amazon.smithy.rust.codegen.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.rustlang.writable
import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerCargoDependency
import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider
import software.amazon.smithy.rust.codegen.smithy.generators.StructureGenerator
import software.amazon.smithy.rust.codegen.smithy.rustType
@ -33,20 +36,25 @@ open class PythonServerStructureGenerator(
private val shape: StructureShape
) : StructureGenerator(model, symbolProvider, writer, shape) {
private val pyo3Symbols = listOf(PythonServerCargoDependency.PyO3.asType())
override fun renderStructure() {
writer.renderPyClass(shape)
if (shape.hasTrait<ErrorTrait>())
Attribute.Custom("pyo3::pyclass(extends = pyo3::exceptions::PyException)", symbols = pyo3Symbols).render(writer)
else
Attribute.Custom("pyo3::pyclass", symbols = pyo3Symbols).render(writer)
super.renderStructure()
renderPyO3Methods()
}
override fun renderStructureMember(writer: RustWriter, member: MemberShape, memberName: String, memberSymbol: Symbol) {
writer.renderPyGetterSetter()
Attribute.Custom("pyo3(get, set)", symbols = pyo3Symbols).render(writer)
super.renderStructureMember(writer, member, memberName, memberSymbol)
}
private fun renderPyO3Methods() {
if (shape.hasTrait<ErrorTrait>() || accessorMembers.isNotEmpty()) {
writer.renderPyMethods()
Attribute.Custom("pyo3::pymethods", symbols = pyo3Symbols).render(writer)
writer.rustTemplate(
"""
impl $name {