[Twisted-Python] watch a directory (Win32)

Hello twisted folks!
Last week I found this great framework that seems to do most of what I need. But it's a bit hard to get into, as you know...
I'm on WinXP with Python 2.3. I need to watch a directory for new files and found this recipe using Win32 events: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/156178
I'd like to use a twisted reactor instead of that while loop, but don't understand how to handle those Win32 notifies using deferreds.
Or perhaps there is a better twisted solution, that I didn't find in the docs?
I'd prefer a portable way (my script could also run on AIX or MacOS X), but as far as I found, it's heavily system dependent (Win32: win32file.FindFirstChangeNotification, BSD/OSX: kqueue?, Linux: dnotify?)
Best regards, Henning Hraban Ramm Südkurier Medienhaus / MediaPro Support/Admin/Development Dept.

On Tue, Mar 08, 2005 at 11:27:35AM +0100, Henning.Ramm@mediapro-gmbh.de wrote: [...]
I'd prefer a portable way (my script could also run on AIX or MacOS X), but as far as I found, it's heavily system dependent (Win32: win32file.FindFirstChangeNotification, BSD/OSX: kqueue?, Linux: dnotify?)
I haven't tried using it with Twisted (yet), but something like the python bindings for gamin would probably be a good start. It can use the new inotify stuff in Linux, or do simple polling. http://www.gnome.org/~veillard/gamin/index.html
-Andrew.

On Tue, Mar 08, 2005 at 10:27:04PM +1100, Andrew Bennetts wrote:
On Tue, Mar 08, 2005 at 11:27:35AM +0100, Henning.Ramm@mediapro-gmbh.de wrote: [...]
I'd prefer a portable way (my script could also run on AIX or MacOS X), but as far as I found, it's heavily system dependent (Win32: win32file.FindFirstChangeNotification, BSD/OSX: kqueue?, Linux: dnotify?)
I haven't tried using it with Twisted (yet), but something like the python bindings for gamin would probably be a good start. It can use the new inotify stuff in Linux, or do simple polling. http://www.gnome.org/~veillard/gamin/index.html
...although I suspect it might not work on win32 :/
-Andrew.

just add something like this in your pathwather.py:
def waiting(): try: old_path_contents = os.listdir (path_to_watch) result = win32event.WaitForSingleObject (change_handle, 500)
# # If the WaitFor... returned because of a notification (as # opposed to timing out or some error) then look for the # changes in the directory contents. # if result == win32con.WAIT_OBJECT_0: new_path_contents = os.listdir (path_to_watch) files_added = [f for f in new_path_contents if not f in old_path_contents] files_deleted = [f for f in old_path_contents if not f in new_path_contents]
if files_added: print print time.asctime () print "<Added file=\""+ files_added[0]+"\"/>" return "<Added file=\""+ files_added[0]+"\"/>" if files_deleted: print "<Deleted file=\""+files_deleted[0]+"\"/>" return "<Deleted file=\""+files_deleted[0]+"/\">"
win32file.FindNextChangeNotification (change_handle) finally: pass #win32file.FindCloseChangeNotification (change_handle)
and call it with reactor.callLater(0,someFunctionThatCallsWaiting) regualary
cheers p@
participants (3)
-
Andrew Bennetts
-
Henning.Ramm@mediapro-gmbh.de
-
Patrick Lauber