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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott Chapman

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

    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

  • dmbkiwi

    #2
    Re: Can I hook a "file&quot ; to a python script?

    On Fri, 07 Nov 2003 20:39:49 -0800, Scott Chapman wrote:
    [color=blue]
    > 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[/color]

    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.rea d()

    or get each line in a list like this:

    file_line_list = file_object.rea dlines()

    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

    Comment

    • Alan Gauld

      #3
      Re: Can I hook a "file&quot ; to a python script?

      On Fri, 7 Nov 2003 20:39:49 -0800, Scott Chapman
      <scott_list@mis chko.com> wrote:[color=blue]
      > 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.[/color]

      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

      Comment

      • Alex Martelli

        #4
        Re: Can I hook a &quot;file&quot ; to a python script?

        Scott Chapman wrote:
        [color=blue]
        > 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.[/color]

        "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

        Comment

        • Mathias Waack

          #5
          Re: Can I hook a &quot;file&quot ; to a python script?

          Scott Chapman wrote:[color=blue]
          > 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.[/color]

          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

          Comment

          • Scott Chapman

            #6
            Re: Can I hook a &quot;file&quot ; to a python script?

            On Saturday 08 November 2003 02:01, Mathias Waack wrote:[color=blue]
            > Scott Chapman wrote:[color=green]
            > > 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.[/color]
            >
            > 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.[/color]

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

            Scott

            Comment

            • Mathias Waack

              #7
              Re: Can I hook a &quot;file&quot ; to a python script?

              Scott Chapman wrote:
              [color=blue]
              > I understand named pipes to be "one-way" only. Is that correct?[/color]

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

              Mathias

              Comment

              • Gary D. Duzan

                #8
                Re: Can I hook a &quot;file&quot ; to a python script?

                In article <2kqv71-9u4.ln1@valpo.d e>, Mathias Waack <M.Waack@gmx.de > wrote:[color=blue]
                >Scott Chapman wrote:
                >[color=green]
                >> I understand named pipes to be "one-way" only. Is that correct?[/color]
                >
                >Not for Linux. Linux named pipes are bidirectional. I assume this
                >holds for all unices but I'm not sure.[/color]

                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


                Comment

                Working...