|
b08e8fa8
»
|
emilerl |
2009-06-02 |
Added the TaskPaper Python API |
1 |
#!/usr/bin/env python |
| |
2 |
# encoding: utf-8 |
| |
3 |
""" |
| |
4 |
taskpaper.py |
| |
5 |
|
| |
6 |
Created by Emil on 2009-06-02. |
| |
7 |
Copyright (c) 2009 Emil Erlandsson |
| |
8 |
|
| |
9 |
This program is free software: you can redistribute it and/or modify |
| |
10 |
it under the terms of the GNU General Public License as published by |
| |
11 |
the Free Software Foundation, either version 3 of the License, or |
| |
12 |
(at your option) any later version. |
| |
13 |
|
| |
14 |
This program is distributed in the hope that it will be useful, |
| |
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
17 |
GNU General Public License for more details. |
| |
18 |
|
| |
19 |
You should have received a copy of the GNU General Public License |
| |
20 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| |
21 |
""" |
| |
22 |
|
| |
23 |
import sys |
| |
24 |
import os |
| |
25 |
|
| |
26 |
class TaskPaperProject(object): |
| |
27 |
"""Representing a project in a TaskPaper file""" |
| |
28 |
def __init__(self, line): |
| |
29 |
self.line = line |
| |
30 |
self.name = "" |
| |
31 |
self.tasks = [] |
| |
32 |
self.notes = [] |
| |
33 |
self.__parse() |
| |
34 |
|
| |
35 |
def __parse(self): |
| |
36 |
"""Parses the line and sets up variables""" |
| |
37 |
self.name = self.line[:-1] |
| |
38 |
|
| |
39 |
def add_note(self, note): |
| |
40 |
"""Adds a note to this project""" |
| |
41 |
self.notes.append(note) |
| |
42 |
|
| |
43 |
def add_task(self, task): |
| |
44 |
"""Adds a task to this project""" |
| |
45 |
self.tasks.append(task) |
| |
46 |
|
| |
47 |
|
| |
48 |
class TaskPaperTask(object): |
| |
49 |
"""Represents a task in a taskpaper""" |
| |
50 |
def __init__(self, line): |
| |
51 |
self.line = line |
| |
52 |
self.tags = [] |
| |
53 |
self.project = None |
| |
54 |
self.name = "" |
| |
55 |
self.notes = [] |
| |
56 |
self.__parse() |
| |
57 |
|
| |
58 |
def __parse(self): |
| |
59 |
"""Parses the line and sets up variables""" |
| |
60 |
tokens = self.line.replace("- ", "").split("@") |
| |
61 |
self.name = tokens[0] |
| |
62 |
self.tags = tokens[1:] |
| |
63 |
|
| |
64 |
def set_project(self, project): |
| |
65 |
self.project = project |
| |
66 |
|
| |
67 |
def add_note(self, note): |
| |
68 |
"""Adds a note to this project""" |
| |
69 |
self.notes.append(note) |
| |
70 |
|
| |
71 |
def add_tag(self, tag): |
| |
72 |
self.tags.append(tag) |
| |
73 |
|
| |
74 |
|
| |
75 |
class TaskPaper(object): |
| |
76 |
"""A wrapper class for TaskPaper files""" |
| |
77 |
def __init__(self, filename): |
| |
78 |
self.filename = filename |
| |
79 |
self.projects = [] |
| |
80 |
self.tasks = [] |
| |
81 |
|
| |
82 |
def add_task(self, task): |
| |
83 |
"""Adds a no-parent task to this task paper""" |
| |
84 |
self.tasks.append(task) |
| |
85 |
|
| |
86 |
def add_project(self, project): |
| |
87 |
"""Adds a project to the taskpaper""" |
| |
88 |
self.projects.append(project) |
| |
89 |
|
| |
90 |
def __str__(self): |
| |
91 |
return "TaskPaper with %d headless tasks and %d projects" \ |
| |
92 |
% (len(self.tasks), len(self.projects)) |
| |
93 |
|
| |
94 |
def print_entries(self): |
| |
95 |
"""Prints all entries in the task paper""" |
| |
96 |
|
| |
97 |
print "Taskpaper: %s" % self.filename |
| |
98 |
|
| |
99 |
for project in self.projects: |
| |
100 |
print "\n%s:" % project.name |
| |
101 |
for note in project.notes: |
| |
102 |
print note |
| |
103 |
|
| |
104 |
for task in project.tasks: |
| |
105 |
rep = "- %s" % task.name.strip() |
| |
106 |
for tag in task.tags: |
| |
107 |
rep += " @%s" % tag.strip() |
| |
108 |
print rep |
| |
109 |
for note in task.notes: |
| |
110 |
print "%s" % note |
| |
111 |
print "\n\n" |
| |
112 |
|
| |
113 |
def print_stats(self): |
| |
114 |
tasks = len(self.tasks) |
| |
115 |
notes = 0 |
| |
116 |
|
| |
117 |
for project in self.projects: |
| |
118 |
tasks += len(project.tasks) |
| |
119 |
notes += len(project.notes) |
| |
120 |
for task in project.tasks: |
| |
121 |
notes += len(task.notes) |
| |
122 |
|
| |
123 |
print "%s has: %d projects with %d tasks and %d notes." \ |
| |
124 |
% (self.filename, len(self.projects), tasks, notes) |
| |
125 |
|
| |
126 |
|
| |
127 |
|
| |
128 |
def parse_task_paper(filename): |
| |
129 |
"""Parsing of a TaskPaper file""" |
| |
130 |
handle = file(filename) |
| |
131 |
|
| |
132 |
taskpaper = TaskPaper(filename) |
| |
133 |
project = None |
| |
134 |
last = None |
| |
135 |
for line in handle.readlines(): |
| |
136 |
line = unicode(line.strip(), "utf-8") |
| |
137 |
|
| |
138 |
if line.startswith("- "): # It is a task |
| |
139 |
task = TaskPaperTask(line) |
| |
140 |
last = task |
| |
141 |
if project is not None: |
| |
142 |
project.add_task(task) |
| |
143 |
task.project = project |
| |
144 |
|
| |
145 |
elif line.endswith(":"): # It is a project |
| |
146 |
project = TaskPaperProject(line) |
| |
147 |
taskpaper.add_project(project) |
| |
148 |
last = project |
| |
149 |
|
| |
150 |
else: # It is a note |
| |
151 |
last.add_note(line) |
| |
152 |
|
| |
153 |
return taskpaper |
| |
154 |
|
| |
155 |
|
| |
156 |
if __name__ == '__main__': |
| |
157 |
tp = parse_task_paper(sys.argv[1]) |
| |
158 |
tp.print_entries() |
| |
159 |
|