Catching Save - newbie

Beni Cherniavsky cben at techunix.technion.ac.il
Tue Apr 15 07:21:40 EDT 2003


Vahur Lokk wrote on 2003-04-15:

> Hi!
> What I want to do is write a small daemon that can catch Save File type of
> events in the system and manipulate files being saved.
> Having mostly sorted out the rest of the program I cannot think of a way to
> catch these saves.
>
Do you mean saves from inside your python program or any saves done by
other programs running on your computer?

In the first case, you can try to replace the builtin `file` function
(and its `open` alias) to return a wrapper that does what you need
when one calls the `.write()` and `.close()` methods of it.
Subclassing `file` sounds like a good way for this.

In the later case, there are two basic approaches:

1. Poll the file system for changes.  libfam_ will do it better than
   you can probably bother to write.  In any case you must realize
   that some updates will be missed and notifications might be
   delayed.  There is no way to get reliable update notifications on
   unix (perhaps there is on windows).

.. _libfam: http://oss.sgi.com/projects/fam/

2. Create a new filesystem driver (or modify existing one) that will
   communicate notifications to your user-land program and replace
   your filesystem with it.  This can be made reliable but is very
   much work and a huge hassle to install.

In any case, you'll have to define what you mean by "Save File"
events.  Under the hood programs can modify files continuosly; at best
you can aim for the close event (not in approach 1 of case 2).  Also
note that you probably care also for rename actions - most
self-respecting editors save files by writing a new file and then
atomically renaming over the old version (or some variant thereof).

Perhaps you should rethink your goals.  Why would you want to catch
file modifications and how do you want to manipulate them?  Perhaps
there are better ways?  What is your real goal?

-- 
Beni Cherniavsky <cben at tx.technion.ac.il>





More information about the Python-list mailing list