breily / zen

command line based task management

This URL has Read+Write access

zen / zen_logic.py
d8b512e8 » Brian Reily 2008-05-28 Moved usage instructions in... 1 import os, random
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 2 import zen_io, zen_utilities
5897a298 » Brian Reily 2008-05-21 first commit 3
18365c4a » Brian Reily 2008-06-28 added some TODOs 4 # TODO: Add the needed functionality.
5
5897a298 » Brian Reily 2008-05-21 first commit 6 def print_projects():
7 """ Prints Project Names """
8 projects = zen_io.parse()
9 print 'Projects: '
10 for p in projects:
fc22f7e3 » Brian Reily 2008-05-22 Updated README file 11 t = 'task' if len(p.tasks) == 1 else 'tasks'
12 print '\t- %s [%s %s] ' %(p.name, len(p.tasks), t)
5897a298 » Brian Reily 2008-05-21 first commit 13
14 def print_tasks(proj_name, all_tasks=False):
15 """ Prints Tasks for a Project """
16 projects = zen_io.parse()
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 17 # Can print tasks for all projects
5897a298 » Brian Reily 2008-05-21 first commit 18 if all_tasks:
19 for p in projects:
20 print 'Project: %s' %p.name
21 for t in p.tasks:
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 22 print zen_utilities.format_task(t)
5897a298 » Brian Reily 2008-05-21 first commit 23 print
24 else:
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 25 p = zen_utilities.get_project(proj_name, projects)
26 if p is None:
27 print 'Error: Could not find project %s' %proj_name
28 return
69bfa175 » Brian Reily 2008-05-27 Moved more code into functi... 29 print 'Project: %s' %p.name
30 if p.tags: print 'Tags: %s' %', '.join(p.tags)
31 if len(p.fields):
32 for field in p.fields.items():
33 print '%s: %s' %(field[0], field[1])
34 print 'Tasks: '
35 for t in p.tasks:
36 print zen_utilities.format_task(t)
5897a298 » Brian Reily 2008-05-21 first commit 37
38 def print_task(proj_name, task_name):
39 """ Print details for one task """
40 projects = zen_io.parse()
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 41 p = zen_utilities.get_project(proj_name, projects)
42 if p is None:
43 print 'Error: Could not find project %s' %proj_name
44 return
69bfa175 » Brian Reily 2008-05-27 Moved more code into functi... 45 t = zen_utilities.get_task(task_name, p)
46 if t is None:
47 print 'Error: Could not find task %s' %task_name
48 return
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 49 print 'From Project: %s' %p.name
69bfa175 » Brian Reily 2008-05-27 Moved more code into functi... 50 print 'Description: %s' %t.desc
51 for key in t.fields.keys():
52 for k in t.fields[key]:
53 print '%s: %s' %(k, t.fields[key][k])
54 for tag in t.tags:
7764d814 » Brian Reily 2008-05-21 Added deletetask functionality 55 print 'Tag: %s' %tag
5897a298 » Brian Reily 2008-05-21 first commit 56
37c83395 » Brian Reily 2008-05-25 Added text search functiona... 57 def add_task(proj_name, args, quiet=False):
5897a298 » Brian Reily 2008-05-21 first commit 58 """ Adds a task """
59 task = ' '.join(args)
60 if task.strip() == '':
61 print 'Error: Cannot add empty tasks'
62 return
63 projects = zen_io.parse()
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 64 p = zen_utilities.get_project(proj_name, projects)
65 if p is None:
66 print 'Error: Could not find project %s' %proj_name
67 return
5897a298 » Brian Reily 2008-05-21 first commit 68 t = zen_io.Task()
69 new_args = []
70 for arg in args:
71 if '=' in arg:
72 s = arg.split('=')
73 if s[0] == 'desc': t.desc = s[1]
74 elif s[0] == 'tag': t.tags.append(s[1])
75 else: t.fields[s[0]] = s[1]
76 else: new_args.append( arg )
77 if t.desc == '': t.desc = ' '.join(new_args)
78 p.tasks.append(t)
79 zen_io.write(projects)
37c83395 » Brian Reily 2008-05-25 Added text search functiona... 80 if not quiet: print_tasks('', all_tasks=True)
5897a298 » Brian Reily 2008-05-21 first commit 81
37c83395 » Brian Reily 2008-05-25 Added text search functiona... 82 def tag_search(tag, proj_name):
5897a298 » Brian Reily 2008-05-21 first commit 83 """ Find items tagged as tag """
84 projects = zen_io.parse()
85 if tag is None:
86 print 'Tags:',
87 for project in projects:
88 for tag in project.tags: print '%s,' %tag,
89 for task in project.tasks:
90 for tag in task.tags: print '%s,' %tag,
91 return
92 found = []
93 if proj_name is None:
94 print 'Tag search for \'%s\':' %tag
95 tasks = [t for p in projects for t in p.tasks]
96 for task in tasks:
97 if tag in task.tags: found.append( (task, 'task') )
98 for proj in projects:
99 if tag in proj.tags: found.append( (proj, 'proj') )
100 for f in found:
101 if f[1] == 'task': print 'Task: %s' %f[0].desc
102 if f[1] == 'proj': print 'Project: %s' %f[0].name
103 if len(found) == 0: print 'No results found'
104 return
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 105 p = zen_utilities.get_project(proj_name, projects)
106 if p is None:
5897a298 » Brian Reily 2008-05-21 first commit 107 print 'Error: Could not find project %s' %proj_name
108 return
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 109 print 'Tag search in \'%s\' for \'%s\':' %(p.name, tag)
110 for task in p.tasks:
5897a298 » Brian Reily 2008-05-21 first commit 111 if tag in task.tags: found.append( task )
112 if len(found) == 0: print 'No tasks found'
113 for f in found:
114 print 'Task: %s' %f.desc
115
37c83395 » Brian Reily 2008-05-25 Added text search functiona... 116 def delete_task(tname, pname, quiet=False):
5897a298 » Brian Reily 2008-05-21 first commit 117 """ Delete a task from a project """
118 projects = zen_io.parse()
119 if pname is not None:
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 120 proj = zen_utilities.get_project(proj_name, projects)
121 if proj is None:
122 print 'Error: Could not find project %s' %proj_name
123 return
5897a298 » Brian Reily 2008-05-21 first commit 124 try:
125 i = int(tname)
126 new_tasks = [v for j, v in enumerate(proj.tasks) if j != i]
127 except:
128 new_tasks = []
129 for task in proj.tasks:
130 if task.desc[:len(tname)] != tname:
131 new_tasks.append(task)
132 proj.tasks = new_tasks
133 else:
134 for proj in projects:
135 new_tasks = []
136 for task in proj.tasks:
137 if task.desc[:len(tname)] != tname:
138 new_tasks.append(task)
139 proj.tasks = new_tasks
140 zen_io.write(projects)
37c83395 » Brian Reily 2008-05-25 Added text search functiona... 141 if not quiet: print_tasks('', all_tasks=True)
142
143 def search(query, proj_name):
144 """ Full Text Search of all Entries """
145 projects = zen_io.parse()
146 if proj_name is None:
147 projects.extend([t for p in projects for t in p.tasks])
148 else:
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 149 proj = [zen_utilities.get_project(proj_name, projects)]
150 if proj == [None]:
151 print 'Error: Could not find project %s' %proj_name
152 return
37c83395 » Brian Reily 2008-05-25 Added text search functiona... 153 proj.extend(proj.tasks)
154 projects = proj
155 for item in projects:
156 try:
157 if query in item.desc: print 'Found Task: %s' %item.desc
158 except:
aa0f501a » Brian Reily 2008-05-26 Added functionality to add ... 159 if query in item.name: print 'Found Project: %s' %item.name
160
161 def add_project(proj_name, quiet=False):
162 """ Adds a project """
163 projects = zen_io.parse()
164 p = zen_io.Project()
165 p.name = proj_name
166 projects.append(p)
167 zen_io.write(projects)
168 if not quiet: print_tasks('', all_tasks=True)
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 169
d8b512e8 » Brian Reily 2008-05-28 Moved usage instructions in... 170 def revert(quiet=False):
274f310a » Brian Reily 2008-05-27 Added an undo function; Mov... 171 """ Copies the old file to the current file """
172 os.system('cp %s.old %s' %(zen_io.FILE_NAME, zen_io.FILE_NAME))
173 print 'Zen has reverted to its previous state.'
d8b512e8 » Brian Reily 2008-05-28 Moved usage instructions in... 174 if not quiet: print_projects()
175
176 def shuffle(quiet=False):
177 """ Shuffles the project list into a random order """
178 projects = zen_io.parse()
179 random.shuffle(projects)
180 zen_io.write(projects)
181 if not quiet: print_projects()
182
183 def reverse(quiet=False):
184 """ Reverses the project list """
185 projects = zen_io.parse()
186 projects.reverse()
187 zen_io.write(projects)
188 if not quiet: print_projects()