Skip to content

Commit c5edb84

Browse files
committed
Add children to CDIMemo. Remove empty groups (offset only) from the tree in the GUI.
1 parent 6421959 commit c5edb84

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

examples/examples_gui.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ def _gui(self, parent):
462462

463463
self.cdi_row += 1
464464
self.network = None
465-
self.cdi_form = None
465+
self.cdi_form = None # type: CDIForm|None
466+
# ^ CDIForm or other XMLDataProcessor subclass
466467

467468
self.example_tab = ttk.Frame(self.notebook)
468469
self.example_tab.columnconfigure(index=0, weight=1)
@@ -492,7 +493,7 @@ def _gui(self, parent):
492493

493494
def setupNetwork(self):
494495
self.network = OpenLCBNetwork(self.getValue('localNodeID'))
495-
self.cdi_form = CDIForm(self.network.canLink, self.cdi_tab) # XMLDataProcessor subclass
496+
self.cdi_form = CDIForm(self.network.canLink, self.cdi_tab)
496497
self.cdi_form.setStatusCallback(self.setStatus)
497498
# ^ formerly OpenLCBNetwork() subclass
498499
# ^ CDIForm has ttk.Treeview etc.

examples/tkexamples/cdiform.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ def _onPopScope(self, cm: CDIMemo):
238238
parts.append(f'offset="{offset}"')
239239
self.debug(*parts)
240240
logger.debug(self.indent() + "Done ignoring {}".format(cm.tag))
241+
if cm.iid:
242+
# If in tree, check if it is an empty group (no name so remove it).
243+
children = self._treeview.get_children(cm.iid)
244+
if (cm.getTag() == "group") and (not children):
245+
# self._treeview.detach(cm.iid) # remove but don't destroy
246+
self._treeview.delete(cm.iid) # detach and destroy
241247
return cm
242248

243249
def write(self, *args, **kwargs):

openlcb/cdimemo.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import xml.etree.ElementTree
22

3-
from typing import Optional, Union
3+
from typing import List, Optional, Union
44

55
from openlcb.message import Message
66

@@ -14,6 +14,9 @@ class CDIMemo:
1414
of previous elements and offset of this element).
1515
content (str|None): string content (collected by parser from
1616
between the start and end tag).
17+
children (List[CDIMemo]): List of children (Therefore not
18+
complete until onPopScope, but you can also check if .end is
19+
True in asynchronous scopes).
1720
done (bool): If True, downloadCDI is finished. Though document
1821
itself may be incomplete if 'error' is also set, stop
1922
tracking status of downloadCDI regardless.
@@ -45,6 +48,7 @@ def __init__(self, tag: Union[str, None] = None,
4548
self.message: Union[Message, None] = None # type: Message|None
4649
self.iid = None # type: str|None
4750
self.address = None # type: int|None
51+
self.children = [] # type: List[CDIMemo]
4852

4953
def getTag(self):
5054
if self.element is None:

openlcb/xmldataprocessor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ def endElement(self, name: str):
430430
cm.parent = self._tag_stack[-1]
431431
cm.element = top_el
432432
# else parent & element should already have been set in startElement
433+
if cm.parent is not None:
434+
cm.parent.children.append(cm)
433435
_ = self.checkDone(cm)
434436
cm.content = self._flushCharBuffer()
435437
self.onPopScope(cm)

0 commit comments

Comments
 (0)