# HG changeset patch # User Steve Losh # Date 1476030827 0 # Node ID 1d79df25277b7b65f5a5ab6a3d1ec7a1514d2666 # Parent 27d8f89d64c398d9a7a63207d16ef361c3a00bfb# Parent bc7821308e0bedefbd5009ab0a92babe4c828960 Merge. diff -r 27d8f89d64c3 -r 1d79df25277b t.py --- a/t.py Sun Apr 07 15:08:50 2013 +0100 +++ b/t.py Sun Oct 09 16:33:47 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,12 +141,15 @@ if os.path.isdir(path): raise InvalidTaskfile if os.path.exists(path): - 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 + 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. @@ -230,9 +240,13 @@ raise InvalidTaskfile 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) + 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) @@ -308,6 +322,8 @@ except UnknownPrefix: e = sys.exc_info()[1] 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__':