graph.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # vim: set fileencoding=utf-8 :
  2. def generate_graph(root, node_type, separator=' ', verbose=False):
  3. """
  4. Return a text-based, utf-8 graph of a tree-like structure. Each tree node
  5. is represented by a length-2 list. If a node has an attribute called
  6. ``title``, that attribute will be called. That way, the node can return a
  7. specific title, otherwise ``+`` is used.
  8. >>> l0, l1 = Leaf(0), Leaf(1)
  9. >>> n0 = Node('+', l0, l1)
  10. >>> l2 = Leaf(2)
  11. >>> print generate_graph(n0, Node)
  12. +
  13. ╭┴╮
  14. 0 1
  15. >>> n1 = Node('-', l2)
  16. >>> print generate_graph(n1, Node)
  17. -
  18. 2
  19. >>> n2 = Node('*', n0, n1)
  20. >>> print generate_graph(n2, Node)
  21. *
  22. ╭─┴╮
  23. + -
  24. ╭┴╮ │
  25. 0 1 2
  26. """
  27. node_width = {}
  28. separator_len = len(separator)
  29. def calculate_width(node):
  30. title = node.title()
  31. title_len = len(title)
  32. # Leaves do not have children and therefore the length of its title is
  33. # the width of the leaf.
  34. if not isinstance(node, node_type):
  35. node_width[node] = title_len
  36. return title_len
  37. width = 0
  38. for child in node:
  39. width += calculate_width(child)
  40. # Add a separator between each node (thus n - 1 separators).
  41. width += separator_len * (len(node) - 1)
  42. # Odd numbers of children should be minus 1, since the middle child
  43. # can be placed directly below the parent. With even numbers, there
  44. # is no middle child, so the space below the parent cannot be used.
  45. #if len(node) % 2 == 1:
  46. # width -= 1
  47. # If the title of the node is wider than the sum of its children, the
  48. # title's width should be used.
  49. width = max(title_len, width)
  50. # print 'width of "%s":' % node.title(), width
  51. node_width[node] = width
  52. return width
  53. def format_lines(node):
  54. if not isinstance(node, node_type):
  55. # Leaf titles do not need to be centered, since the parent will
  56. # center those lines. And if there are no parents, the entire graph
  57. # consists of a single leaf, so in that case there still is no
  58. # reason to center it.
  59. return [node.title()]
  60. # At least one child, otherwise it would be a leaf.
  61. assert node[0]
  62. child_lines = [format_lines(child) for child in node]
  63. max_height = max(map(len, child_lines))
  64. # Assert that all child boxes are of equal height
  65. for lines in child_lines:
  66. additional_line = separator * len(lines[0])
  67. lines += [additional_line for i in range(max_height - len(lines))]
  68. assert len(child_lines[0]) == max_height
  69. from copy import deepcopy
  70. result = deepcopy(child_lines[0])
  71. for lines in child_lines[1:]:
  72. assert len(lines) == max_height
  73. for i, line in enumerate(lines):
  74. result[i] += separator + line
  75. line_width = node_width[node]
  76. box_widths = [len(lines[0]) for lines in child_lines]
  77. node_len = len(node)
  78. middle_node = int(node_len / 2)
  79. middle = sum([box_widths[i] for i in range(middle_node)]) \
  80. + max(middle_node - int(node_len % 2 == 0), 0) * separator_len
  81. title_line = center_text(node.title(), line_width, middle)
  82. pipe_sign = '│'.decode('utf-8')
  83. dash_sign = '─'.decode('utf-8')
  84. cross_sign = '┼'.decode('utf-8')
  85. tsplit_dn_sign = '┬'.decode('utf-8')
  86. tsplit_up_sign = '┴'.decode('utf-8')
  87. left_sign = '╭'.decode('utf-8')
  88. right_sign = '╮'.decode('utf-8')
  89. if node_len == 1:
  90. # Unary operators
  91. edge_line = center_text(pipe_sign, box_widths[0], middle)
  92. elif node_len % 2:
  93. # n-ary operators (n is odd)
  94. edge_line = ''
  95. for i, child in enumerate(node):
  96. if i > 0:
  97. edge_line += dash_sign
  98. if i < middle_node:
  99. marker = (left_sign if i == 0 else tsplit_dn_sign)
  100. edge_line += center_text(marker, box_widths[i],
  101. middle=0, right=dash_sign)
  102. else:
  103. if i == middle_node:
  104. marker = cross_sign
  105. edge_line += center_text(marker, box_widths[i],
  106. middle=0, right=dash_sign,
  107. left=dash_sign)
  108. else:
  109. if i == node_len - 1:
  110. marker = right_sign
  111. else:
  112. marker = tsplit_dn_sign
  113. edge_line += center_text(marker, box_widths[i],
  114. middle=0, left=dash_sign)
  115. else:
  116. # n-ary operators (n is even)
  117. edge_line = ''
  118. for i, child in enumerate(node):
  119. if i > 0:
  120. if i == middle_node:
  121. edge_line += tsplit_up_sign
  122. else:
  123. edge_line += dash_sign
  124. if i < middle_node:
  125. marker = (left_sign if i == 0 else tsplit_dn_sign)
  126. edge_line += center_text(marker, box_widths[i],
  127. middle=0, right=dash_sign)
  128. else:
  129. if i == node_len - 1:
  130. marker = right_sign
  131. else:
  132. marker = tsplit_dn_sign
  133. edge_line += center_text(marker, box_widths[i],
  134. middle=0, left=dash_sign)
  135. try:
  136. assert len(title_line) == len(edge_line)
  137. except AssertionError: # pragma: nocover
  138. print '------------------'
  139. print 'line_width:', line_width
  140. print 'title_line:', title_line, 'len:', len(title_line)
  141. print 'edge_line: %s (%d)' % (edge_line.encode('utf-8'),
  142. len(edge_line))
  143. print 'lines:'
  144. print '\n'.join(map(lambda x: x + ' %d' % len(x), lines))
  145. raise Exception()
  146. # Add the line of this node before all child lines.
  147. return [title_line, edge_line] + result
  148. calculate_width(root)
  149. #if verbose:
  150. # print '------- node_width ---------'
  151. # for node, width in node_width.iteritems():
  152. # print node.title(), 'width:', width
  153. lines = format_lines(root)
  154. # Strip trailing whitespace.
  155. return '\n'.join(map(lambda x: x.rstrip(), lines)).encode('utf-8')
  156. def center_text(text, width, middle=0, left=' ', right=' '):
  157. """
  158. >>> print center_text('│', 1, 1)
  159. >>> center_text('+', 15, 11)
  160. ' + '
  161. """
  162. text_len = len(text)
  163. text_mid = text_len / 2
  164. #print '---------'
  165. #print 'text_len:', text_len
  166. #print 'text_mid:', text_mid
  167. #print 'width:', width
  168. #print 'middle:', middle
  169. #print '---------'
  170. # TODO: this code requires cleanup.
  171. if middle:
  172. # If this is true, the text is at the left.
  173. if text_mid > middle:
  174. text += left * (width - text_len)
  175. # If this is true, the text is at the right.
  176. elif text_mid > (width - middle):
  177. text = right * (width - text_len) + text
  178. # Else, the text has spacing on its left and right.
  179. else:
  180. text = left * (middle - text_mid) + text
  181. text += right * (width - len(text))
  182. return text
  183. spacing = width - text_len
  184. # Even number of spaces can be split equally.
  185. if spacing % 2 == 0:
  186. return left * (spacing / 2) + text + right * (spacing / 2)
  187. # For an odd number of space, put the extra space at the end.
  188. return left * (spacing / 2) + text + right * (spacing / 2 + 1)