Can I hook a "file" to a python script?

S

Scott Chapman

Hi!

I'd like a "file" on the Linux box to actually be the input and output
of a Python script. Anything written to the file would be sent to a
database and anything read from the file would come from the database.
I know how to do the database end of this but I'm not sure if a script
can be hooked to a "file" transparently. The script would probably
have to be run as a daemon. I doubt there's enough magic in the Linux
world to make it so that a read/write would actually launch the script.
That'd be Ok.

Scott
 
D

dmbkiwi

Hi!

I'd like a "file" on the Linux box to actually be the input and output
of a Python script. Anything written to the file would be sent to a
database and anything read from the file would come from the database.
I know how to do the database end of this but I'm not sure if a script
can be hooked to a "file" transparently. The script would probably
have to be run as a daemon. I doubt there's enough magic in the Linux
world to make it so that a read/write would actually launch the script.
That'd be Ok.

Scott

I may be missing the point here, so forgive me if I am.

However, files can be opened as objects and read and written to very
simply in python.

To open a file:

file_object = open('/path/to/file', 'r')

Then you can read it like thus:

file_contents = file_object.read()

or get each line in a list like this:

file_line_list = file_object.readlines()

you can also write to a file. You have to create the file object first:

save_file = open('filename', 'w')

then you can write to it like:

print >> save_file, 'some text'

you should also close the file objects you open once you're done with them.

Gook luck

Matt
 
A

Alan Gauld

I know how to do the database end of this but I'm not sure if a script
can be hooked to a "file" transparently. The script would probably
have to be run as a daemon. I doubt there's enough magic in the Linux
world to make it so that a read/write would actually launch the script.

This can be done - indeed its how the ClearCase config management
tool works on *nix - but it involves some low level magic. I
believe you need to swap out the read/write system calls. I don't
pretend to know how you do that but it can be done. Whether you
could do it in pure Python I doubt, but a C program that called
Python maybe.

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 
A

Alex Martelli

Scott said:
I'd like a "file" on the Linux box to actually be the input and output
of a Python script. Anything written to the file would be sent to a
database and anything read from the file would come from the database.
I know how to do the database end of this but I'm not sure if a script
can be hooked to a "file" transparently. The script would probably
have to be run as a daemon. I doubt there's enough magic in the Linux
world to make it so that a read/write would actually launch the script.

"named pipes", aka FIFOs (created by the mkfifo shell command) are
going to be the "files" you use for that. But, yes, SOME daemon must
be watching on them, e.g. with a select -- could be a generic one that
will spawn the specific one at need, but it's simpler to have the real
script watch on them -- I do agree there's probably no way to get the
kernel to do it for you.


Alex
 
M

Mathias Waack

Scott said:
I'd like a "file" on the Linux box to actually be the input and
output
of a Python script. Anything written to the file would be sent to
a database and anything read from the file would come from the
database. I know how to do the database end of this but I'm not
sure if a script
can be hooked to a "file" transparently. The script would probably
have to be run as a daemon. I doubt there's enough magic in the
Linux world to make it so that a read/write would actually launch
the script. That'd be Ok.

The solution is very easy. There are at least two ways. First the
easier one: create a named pipe using mkfifo(1) and start your
python script which should attach itself to one end of the pipe. The
second solution is bit more complex: you need a new device. The
device driver will only forward all input to your user space daemon
or read all output from it. This way your python script can be
started automagically each time someone accesses the device special
file.

And I'm sure there is a whole bunch of other solutions...

Mathias
 
S

Scott Chapman

The solution is very easy. There are at least two ways. First the
easier one: create a named pipe using mkfifo(1) and start your
python script which should attach itself to one end of the pipe. The
second solution is bit more complex: you need a new device. The
device driver will only forward all input to your user space daemon
or read all output from it. This way your python script can be
started automagically each time someone accesses the device special
file.

I understand named pipes to be "one-way" only. Is that correct?

Scott
 
M

Mathias Waack

Scott said:
I understand named pipes to be "one-way" only. Is that correct?

Not for Linux. Linux named pipes are bidirectional. I assume this
holds for all unices but I'm not sure.

Mathias
 
G

Gary D. Duzan

Not for Linux. Linux named pipes are bidirectional. I assume this
holds for all unices but I'm not sure.

No, it doesn't hold everywhere. A more portable bi-directional
construct would be a UNIX/LOCAL domain socket bound to a file.

Gary Duzan
BBN Technologies
A Verizon Company
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top