Commit 3ba34834 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Only emit a file update event when todo.txt was changed externally

parent e0a54122
...@@ -30,6 +30,7 @@ class TodoFile(object): ...@@ -30,6 +30,7 @@ class TodoFile(object):
def __init__(self, p_path, p_on_update=None): def __init__(self, p_path, p_on_update=None):
self.path = os.path.abspath(p_path) self.path = os.path.abspath(p_path)
self.write_lock = False
if p_on_update: if p_on_update:
from watchdog.observers import Observer from watchdog.observers import Observer
...@@ -39,7 +40,7 @@ class TodoFile(object): ...@@ -39,7 +40,7 @@ class TodoFile(object):
def _handle(_, p_event): def _handle(_, p_event):
right_type = isinstance(p_event, FileCreatedEvent) or isinstance(p_event, FileModifiedEvent) right_type = isinstance(p_event, FileCreatedEvent) or isinstance(p_event, FileModifiedEvent)
if right_type and p_event.src_path == self.path: if not self.write_lock and right_type and p_event.src_path == self.path:
p_on_update() p_on_update()
def on_created(self, p_event): def on_created(self, p_event):
...@@ -75,6 +76,11 @@ class TodoFile(object): ...@@ -75,6 +76,11 @@ class TodoFile(object):
p_todos can be a list of todo items, or a string that is just written p_todos can be a list of todo items, or a string that is just written
to the file. to the file.
""" """
# make sure not to reread the todo file because this instance is
# actually writing it
self.write_lock = True
todofile = codecs.open(self.path, 'w', encoding="utf-8") todofile = codecs.open(self.path, 'w', encoding="utf-8")
if p_todos is list: if p_todos is list:
...@@ -86,3 +92,4 @@ class TodoFile(object): ...@@ -86,3 +92,4 @@ class TodoFile(object):
todofile.write("\n") todofile.write("\n")
todofile.close() todofile.close()
self.write_lock = False
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment