Testing for the presence of input from stdin.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will McDonald

    #1

    Testing for the presence of input from stdin.

    Hi all.

    I'm writing a little script that operates on either stdin or a file
    specified on the command line when run. I'm trying to handle the
    situation where the script's run without any input gracefully but
    can't think how to test for stdin.

    I can test for a file argument on the command line using getopt and
    validate its existence with os.path.exists. If it doesn't I can print
    the useage.

    I can get the script to behave as expected when content's piped to it
    using sys.stdin but I'd like to know that there's data coming from
    stdin or fail and print the useage again. Is there a simple way to
    achieve this?

    Thanks,

    Will.

    Here's what I've got so far...

    #!/usr/bin/python
    #
    # hail - heads and tails

    import sys, os, getopt

    def hail(file,headl ines=10,taillin es=10):
    lines = file.readlines( )
    sys.stdout.writ elines(lines[:headlines])
    sys.stdout.writ elines(lines[taillines:])

    def useage():
    print "Useage: hail [OPTION] [FILE]"
    print " -t, --top # lines from top (default 10)"
    print " -b, --bottom # lines from bottom (default 10)"
    print " -h, --help display this help and exit"

    def main():
    try:
    opts, args = getopt.getopt(s ys.argv[1:], "t:b:h",
    ['top=','bottom= ','help'])
    except getopt.GetoptEr ror:
    useage()
    sys.exit(2)
    for o,a in opts:
    if o in ("-t", "--top"):
    toplines = a
    if o in ("-b", "--bottom"):
    bottomlines = a
    if o in ("-h", "--help"):
    useage()
    sys.exit()
    if len(args) == 1 and os.path.exists( str(args[0])):
    file = (str(args[0]))
    # else:
    # file = sys.stdin
    hail(file,headl ines=toplines,t aillines=bottom lines)

    if __name__ == "__main__":
    main()
  • Diez B. Roggisch

    #2
    Re: Testing for the presence of input from stdin.

    > I can get the script to behave as expected when content's piped to it[color=blue]
    > using sys.stdin but I'd like to know that there's data coming from
    > stdin or fail and print the useage again. Is there a simple way to
    > achieve this?[/color]

    There are more experienced UNIXers here, but from my POV I don't see how
    that can happen. The reason is simply that

    - sys.stdin alwasy exists (unless you close it yourself)

    - in a pipe (which this essentially is) there is now way to know if there
    is more date to come or not, except for the "broken pipe" error - but that
    won't happen to you, as sys.stdin is not broken just because there is
    currently no data arriving.


    The only thing I can think of is to introduce a timeout. If no data has
    arrived for a certain amount of time, you terminate. That could be achieved
    using either threads, or non-blocking IO.

    However, it's a heuristic. If your aunt sally starts the program and wants
    to feed it manually, but is slow because she first has to fetch her glasses
    from the kitchen, she'll think you're nasty because you terminated her
    program...

    Diez

    Comment

    • Will McDonald

      #3
      Re: Testing for the presence of input from stdin.

      On 23/01/06, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=blue][color=green]
      > > I can get the script to behave as expected when content's piped to it
      > > using sys.stdin but I'd like to know that there's data coming from
      > > stdin or fail and print the useage again. Is there a simple way to
      > > achieve this?[/color]
      >
      > There are more experienced UNIXers here, but from my POV I don't see how
      > that can happen. The reason is simply that
      >
      > - sys.stdin alwasy exists (unless you close it yourself)
      >
      > - in a pipe (which this essentially is) there is now way to know if there
      > is more date to come or not, except for the "broken pipe" error - but that
      > won't happen to you, as sys.stdin is not broken just because there is
      > currently no data arriving.[/color]

      That's a good point. I did wonder if it'd just have to sit there
      waiting for input much like cat would. I think that's preferable, and
      simpler :), than implementing timeouts.

      Thanks.

      Will.

      Comment

      • Peter Gsellmann

        #4
        Re: Testing for the presence of input from stdin.

        Will McDonald wrote:
        [color=blue]
        > On 23/01/06, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=green][color=darkred]
        >> > I can get the script to behave as expected when content's piped to it
        >> > using sys.stdin but I'd like to know that there's data coming from
        >> > stdin or fail and print the useage again. Is there a simple way to
        >> > achieve this?[/color]
        >>
        >> There are more experienced UNIXers here, but from my POV I don't see how
        >> that can happen. The reason is simply that
        >>
        >> - sys.stdin alwasy exists (unless you close it yourself)
        >>
        >> - in a pipe (which this essentially is) there is now way to know if
        >> there
        >> is more date to come or not, except for the "broken pipe" error - but
        >> that won't happen to you, as sys.stdin is not broken just because there
        >> is currently no data arriving.[/color]
        >
        > That's a good point. I did wonder if it'd just have to sit there
        > waiting for input much like cat would. I think that's preferable, and
        > simpler :), than implementing timeouts.
        >[/color]
        In unix you can always use select.select() on files and pipes as sys.stdin
        is. Use a timout-value of 0 and you get the 'ready-state' of the file
        descriptor i.e. the presence of waiting input-data.

        Peter

        Comment

        • Roland Heiber

          #5
          Re: Testing for the presence of input from stdin.

          Will McDonald wrote:[color=blue]
          > Hi all.
          >
          > I'm writing a little script that operates on either stdin or a file
          > specified on the command line when run. I'm trying to handle the
          > situation where the script's run without any input gracefully but
          > can't think how to test for stdin.
          >[/color]

          Hi,

          maybe http://docs.python.org/lib/module-fileinput.html is useful for you:

          "This iterates over the lines of all files listed in sys.argv[1:],
          defaulting to sys.stdin if the list is empty. If a filename is '-', it
          is also replaced by sys.stdin. To specify an alternative list of
          filenames, pass it as the first argument to input(). A single file name
          is also allowed."

          HtH, Roland

          Comment

          • Will McDonald

            #6
            Re: Testing for the presence of input from stdin.

            On 24/01/06, Roland Heiber <newstonne@web. de> wrote:[color=blue]
            > Will McDonald wrote:[color=green]
            > > Hi all.
            > >
            > > I'm writing a little script that operates on either stdin or a file
            > > specified on the command line when run. I'm trying to handle the
            > > situation where the script's run without any input gracefully but
            > > can't think how to test for stdin.[/color]
            >
            > maybe http://docs.python.org/lib/module-fileinput.html is useful for you:[/color]

            That looks ideal, I'll have a play with it to see how it behaves in
            conjunction with getopt. Thanks Roland. Thanks Peter and Diez for your
            input too.

            Will.

            Comment

            • Fredrik Lundh

              #7
              Re: Testing for the presence of input from stdin.

              Will McDonald wrote:
              [color=blue][color=green]
              > > There are more experienced UNIXers here, but from my POV I don't see how
              > > that can happen. The reason is simply that
              > >
              > > - sys.stdin alwasy exists (unless you close it yourself)
              > >
              > > - in a pipe (which this essentially is) there is now way to know if there
              > > is more date to come or not, except for the "broken pipe" error - but that
              > > won't happen to you, as sys.stdin is not broken just because there is
              > > currently no data arriving.[/color]
              >
              > That's a good point. I did wonder if it'd just have to sit there
              > waiting for input much like cat would. I think that's preferable, and
              > simpler :), than implementing timeouts.[/color]

              the usual way to implement this is to treat the filename "-" as stdin.

              if filename == "-":
              f = sys.stdin
              else:
              f = open(filename)

              ... read from f ...

              if f is not sys.stdin:
              f.close()

              </F>



              Comment

              • Thomas Bellman

                #8
                Re: Testing for the presence of input from stdin.

                Peter Gsellmann <peter-gsellmann@eunet .at> writes:
                [color=blue]
                > Will McDonald wrote:[/color]
                [color=blue][color=green]
                >> That's a good point. I did wonder if it'd just have to sit there
                >> waiting for input much like cat would. I think that's preferable, and
                >> simpler :), than implementing timeouts.
                >>[/color]
                > In unix you can always use select.select() on files and pipes as sys.stdin
                > is. Use a timout-value of 0 and you get the 'ready-state' of the file
                > descriptor i.e. the presence of waiting input-data.[/color]

                If you terminate when select() indicates that there is nothing
                more to read, you will terminate prematurely in many cases. Even
                'dd if=/dev/zero | myprogram.py' will stop at some random point,
                when the OS happens to decide that myprogram.py should be scheduled
                twice without dd getting the chance to fill the pipe buffer
                inbetween.


                --
                Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
                "We don't understand the software, and ! bellman @ lysator.liu.se
                sometimes we don't understand the hardware, !
                but we can *see* the blinking lights!" ! Make Love -- Nicht Wahr!

                Comment

                Working...