graph.py 7.8 KB

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