Writer for the DOT language. See also: - "The DOT Language" specification http://www.graphviz.org/doc/info/lang.html
| 2561 | |
| 2562 | |
| 2563 | class DotWriter: |
| 2564 | """Writer for the DOT language. |
| 2565 | |
| 2566 | See also: |
| 2567 | - "The DOT Language" specification |
| 2568 | http://www.graphviz.org/doc/info/lang.html |
| 2569 | """ |
| 2570 | |
| 2571 | strip = False |
| 2572 | wrap = False |
| 2573 | |
| 2574 | def __init__(self, fp): |
| 2575 | self.fp = fp |
| 2576 | |
| 2577 | def wrap_function_name(self, name): |
| 2578 | """Split the function name on multiple lines.""" |
| 2579 | |
| 2580 | if len(name) > 32: |
| 2581 | ratio = 2.0/3.0 |
| 2582 | height = max(int(len(name)/(1.0 - ratio) + 0.5), 1) |
| 2583 | width = max(len(name)/height, 32) |
| 2584 | # TODO: break lines in symbols |
| 2585 | name = textwrap.fill(name, width, break_long_words=False) |
| 2586 | |
| 2587 | # Take away spaces |
| 2588 | name = name.replace(", ", ",") |
| 2589 | name = name.replace("> >", ">>") |
| 2590 | name = name.replace("> >", ">>") # catch consecutive |
| 2591 | |
| 2592 | return name |
| 2593 | |
| 2594 | def graph(self, profile, theme): |
| 2595 | self.begin_graph() |
| 2596 | |
| 2597 | fontname = theme.graph_fontname() |
| 2598 | |
| 2599 | self.attr('graph', fontname=fontname, ranksep=0.25, nodesep=0.125) |
| 2600 | self.attr('node', fontname=fontname, shape="box", style="filled", fontcolor="white", width=0, height=0) |
| 2601 | self.attr('edge', fontname=fontname) |
| 2602 | |
| 2603 | for function in profile.functions.values(): |
| 2604 | labels = [] |
| 2605 | if function.process is not None: |
| 2606 | labels.append(function.process) |
| 2607 | if function.module is not None: |
| 2608 | labels.append(function.module) |
| 2609 | |
| 2610 | if self.strip: |
| 2611 | function_name = function.stripped_name() |
| 2612 | else: |
| 2613 | function_name = function.name |
| 2614 | if self.wrap: |
| 2615 | function_name = self.wrap_function_name(function_name) |
| 2616 | labels.append(function_name) |
| 2617 | |
| 2618 | for event in TOTAL_TIME_RATIO, TIME_RATIO: |
| 2619 | if event in function.events: |
| 2620 | label = event.format(function[event]) |