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