Skip to content
Open
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
46 changes: 36 additions & 10 deletions scripts/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ def __init__(self, service_name: str, service: Service):
self.resources: Dict[str, ResourceType] = service.resources
self.properties: Dict[str, PropertyType] = service.properties
self.property_validators = service.property_validators
self.statement_found = False

def generate(self, file=None) -> str:
"""Generated the troposphere source code."""
Expand All @@ -428,6 +427,8 @@ def generate(self, file=None) -> str:
if self._walk_for_tags():
code.append("from . import Tags")
code.append("from . import PropsDictType")
code.append("from . import Template")
code.append("from typing import Optional")

if not stub:
# Output imports for commonly used validators
Expand Down Expand Up @@ -526,13 +527,6 @@ def _build_tree(
# prevent recursive properties
if property_name == name:
continue
# This is a horrible hack to fix an indirect recursion issue in WAFv2
# XXX - Need to implement a more durable solution to detect recursion
if self.service_name == "wafv2" and property_name == "Statement":
if self.statement_found:
continue
else:
self.statement_found = True

try:
child = self._build_tree(
Expand Down Expand Up @@ -676,10 +670,13 @@ def _get_type(self, value: Property, stub=False):

if value.type == "List":
if value.item_type:
return "[%s]" % value.item_type
if stub:
return "'list[%s]'" % value.item_type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list[…] instead of typing.List[…] is only available in Python 3.9+

else:
return "[%s]" % value.item_type
else:
if stub:
return "[%s]" % map_stub_type.get(
return "'list[%s]'" % map_stub_type.get(
value.primitive_item_type, value.primitive_item_type
)
else:
Expand Down Expand Up @@ -722,7 +719,36 @@ def _generate_class(
code.append(' """')
code.append("")

if len(property_type.properties) == 0:
code.append(
" def __init__(self, title: Optional[str], template: Optional[Template] = None, validation: bool = True):"
)
code.append(" super().__init__(title, template, validation,)")
else:
# Output the __init__ function
code.append(
" def __init__(self, title: Optional[str], template: Optional[Template] = None, validation: bool = True, *,"
)
for key, value in sorted(property_type.properties.items()):
if property_validator and key in property_validator:
value_type = property_validator[key]
elif value.type == "Tags":
value_type = value.type
if value.primitive_type == "Json":
value_type = "dict"
else:
value_type = self._get_type(value, stub=True)

code.append(f" {key}: {value_type} = None,")
code.append("):")

code.append(" super().__init__(title, template, validation,")
for key, value in sorted(property_type.properties.items()):
code.append(f" {key}={key}, ")
code.append(" )")

# Output the props dict
code.append("")
code.append(" props: PropsDictType = {")
for key, value in sorted(property_type.properties.items()):
if property_validator and key in property_validator:
Expand Down
3 changes: 2 additions & 1 deletion troposphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ def __init__(

# Now that it is initialized, populate it with the kwargs
for k, v in kwargs.items():
self.__setattr__(k, v)
if v is not None:
self.__setattr__(k, v)

self.add_to_template()

Expand Down