public
Description: command line based task management
Homepage: http://brianreily.com/projects/zen
Clone URL: git://github.com/breily/zen.git
zen / zen_io.py
100755 114 lines (108 sloc) 3.911 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
import yaml, sys, os
 
# Locations of your projects file.
# A project called 'todo list' must exist.
# TODO: Fix so 'todo list' doesn't have to exist, but is
# created if needed.
# TODO: Allow an alternate project file to be specified
# in command line options.
FILE_NAME = '/Users/brian/projects/zen/zen.db'
# Setting this to False will use YAML's default
# object dump - making your list difficult to read.
# TODO: Do some actual testing on this.
HUMAN_READABLE = True
 
class Project:
    """
A Project object has a name, list of tags,
a dictionary of fields, and a list of Tasks.
"""
    def __init__(self):
        self.name = ''
        self.tags = []
        self.fields = {}
        self.tasks = []
    def __repr__(self):
        return '< Project: \'%s\' >' %self.name
 
class Task:
    """
A Task object has a desc, list of tags, and
a dictionary of fields.
"""
    def __init__(self):
        self.desc = ''
        self.tags = []
        self.fields = {}
    def __repr__(self):
        return '< Task: \'%s\' >' %self.desc
 
def write(project_list):
    """
Writes a list of projects to FILE_NAME.
Creates a backup with the extension 'old'.
"""
    os.system('cp %s %s.old' %(FILE_NAME, FILE_NAME))
    os.system('rm -f %s' %FILE_NAME)
    if HUMAN_READABLE:
        file = open(FILE_NAME, 'w')
        tab = ' '
        file.write('projects:\n')
        for project in project_list:
            file.write('%s-\n' %(tab))
            file.write('%sname: %s\n' %(tab*2, project.name))
            if len(project.tags) != 0:
                file.write('%stags:\n' %(tab*2))
            for tag in project.tags:
                file.write('%s- %s\n' %(tab*3, tag))
            if len(project.fields) != 0:
                file.write('%sfields:\n' %(tab*2))
            for item in project.fields.items():
                file.write('%s%s: %s\n' %(tab*3, item[0], item[1]))
            if len(project.tasks) != 0:
                file.write('%stasks:\n' %(tab*2))
            for task in project.tasks:
                file.write('%s-\n' %(tab*3))
                file.write('%sdesc: %s\n' %(tab*4, task.desc))
                if len(task.tags) != 0:
                    file.write('%stags:\n' %(tab*4))
                for tag in task.tags:
                    file.write('%s- %s\n' %(tab*5, tag))
                if len(task.fields) != 0:
                    file.write('%sfields:\n' %(tab*4))
                for item in task.fields.items():
                    k = item[1].keys()[0]
                    v = item[1][k]
                    file.write('%s%s: %s\n' %(tab*5, k, v))
        file.close()
    else:
        yaml.dump( project_list, open(FILE_NAME, 'w') )
 
def parse():
    """
Returns a list of projects parsed from FILE_NAME.
"""
    data = yaml.load( open(FILE_NAME) )
    if HUMAN_READABLE:
        projects = []
        for proj in data['projects']:
            p = Project()
            # Parse out name
            try: p.name = proj['name']
            except: p.name = 'untitled'
            # Parse meta information
            if 'fields' in proj.keys():
                for key in proj['fields'].keys():
                    p.fields[key] = proj['fields'][key]
            # Parse tags
            if 'tags' in proj.keys():
                for tag in proj['tags']:
                    p.tags.append( tag )
            # Parse tasks
            if 'tasks' in proj.keys():
                for task in proj['tasks']:
                    t = Task()
                    for key in task.keys():
                        if key == 'desc': t.desc = task[key]
                        elif key == 'tags':
                            t.tags.extend( task[key] )
                        else: t.fields[key] = task[key]
                    p.tasks.append(t)
            projects.append(p)
        return projects
    else: return data