1.1 --- a/desktop.py Sun Aug 27 00:02:00 2006 +0000
1.2 +++ b/desktop.py Wed Oct 04 20:50:35 2006 +0000
1.3 @@ -48,21 +48,6 @@
1.4 to use the "standard" desktop opening mechanism which is controlled by the
1.5 DESKTOP_LAUNCH environment variable as described below.
1.6
1.7 -Opening Dialogue Boxes (Dialogs)
1.8 ---------------------------------
1.9 -
1.10 -To open a dialogue box (dialog) in the current desktop environment, relying on
1.11 -the automatic detection of that environment, use the desktop.dialog function as
1.12 -follows:
1.13 -
1.14 -desktop.dialog("question", text="Are you sure?")
1.15 -
1.16 -To override the detected desktop, specify the desktop parameter to the dialog
1.17 -function as follows:
1.18 -
1.19 -desktop.dialog("question", "KDE", text="Are you sure?") # Insists on KDE
1.20 -desktop.dialog("question", "GNOME", text="Are you sure?") # Insists on GNOME
1.21 -
1.22 The DESKTOP_LAUNCH Environment Variable
1.23 ---------------------------------------
1.24
1.25 @@ -88,6 +73,8 @@
1.26 import os
1.27 import sys
1.28
1.29 +# Provide suitable process creation functions.
1.30 +
1.31 try:
1.32 import subprocess
1.33 def _run(cmd, shell, wait):
1.34 @@ -125,43 +112,6 @@
1.35
1.36 import commands
1.37
1.38 -# Internal data.
1.39 -
1.40 -_dialog_commands = {
1.41 - "KDE" : "kdialog",
1.42 - "GNOME" : "zenity",
1.43 - "X11" : "Xdialog"
1.44 - }
1.45 -
1.46 -_dialog_options = {
1.47 - "KDE" : {
1.48 - "question" : (_status, ["--yesno"], ["text"]),
1.49 - "message" : (_status, ["--msgbox"], ["text"]),
1.50 - "input" : (_readfrom, ["--inputbox"], ["text", "input"]),
1.51 - "password" : (_readfrom, ["--password"], ["text"]),
1.52 - "textfile" : (_readfrom, ["--textbox"], ["filename", "width", "height"]),
1.53 - "menu" : (_readfrom, ["--menu"], ["text", "entries2"]),
1.54 - "radiolist" : (_readfrom, ["--radiolist"], ["text", "entries3"]),
1.55 - "checklist" : (_readfrom, ["--checklist"], ["text", "entries3"]),
1.56 - "pulldown" : (_readfrom, ["--combobox"], ["text", "entries1"]),
1.57 - },
1.58 - "X11" : {
1.59 - "question" : (_status, ["--stdout", "--yesno"], ["text", "height", "width"]),
1.60 - "message" : (_status, ["--stdout", "--msgbox"], ["text", "height", "width"]),
1.61 - "input" : (_readfrom, ["--stdout", "--inputbox"], ["text", "height", "width", "input"]),
1.62 - "password" : (_readfrom, ["--stdout", "--password", "--inputbox"], ["text", "height", "width"]),
1.63 - "textfile" : (_readfrom, ["--stdout", "--textbox"], ["text", "height", "width"]),
1.64 - "menu" : (_readfrom, ["--stdout", "--menubox"], ["text", "height", "width", "menu_height", "entries2"]),
1.65 - "radiolist" : (_readfrom, ["--stdout", "--radiolist"], ["text", "height", "width", "list_height", "entries3"]),
1.66 - "checklist" : (_readfrom, ["--stdout", "--checklist"], ["text", "height", "width", "list_height", "entries3"]),
1.67 - "pulldown" : (_readfrom, ["--stdout", "--combobox"], ["text", "height", "width", "entries1"]),
1.68 - },
1.69 - }
1.70 -
1.71 -_dialog_defaults = {
1.72 - "width" : 40, "height" : 30, "menu_height" : 20, "list_height" : 20
1.73 - }
1.74 -
1.75 # Introspection functions.
1.76
1.77 def get_desktop():
1.78 @@ -277,108 +227,8 @@
1.79 # Finish with an error where no suitable desktop was identified.
1.80
1.81 else:
1.82 - raise OSError, "Desktop not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)"
1.83 + raise OSError, "Desktop '%s' not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" % desktop_in_use
1.84
1.85 return _run(cmd, 0, wait)
1.86
1.87 -def dialog(dialog_type, desktop=None, **options):
1.88 -
1.89 - """
1.90 - Open a dialogue box (dialog) using a program appropriate to the desktop
1.91 - environment in use. The specified 'dialog_type' may be one of the following:
1.92 -
1.93 - question A dialogue asking a question and showing response buttons.
1.94 - Options: text, width (in characters), height (in characters)
1.95 -
1.96 - message A message dialogue.
1.97 - Options: text, level ("warning", "error", "info")
1.98 -
1.99 - menu A menu of options, one of which being selectable.
1.100 - Options: text, width (in characters), height (in characters),
1.101 - menu_height (in entries), entries (list of (value, text) tuples)
1.102 -
1.103 - radiolist A list of radio buttons, one of which being selectable.
1.104 - Options: text, width (in characters), height (in characters),
1.105 - list_height (in entries), entries (list of (value, text, status)
1.106 - tuples)
1.107 -
1.108 - checklist A list of checkboxes, many being selectable.
1.109 - Options: text, width (in characters), height (in characters),
1.110 - list_height (in entries), entries (list of (value, text, status)
1.111 - tuples)
1.112 -
1.113 - pulldown A pull-down menu of options, one of which being selectable.
1.114 - Options: text, width (in characters), height (in characters),
1.115 - entries (list of values)
1.116 -
1.117 - input An input dialogue, consisting of an input field.
1.118 - Options: text, width (in characters), height (in characters),
1.119 - input
1.120 -
1.121 - password A password dialogue, consisting of a password entry field.
1.122 - Options: text, width (in characters), height (in characters),
1.123 - input
1.124 -
1.125 - textfile A text file input box.
1.126 - Options: text, width (in characters), height (in characters),
1.127 - filename
1.128 -
1.129 - If the optional 'desktop' parameter is specified then attempt to use that
1.130 - particular desktop environment's mechanisms to open the 'url' instead of
1.131 - guessing or detecting which environment is being used.
1.132 -
1.133 - Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X",
1.134 - "Windows".
1.135 -
1.136 - The result of the dialogue interaction may be a string indicating user
1.137 - input (for input, password, menu, radiolist, pulldown), a list of strings
1.138 - indicating selections of one or more items (for checklist), or a value
1.139 - indicating true or false (for question).
1.140 - """
1.141 -
1.142 - # Decide on the desktop environment in use.
1.143 -
1.144 - desktop_in_use = use_desktop(desktop)
1.145 -
1.146 - # Get the base command.
1.147 -
1.148 - cmd = _dialog_commands[desktop_in_use]
1.149 -
1.150 - # Get the handler for the command, along with required command options and
1.151 - # the fields that the command expects.
1.152 -
1.153 - handler, cmd_options, fields = _dialog_options[desktop_in_use][dialog_type]
1.154 -
1.155 - # Form a command list.
1.156 -
1.157 - l = [cmd] + cmd_options
1.158 -
1.159 - # Process the fields, adding values if found or defaults if available.
1.160 -
1.161 - for field in fields:
1.162 - if field[-1].isdigit():
1.163 - n = int(field[-1])
1.164 - field = field[:-1]
1.165 - else:
1.166 - n = 0 # not list
1.167 -
1.168 - if options.has_key(field):
1.169 - if n == 0:
1.170 - l.append(str(options[field]))
1.171 - else:
1.172 - values = options[field]
1.173 - for value in values:
1.174 - if n == 1:
1.175 - l.append(str(value))
1.176 - else:
1.177 - for i in range(0, n):
1.178 - l.append(str(value[i]))
1.179 - elif _dialog_defaults.has_key(field):
1.180 - l.append(str(_dialog_defaults[field]))
1.181 - else:
1.182 - raise ValueError, "Field '%s' missing from options." % field
1.183 -
1.184 - print l
1.185 - return handler(l, 0)
1.186 -
1.187 # vim: tabstop=4 expandtab shiftwidth=4