# HG changeset patch # User Steve Losh # Date 1264038917 18000 # Node ID c00fff9956e66e1b070e4f38d1e92c5965522bb0 # Parent b8afd9a5c913d59d744a11bae693eba64d9dbac1 Add the --delete-if-empty options for Desktop users. diff -r b8afd9a5c913 -r c00fff9956e6 README --- a/README Mon Sep 28 23:19:42 2009 -0400 +++ b/README Wed Jan 20 20:55:17 2010 -0500 @@ -128,6 +128,11 @@ 30 - Clean the entire apartment. $ +### Delete the Task List if it's Empty + +If you keep your task list in a visible place (like your desktop) you might want it to be deleted if there are no tasks in it. To do this automatically you can use the `--delete-if-empty` option in your alias: + + alias t='python ~/path/to/t.py --task-dir ~/Desktop --list todo.txt --delete-if-empty' Tips and Tricks --------------- diff -r b8afd9a5c913 -r c00fff9956e6 t.py --- a/t.py Mon Sep 28 23:19:42 2009 -0400 +++ b/t.py Wed Jan 20 20:55:17 2010 -0500 @@ -193,17 +193,20 @@ p = '%s - ' % task[label].ljust(plen) if not quiet else '' print p + task['text'] - def write(self): + def write(self, delete_if_empty=False): """Flush the finished and unfinished tasks to the files on disk.""" filemap = (('tasks', self.name), ('done', '.%s.done' % self.name)) for kind, filename in filemap: path = os.path.join(os.path.expanduser(self.taskdir), filename) if os.path.isdir(path): raise InvalidTaskfile - with open(path, 'w') as tfile: - tasks = sorted(getattr(self, kind).values(), key=itemgetter('id')) - for taskline in _tasklines_from_tasks(tasks): - tfile.write(taskline) + tasks = sorted(getattr(self, kind).values(), key=itemgetter('id')) + if tasks or not delete_if_empty: + with open(path, 'w') as tfile: + for taskline in _tasklines_from_tasks(tasks): + tfile.write(taskline) + elif not tasks and os.path.isfile(path): + os.remove(path) @@ -225,6 +228,9 @@ help="work on LIST", metavar="LIST") config.add_option("-t", "--task-dir", dest="taskdir", default="", help="work on the lists in DIR", metavar="DIR") + config.add_option("-d", "--delete-if-empty", + action="store_true", dest="delete", default=False, + help="delete the task file if it becomes empty") parser.add_option_group(config) output = OptionGroup(parser, "Output Options") @@ -250,13 +256,13 @@ try: if options.finish: td.finish_task(options.finish) - td.write() + td.write(options.delete) elif options.edit: td.edit_task(options.edit, text) - td.write() + td.write(options.delete) elif text: td.add_task(text) - td.write() + td.write(options.delete) else: td.print_list(verbose=options.verbose, quiet=options.quiet, grep=options.grep)