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