diff options
author | rfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f> | 2010-09-13 03:59:20 +0000 |
---|---|---|
committer | rfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f> | 2010-09-13 03:59:20 +0000 |
commit | dbd5a0fc19c915cd76947b6bda94859dfb50fba8 (patch) | |
tree | 8540f291c698c7c0657f5007ae96eb9aaa34a336 /fs/watch.py | |
parent | d7d829c1f167ef0bc813607fa861720230622ad6 (diff) | |
download | pyfilesystem-dbd5a0fc19c915cd76947b6bda94859dfb50fba8.tar.gz |
make WatchableFSMixin instances picklable, by discarding watchers
git-svn-id: http://pyfilesystem.googlecode.com/svn/trunk@423 67cdc799-7952-0410-af00-57a81ceafa0f
Diffstat (limited to 'fs/watch.py')
-rw-r--r-- | fs/watch.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/fs/watch.py b/fs/watch.py index 991533f..7424841 100644 --- a/fs/watch.py +++ b/fs/watch.py @@ -141,6 +141,15 @@ class WatchableFSMixin(FS): self._watchers = PathMap() super(WatchableFSMixin,self).__init__(*args,**kwds) + def __getstate__(self): + state = super(WatchableFSMixin,self).__getstate__() + state.pop("_watchers",None) + return state + + def __setstate__(self,state): + super(WatchableFSMixin,self).__setstate__(state) + self._watchers = PathMap() + def add_watcher(self,callback,path="/",events=None,recursive=True): """Add a watcher callback to the FS.""" w = Watcher(self,callback,path,events,recursive=recursive) @@ -165,9 +174,14 @@ class WatchableFSMixin(FS): if watcher.callback is callback: yield watcher - def notify_watchers(self,event_class,path=None,*args,**kwds): + def notify_watchers(self,event_or_class,path=None,*args,**kwds): """Notify watchers of the given event data.""" - event = event_class(self,path,*args,**kwds) + if isinstance(event_or_class,EVENT): + event = event_or_class + else: + event = event_or_class(self,path,*args,**kwds) + if path is None: + path = event.path if path is None: for watchers in self._watchers.itervalues(): for watcher in watchers: @@ -472,7 +486,13 @@ def ensure_watchable(fs,wrapper_class=PollingWatchableFS,*args,**kwds): for watcher callbacks. This may be the original object if it supports them natively, or a wrapper class if they must be simulated. """ + if isinstance(fs,wrapper_class): + return fs try: + # Try to add a watcher to a path that's unlikely to exist. + # It's OK if the path does exist, since we remove the watcher. + # It just might be slightly slower as the fs will actually have + # to establish the watcher. w = fs.add_watcher(lambda e: None,"/somepaththatsnotlikelytoexist") except (AttributeError,UnsupportedError): return wrapper_class(fs,*args,**kwds) |