Commit 9f78bed1 by rfkelly0

WatchableFS: more accurate event generation for makedir, copy and move

parent 8918947d
...@@ -261,9 +261,17 @@ class WatchableFS(WrapFS,WatchableFSMixin): ...@@ -261,9 +261,17 @@ class WatchableFS(WrapFS,WatchableFSMixin):
self.notify_watchers(ACCESSED,path) self.notify_watchers(ACCESSED,path)
return WatchedFile(f,self,path,mode) return WatchedFile(f,self,path,mode)
def makedir(self,path,*args,**kwds): def makedir(self,path,recursive=False,allow_recreate=False):
existed = self.wrapped_fs.isdir(path) existed = self.wrapped_fs.isdir(path)
super(WatchableFS,self).makedir(path,*args,**kwds) try:
super(WatchableFS,self).makedir(path,allow_recreate=allow_recreate)
except ParentDirectoryMissingError:
if not recursive:
raise
parent = dirname(path)
if parent != path:
self.makedir(dirname(path),recursive=True,allow_recreate=True)
super(WatchableFS,self).makedir(path,allow_recreate=allow_recreate)
if not existed: if not existed:
self.notify_watchers(CREATED,path) self.notify_watchers(CREATED,path)
...@@ -271,9 +279,23 @@ class WatchableFS(WrapFS,WatchableFSMixin): ...@@ -271,9 +279,23 @@ class WatchableFS(WrapFS,WatchableFSMixin):
super(WatchableFS,self).remove(path) super(WatchableFS,self).remove(path)
self.notify_watchers(REMOVED,path) self.notify_watchers(REMOVED,path)
def removedir(self,path,*args,**kwds): def removedir(self,path,recursive=False,force=False):
super(WatchableFS,self).removedir(path,*args,**kwds) if not force:
for nm in self.listdir(path):
raise DirectoryNotEmptyError(path)
else:
for nm in self.listdir(path,dirs_only=True):
self.removedir(pathjoin(path,nm),force=True)
for nm in self.listdir(path,files_only=True):
self.remove(pathjoin(path,nm))
super(WatchableFS,self).removedir(path)
self.notify_watchers(REMOVED,path) self.notify_watchers(REMOVED,path)
if recursive:
parent = dirname(path)
while parent and not self.listdir(parent):
super(WatchableFS,self).removedir(parent)
self.notify_watchers(REMOVED,parent)
parent = dirname(parent)
def rename(self,src,dst): def rename(self,src,dst):
d_existed = self.wrapped_fs.exists(dst) d_existed = self.wrapped_fs.exists(dst)
...@@ -309,36 +331,36 @@ class WatchableFS(WrapFS,WatchableFSMixin): ...@@ -309,36 +331,36 @@ class WatchableFS(WrapFS,WatchableFSMixin):
dst_paths = {} dst_paths = {}
try: try:
for (dirnm,filenms) in self.wrapped_fs.walk(dst): for (dirnm,filenms) in self.wrapped_fs.walk(dst):
dirnm = dirnm[len(dst):] dirnm = dirnm[len(dst)+1:]
dst_paths[dirnm] = True dst_paths[dirnm] = True
for filenm in filenms: for filenm in filenms:
dst_paths[filenm] = False dst_paths[filenm] = False
except ResourceNotFoundError: except ResourceNotFoundError:
pass pass
except ResourceInvalidError: except ResourceInvalidError:
dst_paths[dst] = False dst_paths[""] = False
src_paths = {} src_paths = {}
try: try:
for (dirnm,filenms) in self.wrapped_fs.walk(src): for (dirnm,filenms) in self.wrapped_fs.walk(src):
dirnm = dirnm[len(src):] dirnm = dirnm[len(src)+1:]
src_paths[dirnm] = True src_paths[dirnm] = True
for filenm in filenms: for filenm in filenms:
src_paths[pathjoin(dirnm,filenm)] = False src_paths[pathjoin(dirnm,filenm)] = False
except ResourceNotFoundError: except ResourceNotFoundError:
pass pass
except ResourceInvalidError: except ResourceInvalidError:
src_paths[src] = False src_paths[""] = False
return (src_paths,dst_paths) return (src_paths,dst_paths)
def _post_copy(self,src,dst,data): def _post_copy(self,src,dst,data):
(src_paths,dst_paths) = data (src_paths,dst_paths) = data
for src_path,isdir in src_paths.iteritems(): for src_path,isdir in sorted(src_paths.items()):
path = pathjoin(dst,src_path) path = pathjoin(dst,src_path)
if src_path in dst_paths: if src_path in dst_paths:
self.notify_watchers(MODIFIED,path,not isdir) self.notify_watchers(MODIFIED,path,not isdir)
else: else:
self.notify_watchers(CREATED,path) self.notify_watchers(CREATED,path)
for dst_path,isdir in dst_paths.iteritems(): for dst_path,isdir in sorted(dst_paths.items()):
path = pathjoin(dst,dst_path) path = pathjoin(dst,dst_path)
if not self.wrapped_fs.exists(path): if not self.wrapped_fs.exists(path):
self.notify_watchers(REMOVED,path) self.notify_watchers(REMOVED,path)
......
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