Skip to content

Commit

Permalink
Add support for global structs/enums/bitmaps to Python codegen. (#34561)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Sep 11, 2024
1 parent 388b1a7 commit 2637921
Show file tree
Hide file tree
Showing 7 changed files with 570 additions and 496 deletions.
2 changes: 1 addition & 1 deletion src/controller/python/chip/clusters/Attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def _BuildClusterIndex():
''' Build internal cluster index for locating the corresponding cluster object by path in the future.
'''
for clusterName, obj in inspect.getmembers(sys.modules['chip.clusters.Objects']):
if ('chip.clusters.Objects' in str(obj)) and inspect.isclass(obj):
if ('chip.clusters.Objects' in str(obj)) and inspect.isclass(obj) and issubclass(obj, Cluster):
_ClusterIndex[obj.id] = obj


Expand Down
950 changes: 492 additions & 458 deletions src/controller/python/chip/clusters/Objects.py

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/controller/python/templates/partials/bitmap_def.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class {{asType label}}(IntFlag):
{{#zcl_bitmap_items}}
k{{asUpperCamelCase label}} = {{asHex mask}}
{{/zcl_bitmap_items}}
18 changes: 18 additions & 0 deletions src/controller/python/templates/partials/enum_def.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{! Takes cluster (possibly "Globals") as argument, already upper-camel-cased. }}
class {{asType label}}(MatterIntEnum):
{{#zcl_enum_items}}
k{{asUpperCamelCase label}} = {{asHex value 2}}
{{/zcl_enum_items}}
{{#unless (isInConfigList (concat cluster "::" label) "EnumsNotUsedAsTypeInXML")}}
# All received enum values that are not listed above will be mapped
# to kUnknownEnumValue. This is a helper enum value that should only
# be used by code to process how it handles receiving an unknown
# enum value. This specific value should never be transmitted.
kUnknownEnumValue = {{first_unused_enum_value mode="first_unused"}},
{{else}}
# kUnknownEnumValue intentionally not defined. This enum never goes
# through DataModel::Decode, likely because it is a part of a derived
# cluster. As a result having kUnknownEnumValue in this enum is error
# prone, and was removed. See
# src/app/common/templates/config-data.yaml.
{{/unless}}
15 changes: 15 additions & 0 deletions src/controller/python/templates/partials/struct_def.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{! Takes cluster (possibly "Globals") as argument, already upper-camel-cased. }}
@dataclass
class {{asUpperCamelCase name}}(ClusterObject):
@ChipUtility.classproperty
def descriptor(cls) -> ClusterObjectDescriptor:
return ClusterObjectDescriptor(
Fields=[
{{#zcl_struct_items}}
ClusterObjectFieldDescriptor(Label="{{ asLowerCamelCase label }}", Tag={{ fieldIdentifier }}, Type={{zapTypeToPythonClusterObjectType type ns=../cluster}}),
{{/zcl_struct_items}}
])

{{#zcl_struct_items}}
{{ asLowerCamelCase label }}: '{{zapTypeToPythonClusterObjectType type ns=../cluster}}' = {{getPythonFieldDefault type ns=../cluster}}
{{/zcl_struct_items}}
65 changes: 30 additions & 35 deletions src/controller/python/templates/python-cluster-Objects-py.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ from .ClusterObjects import (Cluster, ClusterAttributeDescriptor, ClusterCommand
ClusterObjectDescriptor, ClusterObjectFieldDescriptor)
from .Types import Nullable, NullValue

class Globals:
class Enums:
{{#zcl_enums}}
{{#if has_no_clusters}}
{{> enum_def cluster="Globals"}}

{{/if}}
{{/zcl_enums}}
class Bitmaps:
{{#zcl_bitmaps}}
{{#if has_no_clusters}}
{{! Work around https://github.com/project-chip/zap/issues/1370 and manually filter out built-in bitmap types. }}
{{#if_is_atomic label}}
{{else}}
{{> bitmap_def }}

{{/if_is_atomic}}
{{/if}}
{{/zcl_bitmaps}}
class Structs:
{{#zcl_structs}}
{{#if has_no_clusters}}
{{> struct_def cluster="Globals"}}

{{/if}}
{{/zcl_structs}}

{{#zcl_clusters}}

@dataclass
Expand Down Expand Up @@ -50,23 +77,7 @@ class {{asUpperCamelCase name}}(Cluster):
{{#first}}
class Enums:
{{/first}}
class {{asType label}}(MatterIntEnum):
{{#zcl_enum_items}}
k{{asUpperCamelCase label}} = {{asHex value 2}}
{{/zcl_enum_items}}
{{#unless (isInConfigList (concat (asUpperCamelCase ../name) "::" label) "EnumsNotUsedAsTypeInXML")}}
# All received enum values that are not listed above will be mapped
# to kUnknownEnumValue. This is a helper enum value that should only
# be used by code to process how it handles receiving and unknown
# enum value. This specific should never be transmitted.
kUnknownEnumValue = {{first_unused_enum_value mode="first_unused"}},
{{else}}
# kUnknownEnumValue intentionally not defined. This enum never goes
# through DataModel::Decode, likely because it is a part of a derived
# cluster. As a result having kUnknownEnumValue in this enum is error
# prone, and was removed. See
# src/app/common/templates/config-data.yaml.
{{/unless}}
{{> enum_def cluster=(asUpperCamelCase ../name)}}

{{#last}}
{{/last}}
Expand All @@ -75,30 +86,14 @@ class {{asUpperCamelCase name}}(Cluster):
{{#first}}
class Bitmaps:
{{/first}}
class {{asType label}}(IntFlag):
{{#zcl_bitmap_items}}
k{{asUpperCamelCase label}} = {{asHex mask}}
{{/zcl_bitmap_items}}
{{> bitmap_def }}

{{/zcl_bitmaps}}
{{#zcl_structs}}
{{#first}}
class Structs:
{{/first}}
@dataclass
class {{asUpperCamelCase name}}(ClusterObject):
@ChipUtility.classproperty
def descriptor(cls) -> ClusterObjectDescriptor:
return ClusterObjectDescriptor(
Fields=[
{{#zcl_struct_items}}
ClusterObjectFieldDescriptor(Label="{{ asLowerCamelCase label }}", Tag={{ fieldIdentifier }}, Type={{zapTypeToPythonClusterObjectType type ns=(asUpperCamelCase parent.parent.name)}}),
{{/zcl_struct_items}}
])

{{#zcl_struct_items}}
{{ asLowerCamelCase label }}: '{{zapTypeToPythonClusterObjectType type ns=(asUpperCamelCase parent.parent.name)}}' = {{getPythonFieldDefault type ns=(asUpperCamelCase parent.parent.name)}}
{{/zcl_struct_items}}
{{> struct_def cluster=(asUpperCamelCase parent.name) }}

{{/zcl_structs}}
{{#zcl_commands}}
Expand Down
12 changes: 10 additions & 2 deletions src/controller/python/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@
"path": "../../../../src/app/zap-templates/partials/clusters_header.zapt"
},
{
"name": "cluster_header",
"path": "../../../../src/app/zap-templates/partials/cluster_header.zapt"
"name": "enum_def",
"path": "partials/enum_def.zapt"
},
{
"name": "bitmap_def",
"path": "partials/bitmap_def.zapt"
},
{
"name": "struct_def",
"path": "partials/struct_def.zapt"
}
],
"templates": [
Expand Down

0 comments on commit 2637921

Please sign in to comment.