Skip to content

Commit

Permalink
More straighforward support for overriding children in subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Aug 28, 2020
1 parent 1f704c1 commit 76696cd
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions tw2/core/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def _collect_base_children(meta, bases):

def __new__(meta, name, bases, dct):
if name != 'Widget' and 'children' not in dct:
# Children not provided,
# build them from class attributes.
new_children = []
for d, v in list(dct.items()):
if isinstance(v, type) and \
Expand All @@ -65,13 +67,16 @@ def __new__(meta, name, bases, dct):
new_children.append((v, d))
del dct[d]

children = meta._collect_base_children(bases)
base_children = meta._collect_base_children(bases)
new_children = sorted(new_children, key=lambda t: t[0]._seq)
children.extend(
direct_children = [
hasattr(v, 'id') and v or v(id=d) for v, d in new_children
)
if children:
dct['children'] = children
]
direct_children_ids = set([c.compound_id for c in direct_children])
dct['children'] = [
# Do not include children that have been overwritten in a subclass.
c for c in base_children if c.compound_id not in direct_children_ids
] + direct_children

widget = super(WidgetMeta, meta).__new__(meta, name, bases, dct)

Expand Down Expand Up @@ -590,11 +595,7 @@ def post_define(cls):
cls._sub_compound = not getattr(cls, 'id', None)
if not hasattr(cls, 'children'):
return

super_children_ids = []
if hasattr(super(cls, cls), 'children'):
super_children_ids = [c.id for c in super(cls, cls).children if hasattr(c, 'id')]


joined_cld = []
for c in cls.children:
if not isinstance(c, type) or not issubclass(c, Widget):
Expand All @@ -605,10 +606,9 @@ def post_define(cls):
for c in cls.children_deep():
if getattr(c, 'id', None):
if c.id in ids:
if c.id not in super_children_ids:
raise core.WidgetError("Duplicate id '%s'" % c.id)
joined_cld.pop([x.id for x in joined_cld].index(c.id))
raise core.WidgetError("Duplicate id '%s'" % c.id)
ids.add(c.id)

cls.children = WidgetBunch(joined_cld)
cls.keyed_children = [
c.id for c in joined_cld
Expand Down Expand Up @@ -733,7 +733,7 @@ def children_deep(cls):
for c in getattr(cls, 'children', []):
for cc in c.children_deep():
yield cc


class RepeatingWidgetBunchCls(object):

Expand Down

0 comments on commit 76696cd

Please sign in to comment.