Skip to content

Commit

Permalink
Add global item support to the python zapxml parser. (project-chip#34603
Browse files Browse the repository at this point in the history
)

* ZapXML support for global items

* Fix typo

* Add a  unit test for enum too

* Restyled by autopep8

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
andy31415 and restyled-commits authored Jul 29, 2024
1 parent 275c8c9 commit b6cfeac
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
52 changes: 52 additions & 0 deletions scripts/py_matter_idl/matter_idl/test_zapxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,36 @@ def testFabricScopedAndSensitive(self):
qualities=StructQuality.FABRIC_SCOPED)],
)]))

def testGlobalEnum(self):
idl = XmlToIdl('''<?xml version="1.0"?>
<configurator>
<enum name="One" type="ENUM8">
<item value="3" name="Three" />
</enum>
<enum name="Two" type="ENUM8">
<item value="100" name="Big" />
<item value="2000" name="Bigger" />
</enum>
</configurator>
''')
e1 = Enum(
name='One',
base_type="ENUM8",
entries=[
ConstantEntry(name="Three", code=3),
]
)
e2 = Enum(
name='Two',
base_type="ENUM8",
entries=[
ConstantEntry(name="Big", code=100),
ConstantEntry(name="Bigger", code=2000),
]
)
self.assertEqual(idl, Idl(global_enums=[e1, e2]))

def testEnum(self):
idl = XmlToIdl('''<?xml version="1.0"?>
<configurator>
Expand Down Expand Up @@ -308,6 +338,28 @@ def testFeatures(self):
Cluster(name='TestFeatures', code=20, bitmaps=[bitmap])
])),

def testGlobalStruct(self):
idl = XmlToIdl('''<?xml version="1.0"?>
<configurator>
<struct name="SomeStruct" isFabricScoped="true">
<item name="FirstMember" type="int16u" />
<item name="SecondMember" type="int32u" />
</struct>
</configurator>
''')
struct = Struct(
name='SomeStruct',
qualities=StructQuality.FABRIC_SCOPED,
fields=[
Field(data_type=DataType(name='int16u'),
code=0, name='FirstMember'),
Field(data_type=DataType(name='int32u'),
code=1, name='SecondMember')
]
)
self.assertEqual(idl, Idl(global_structs=[struct]))

def testStruct(self):
idl = XmlToIdl('''<?xml version="1.0"?>
<configurator>
Expand Down
13 changes: 5 additions & 8 deletions scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def FinalizeProcessing(self, idl: Idl):
# - inside a cluster if a code exists
# - inside top level if no codes were associated
if not self._cluster_codes:
LOGGER.error('Struct %s has no cluster codes' % self._struct.name)
idl.global_structs.append(self._struct)
return

for code in self._cluster_codes:
Expand Down Expand Up @@ -270,9 +270,9 @@ def GetNextProcessor(self, name, attrs):

def FinalizeProcessing(self, idl: Idl):
if not self._cluster_codes:
LOGGER.error("Found enum without a cluster code: %s" %
(self._enum.name))
idl.global_enums.append(self._enum)
return

found = set()
for c in idl.clusters:
if c.code in self._cluster_codes:
Expand Down Expand Up @@ -313,14 +313,11 @@ def GetNextProcessor(self, name, attrs):
return BaseHandler(self.context)

def FinalizeProcessing(self, idl: Idl):
# We have two choices of adding an enum:
# We have two choices of adding a bitmap:
# - inside a cluster if a code exists
# - inside top level if a code does not exist
if not self._cluster_codes:
# Log only instead of critical, as not our XML is well formed.
# For example at the time of writing this, SwitchFeature in switch-cluster.xml
# did not have a code associated with it.
LOGGER.error("Bitmap %r has no cluster codes" % self._bitmap)
idl.global_bitmaps.append(self._bitmap)
return

for code in self._cluster_codes:
Expand Down

0 comments on commit b6cfeac

Please sign in to comment.