otBase: add iterSubTables method to iterate over all BaseTables

can be useful to traverse a tree of otTables
This commit is contained in:
Cosimo Lupo 2022-03-17 12:53:06 +00:00
parent 7f1e5e1fc7
commit 64dc37fc01

View File

@ -851,6 +851,16 @@ class BaseTable(object):
return self.__dict__ == other.__dict__
def iterSubTables(self):
for conv in self.getConverters():
value = getattr(self, conv.name, None)
if value is None:
continue
if isinstance(value, BaseTable):
yield value
elif isinstance(value, list):
yield from (v for v in value if isinstance(v, BaseTable))
class FormatSwitchingBaseTable(BaseTable):
@ -862,6 +872,13 @@ class FormatSwitchingBaseTable(BaseTable):
return NotImplemented
def getConverters(self):
try:
fmt = self.Format
except AttributeError:
# some FormatSwitchingBaseTables (e.g. Coverage) no longer have 'Format'
# attribute after fully decompiled, only gain one in preWrite before being
# recompiled.
return []
return self.converters.get(self.Format, [])
def getConverterByName(self, name):