Commit 93c657fe authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add printer for Graphviz Dot format

This generates a graph of todo items, where the edges denote
dependencies between items. The tooltip shows the description (visible
in an SVG by using a decent browser).

Based on the idea that I implemented in topydo's predecessor:
Dot output by todo.txt-tools [1]. There, I implemented it as a
subsubcommand of `dep` to visualize dependencies. This implementation,
however, is part of the `ls` command, where you can use a filter
expression to limit the size of the graph.

The todo.txt-tools implementation also prints a box with a lookup table
of the numbers to descriptions. This is lacking at the moment.

To visualize the todo items of a project, run:

    topydo ls -x -f dot +Project | dot -Tsvg -o Project1.svg

(-x is suggested because parents with unfinished todo items are not
printed by default).

[1] https://github.com/bram85/todo.txt-tools/blob/2f9b2baa6382346c5cd9d2272c61ee0e342e25b6/actions/dep#L162
parent 75adf777
......@@ -62,6 +62,9 @@ class ListCommand(ExpressionCommand):
if self._poke_icalendar():
from topydo.lib.IcalPrinter import IcalPrinter
self.printer = IcalPrinter(self.todolist)
elif value == 'dot':
from topydo.lib.DotPrinter import DotPrinter
self.printer = DotPrinter(self.todolist)
else:
self.printer = None
elif opt == '-n':
......
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Provides a printer that transforms a list of Todo items to a graph in Dot
notation. Useful for displaying dependencies.
"""
from topydo.lib.PrettyPrinter import Printer
class DotPrinter(Printer):
"""
A printer that converts a list of Todo items to a string in Dot format.
"""
def __init__(self, p_todolist):
super(DotPrinter, self).__init__()
self.todolist = p_todolist
def print_list(self, p_todos):
node_name = lambda t: str(self.todolist.number(t))
node_tooltip = lambda t: todo.text().replace('"', '\\"')
result = 'digraph {\n'
# print todos
for todo in p_todos:
result += ' {} [tooltip="{}"]\n'.format(
node_name(todo),
node_tooltip(todo),
)
# print edges
for todo in p_todos:
for child in self.todolist.children(todo, p_only_direct=True):
result += ' {} -> {}\n'.format(
node_name(todo),
node_name(child)
)
result += '}\n'
return result
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment