graph.py 8.6 KB

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