From 47c9e50ec6b5813f7903754ad9e20b282c1ab800 Mon Sep 17 00:00:00 2001 From: David Lehman Date: Fri, 6 Mar 2015 12:10:13 -0600 Subject: [PATCH 04/19] Improve output of DeviceTree.__str__. Where multiple parents share a child, display the common child and ellipsize its ancestors to avoid unnecessary duplication. Also streamline slightly by passing depth instead of calculating it. --- blivet/devicetree.py | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/blivet/devicetree.py b/blivet/devicetree.py index 88112a9..f5a2f2d 100644 --- a/blivet/devicetree.py +++ b/blivet/devicetree.py @@ -988,29 +988,19 @@ class DeviceTree(object): def __str__(self): done = [] - def get_depth(device): - depth = 0 - _device = device - while True: - if _device.parents: - depth += 1 - _device = _device.parents[0] - else: - break - - return depth - - def show_subtree(root): - if root in done: - return "" - s = "%s%s\n" % (" " * get_depth(root), root) + def show_subtree(root, depth): + abbreviate_subtree = root in done + s = "%s%s\n" % (" " * depth, root) done.append(root) - for child in self.getChildren(root): - s+= show_subtree(child) + if abbreviate_subtree: + s += "%s...\n" % (" " * (depth+1),) + else: + for child in self.getChildren(root): + s += show_subtree(child, depth + 1) return s roots = [d for d in self._devices if not d.parents] tree = "" for root in roots: - tree += show_subtree(root) + tree += show_subtree(root, 0) return tree -- 1.9.3