Checking for X availability

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Flavio codeco coelho

    #1

    Checking for X availability

    I have a program that uses pythondialog for its UI.

    Pythondialog is a wrapper of the shell dialog and xdialog libs.

    But I would like for it to switch between using Dialog ( when X is not
    available ) and xdialog (when X is available)

    So my question is: how can I check for the availability of X? i.e.,
    How will my program know if its running in a text only console or in
    console window over X?

    thanks,

    Flávio
  • Nils Nordman

    #2
    Re: Checking for X availability

    On Tue, Jan 11, 2005 at 03:32:01AM -0800, Flavio codeco coelho wrote:[color=blue]
    > So my question is: how can I check for the availability of X? i.e.,
    > How will my program know if its running in a text only console or in
    > console window over X?[/color]

    Well, one way to do it is to check whether the environment variable
    DISPLAY is set (which it is when running under X, and should not be
    otherwise).

    Cheers,

    --
    Nils Nordman <nino@nordman.o rg>

    Comment

    • Jeremy Bowers

      #3
      Re: Checking for X availability

      On Tue, 11 Jan 2005 03:32:01 -0800, Flavio codeco coelho wrote:[color=blue]
      > So my question is: how can I check for the availability of X? i.e., How
      > will my program know if its running in a text only console or in console
      > window over X?[/color]

      The first thing that leaps to mind is... try it. If it fails, switch to
      the other. Remember modules are just variables and can be re-assigned, if
      needed.

      try:
      import xdialog as mydialog
      except ImportError:
      # user must not have it
      import dialog as mydialog

      try:
      # do something with mydialog here
      except YouDontHaveXErr or:
      # tried to use xdialog, but it failed
      import dialog as mydialog

      etc.

      That won't be useful, and I don't know what exception will be thrown when
      X isn't found, but you should get something. This should be invisible to
      the user, and is also the most transparent way to get it right; using
      environment vars and such may not be the best way to do it.

      This, by the way, was assuming that xdialog and dialog have identical
      APIs. If that is not the case, I'd suggest "process-level recursion"; try
      starting up with X, and if it fails, os.execl your process again with a
      command line parameter to use console dialog. That will be somewhat
      simpler than trying to detect it inside the program itself.

      Comment

      • Dan Stromberg

        #4
        Re: Checking for X availability

        On Tue, 11 Jan 2005 03:32:01 -0800, Flavio codeco coelho wrote:
        [color=blue]
        > I have a program that uses pythondialog for its UI.
        >
        > Pythondialog is a wrapper of the shell dialog and xdialog libs.
        >
        > But I would like for it to switch between using Dialog ( when X is not
        > available ) and xdialog (when X is available)
        >
        > So my question is: how can I check for the availability of X? i.e.,
        > How will my program know if its running in a text only console or in
        > console window over X?
        >
        > thanks,
        >
        > Flávio[/color]

        If there's interest, I could make available my "pdialog" module, which is
        also a wrapper around *dialog, but uses X when available, and curses when
        it is not.

        Comment

        • Cameron Laird

          #5
          Re: Checking for X availability

          In article <mailman.508.11 05443600.22381. python-list@python.org >,
          Nils Nordman <nino@nordman.o rg> wrote:[color=blue]
          >On Tue, Jan 11, 2005 at 03:32:01AM -0800, Flavio codeco coelho wrote:[color=green]
          >> So my question is: how can I check for the availability of X? i.e.,
          >> How will my program know if its running in a text only console or in
          >> console window over X?[/color]
          >
          >Well, one way to do it is to check whether the environment variable
          >DISPLAY is set (which it is when running under X, and should not be
          >otherwise).[/color]

          Comment

          Working...