--- a/t.py Sat Aug 31 10:31:43 2013 +0200
+++ b/t.py Sun Oct 02 12:56:52 2016 +0000
@@ -25,6 +25,13 @@
super(UnknownPrefix, self).__init__()
self.prefix = prefix
+class BadFile(Exception):
+ """Raised when something else goes wrong trying to work with the task file."""
+ def __init__(self, path, problem):
+ super(BadFile, self).__init__()
+ self.path = path
+ self.problem = problem
+
def _hash(text):
"""Return a hash of the given text for use as an id.
@@ -134,15 +141,15 @@
if os.path.isdir(path):
raise InvalidTaskfile
if os.path.exists(path):
- try:
- with open(path, 'r') as tfile:
- tls = [tl.strip() for tl in tfile if tl]
- tasks = map(_task_from_taskline, tls)
- for task in tasks:
- if task is not None:
- getattr(self, kind)[task['id']] = task
- except IOError as e:
- sys.exit(e.strerror + ' - ' + path)
+ try:
+ with open(path, 'r') as tfile:
+ tls = [tl.strip() for tl in tfile if tl]
+ tasks = map(_task_from_taskline, tls)
+ for task in tasks:
+ if task is not None:
+ getattr(self, kind)[task['id']] = task
+ except IOError as e:
+ raise BadFile(path, e.strerror)
def __getitem__(self, prefix):
"""Return the unfinished task with the given prefix.
@@ -233,12 +240,12 @@
raise InvalidTaskfile
tasks = sorted(getattr(self, kind).values(), key=itemgetter('id'))
if tasks or not delete_if_empty:
- try:
- with open(path, 'w') as tfile:
- for taskline in _tasklines_from_tasks(tasks):
- tfile.write(taskline)
- except IOError as e:
- sys.exit(e.strerror + ' - ' + path)
+ try:
+ with open(path, 'w') as tfile:
+ for taskline in _tasklines_from_tasks(tasks):
+ tfile.write(taskline)
+ except IOError as e:
+ raise BadFile(path, e.strerror)
elif not tasks and os.path.isfile(path):
os.remove(path)
@@ -313,6 +320,8 @@
sys.stderr.write('The ID "%s" matches more than one task.\n' % e.prefix)
except UnknownPrefix, e:
sys.stderr.write('The ID "%s" does not match any task.\n' % e.prefix)
+ except BadFile, e:
+ sys.stderr.write('%s - %s\n' % (e.problem, e.path))
if __name__ == '__main__':