Commit b202f48c by rfkelly0

fs.watch.WatchedFile: inherit from FileWrapper, send MODIFIED only on write

parent c4bf5237
...@@ -39,6 +39,7 @@ from fs.path import * ...@@ -39,6 +39,7 @@ from fs.path import *
from fs.errors import * from fs.errors import *
from fs.wrapfs import WrapFS from fs.wrapfs import WrapFS
from fs.base import FS from fs.base import FS
from fs.filelike import FileWrapper
class EVENT(object): class EVENT(object):
...@@ -241,51 +242,40 @@ class WatchableFSMixin(FS): ...@@ -241,51 +242,40 @@ class WatchableFSMixin(FS):
class WatchedFile(object): class WatchedFile(FileWrapper):
"""File wrapper for use with WatchableFS. """File wrapper for use with WatchableFS.
This file wrapper provides access to a file opened from a WatchableFS This file wrapper provides access to a file opened from a WatchableFS
instance, and fires MODIFIED events when the file is modified. instance, and fires MODIFIED events when the file is modified.
""" """
def __init__(self,file,fs,path,mode): def __init__(self,file,fs,path,mode=None):
self.file = file super(WatchedFile,self).__init__(file,mode)
self.fs = fs self.fs = fs
self.path = path self.path = path
self.mode = mode self.was_modified = False
def __del__(self): def _write(self,string,flushing=False):
# Don't bother if python if being torn down self.was_modified = True
if Watcher is not None: return super(WatchedFile,self)._write(string,flushing=flushing)
self.close()
def __getattr__(self,name):
file = self.__dict__['file']
a = getattr(file, name)
if callable(a):
setattr(self,name,a)
return a
def __enter__(self): def _truncate(self,size):
self.file.__enter__() self.was_modified = True
return self return super(WatchedFile,self)._truncate(size)
def __exit__(self,exc_type,exc_value,traceback):
self.close()
return False
def __iter__(self):
return iter(self.file)
def flush(self): def flush(self):
self.file.flush() super(WatchedFile,self).flush()
if "w" in self.mode or "a" in self.mode or "+" in self.mode: # Don't bother if python if being torn down
self.fs.notify_watchers(MODIFIED,self.path,True) if Watcher is not None:
if self.was_modified:
self.fs.notify_watchers(MODIFIED,self.path,True)
def close(self): def close(self):
self.file.close() super(WatchedFile,self).close()
if "w" in self.mode or "a" in self.mode or "+" in self.mode: # Don't bother if python if being torn down
self.fs.notify_watchers(MODIFIED,self.path,True) if Watcher is not None:
if self.was_modified:
self.fs.notify_watchers(MODIFIED,self.path,True)
class WatchableFS(WatchableFSMixin,WrapFS): class WatchableFS(WatchableFSMixin,WrapFS):
......
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