Commits

Nozomu Kaneko  committed 6abf0bd

Added command line option to keep network order in diagram.
It can be used to adjust vertical order of nodes.

  • Participants
  • Parent commits 672b737
  • Branches patches

Comments (0)

Files changed (2)

File src/nwdiag/builder.py

 
 
 class DiagramLayoutManager:
-    def __init__(self, diagram):
+    def __init__(self, diagram, keep_network_order=False):
         self.diagram = diagram
 
         self.coordinates = []
+        self.should_sort_networks = not keep_network_order
 
     def run(self):
         self.do_layout()
 
             return cmp(len(n1), len(n2))
 
-        for i in range(len(self.diagram.networks) - 1):
-            parent = self.diagram.networks[i]
-            others = self.diagram.networks[i + 1:]
-            nodes = [n for n in self.diagram.nodes  if parent in n.networks]
+        if self.should_sort_networks:
+            for i in range(len(self.diagram.networks) - 1):
+                parent = self.diagram.networks[i]
+                others = self.diagram.networks[i + 1:]
+                nodes = [n for n in self.diagram.nodes  if parent in n.networks]
 
-            others.sort(compare, reverse=True)
+                others.sort(compare, reverse=True)
 
-            if self.diagram.networks[i + 1] != others[0]:
-                self.diagram.networks.remove(others[0])
-                self.diagram.networks.insert(i + 1, others[0])
+                if self.diagram.networks[i + 1] != others[0]:
+                    self.diagram.networks.remove(others[0])
+                    self.diagram.networks.insert(i + 1, others[0])
 
         for i, network in enumerate(self.diagram.networks):
             network.xy = XY(0, i)
 
 class ScreenNodeBuilder:
     @classmethod
-    def build(klass, tree):
+    def build(klass, tree, keep_network_order=False):
         DiagramNode.clear()
         DiagramEdge.clear()
         NodeGroup.clear()
         Network.clear()
 
         diagram = DiagramTreeBuilder().build(tree)
-        DiagramLayoutManager(diagram).run()
+        DiagramLayoutManager(diagram, keep_network_order).run()
 
         return diagram

File src/nwdiag/command.py

                  help='Drop into debugger on exception')
     p.add_option('-T', dest='type', default='PNG',
                  help='Output diagram as TYPE format')
+    p.add_option('-N', '--keep-network-order', dest='keep_network_order',
+                 action='store_true', default=False,
+                 help='Do not sort networks')
     options, args = p.parse_args()
 
     if len(args) == 0:
         sys.stderr.write("ERROR: %s\n" % e)
         return
 
-    diagram = ScreenNodeBuilder.build(tree)
+    diagram = ScreenNodeBuilder.build(tree, options.keep_network_order)
 
     draw = DiagramDraw.DiagramDraw(options.type, diagram, outfile,
                                    font=fontpath, antialias=options.antialias)