paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@5 | 3 | """ |
paulb@5 | 4 | Simple desktop integration for Python. This module provides desktop environment |
paulb@5 | 5 | detection and resource opening support for a selection of common and |
paulb@5 | 6 | standardised desktop environments. |
paulb@5 | 7 | |
paulb@27 | 8 | Copyright (C) 2005, 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@12 | 9 | |
paulb@24 | 10 | This library is free software; you can redistribute it and/or |
paulb@24 | 11 | modify it under the terms of the GNU Lesser General Public |
paulb@24 | 12 | License as published by the Free Software Foundation; either |
paulb@24 | 13 | version 2.1 of the License, or (at your option) any later version. |
paulb@24 | 14 | |
paulb@24 | 15 | This library is distributed in the hope that it will be useful, |
paulb@24 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@24 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@24 | 18 | Lesser General Public License for more details. |
paulb@24 | 19 | |
paulb@24 | 20 | You should have received a copy of the GNU Lesser General Public |
paulb@24 | 21 | License along with this library; if not, write to the Free Software |
paulb@24 | 22 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@12 | 23 | |
paulb@12 | 24 | -------- |
paulb@12 | 25 | |
paulb@7 | 26 | Desktop Detection |
paulb@7 | 27 | ----------------- |
paulb@7 | 28 | |
paulb@5 | 29 | To detect a specific desktop environment, use the get_desktop function. |
paulb@7 | 30 | To detect whether the desktop environment is standardised (according to the |
paulb@5 | 31 | proposed DESKTOP_LAUNCH standard), use the is_standard function. |
paulb@5 | 32 | |
paulb@7 | 33 | Opening URLs |
paulb@7 | 34 | ------------ |
paulb@7 | 35 | |
paulb@7 | 36 | To open a URL in the current desktop environment, relying on the automatic |
paulb@7 | 37 | detection of that environment, use the desktop.open function as follows: |
paulb@7 | 38 | |
paulb@7 | 39 | desktop.open("http://www.python.org") |
paulb@7 | 40 | |
paulb@7 | 41 | To override the detected desktop, specify the desktop parameter to the open |
paulb@7 | 42 | function as follows: |
paulb@7 | 43 | |
paulb@7 | 44 | desktop.open("http://www.python.org", "KDE") # Insists on KDE |
paulb@7 | 45 | desktop.open("http://www.python.org", "GNOME") # Insists on GNOME |
paulb@7 | 46 | |
paulb@7 | 47 | Without overriding using the desktop parameter, the open function will attempt |
paulb@7 | 48 | to use the "standard" desktop opening mechanism which is controlled by the |
paulb@7 | 49 | DESKTOP_LAUNCH environment variable as described below. |
paulb@7 | 50 | |
paulb@31 | 51 | Opening Dialogue Boxes (Dialogs) |
paulb@31 | 52 | -------------------------------- |
paulb@31 | 53 | |
paulb@31 | 54 | To open a dialogue box (dialog) in the current desktop environment, relying on |
paulb@31 | 55 | the automatic detection of that environment, use the desktop.dialog function as |
paulb@31 | 56 | follows: |
paulb@31 | 57 | |
paulb@31 | 58 | desktop.dialog("question", text="Are you sure?") |
paulb@31 | 59 | |
paulb@31 | 60 | To override the detected desktop, specify the desktop parameter to the dialog |
paulb@31 | 61 | function as follows: |
paulb@31 | 62 | |
paulb@31 | 63 | desktop.dialog("question", "KDE", text="Are you sure?") # Insists on KDE |
paulb@31 | 64 | desktop.dialog("question", "GNOME", text="Are you sure?") # Insists on GNOME |
paulb@31 | 65 | |
paulb@7 | 66 | The DESKTOP_LAUNCH Environment Variable |
paulb@7 | 67 | --------------------------------------- |
paulb@7 | 68 | |
paulb@7 | 69 | The DESKTOP_LAUNCH environment variable must be shell-quoted where appropriate, |
paulb@7 | 70 | as shown in some of the following examples: |
paulb@7 | 71 | |
paulb@7 | 72 | DESKTOP_LAUNCH="kdialog --msgbox" Should present any opened URLs in |
paulb@7 | 73 | their entirety in a KDE message box. |
paulb@7 | 74 | (Command "kdialog" plus parameter.) |
paulb@7 | 75 | DESKTOP_LAUNCH="my\ opener" Should run the "my opener" program to |
paulb@7 | 76 | open URLs. |
paulb@7 | 77 | (Command "my opener", no parameters.) |
paulb@7 | 78 | DESKTOP_LAUNCH="my\ opener --url" Should run the "my opener" program to |
paulb@7 | 79 | open URLs. |
paulb@7 | 80 | (Command "my opener" plus parameter.) |
paulb@7 | 81 | |
paulb@5 | 82 | Details of the DESKTOP_LAUNCH environment variable convention can be found here: |
paulb@5 | 83 | http://lists.freedesktop.org/archives/xdg/2004-August/004489.html |
paulb@5 | 84 | """ |
paulb@1 | 85 | |
paulb@31 | 86 | __version__ = "0.2.4" |
paulb@8 | 87 | |
paulb@1 | 88 | import os |
paulb@4 | 89 | import sys |
paulb@27 | 90 | |
paulb@27 | 91 | try: |
paulb@27 | 92 | import subprocess |
paulb@27 | 93 | def _run(cmd, shell, wait): |
paulb@27 | 94 | opener = subprocess.Popen(cmd, shell=shell) |
paulb@27 | 95 | if wait: opener.wait() |
paulb@27 | 96 | return opener.pid |
paulb@27 | 97 | |
paulb@31 | 98 | def _readfrom(cmd, shell): |
paulb@31 | 99 | opener = subprocess.Popen(cmd, shell=shell, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
paulb@31 | 100 | opener.stdin.close() |
paulb@31 | 101 | return opener.stdout.read() |
paulb@31 | 102 | |
paulb@31 | 103 | def _status(cmd, shell): |
paulb@31 | 104 | opener = subprocess.Popen(cmd, shell=shell) |
paulb@31 | 105 | opener.wait() |
paulb@31 | 106 | return opener.returncode == 0 |
paulb@31 | 107 | |
paulb@27 | 108 | except ImportError: |
paulb@27 | 109 | import popen2 |
paulb@27 | 110 | def _run(cmd, shell, wait): |
paulb@27 | 111 | opener = popen2.Popen3(cmd) |
paulb@27 | 112 | if wait: opener.wait() |
paulb@27 | 113 | return opener.pid |
paulb@27 | 114 | |
paulb@31 | 115 | def _readfrom(cmd, shell): |
paulb@31 | 116 | opener = popen2.Popen3(cmd) |
paulb@31 | 117 | opener.tochild.close() |
paulb@31 | 118 | opener.childerr.close() |
paulb@31 | 119 | return opener.fromchild.read() |
paulb@31 | 120 | |
paulb@31 | 121 | def _status(cmd, shell): |
paulb@31 | 122 | opener = popen2.Popen3(cmd) |
paulb@31 | 123 | opener.wait() |
paulb@31 | 124 | return opener.poll() == 0 |
paulb@31 | 125 | |
paulb@6 | 126 | import commands |
paulb@1 | 127 | |
paulb@31 | 128 | # Internal data. |
paulb@31 | 129 | |
paulb@31 | 130 | _dialog_commands = { |
paulb@31 | 131 | "KDE" : "kdialog", |
paulb@31 | 132 | "GNOME" : "zenity", |
paulb@31 | 133 | "X11" : "Xdialog" |
paulb@31 | 134 | } |
paulb@31 | 135 | |
paulb@31 | 136 | _dialog_options = { |
paulb@31 | 137 | "KDE" : { |
paulb@31 | 138 | "question" : (_status, ["--yesno"], ["text"]), |
paulb@31 | 139 | "message" : (_status, ["--msgbox"], ["text"]), |
paulb@31 | 140 | "input" : (_readfrom, ["--inputbox"], ["text", "input"]), |
paulb@31 | 141 | "password" : (_readfrom, ["--password"], ["text"]), |
paulb@31 | 142 | "textfile" : (_readfrom, ["--textbox"], ["filename", "width", "height"]), |
paulb@31 | 143 | "menu" : (_readfrom, ["--menu"], ["text", "entries2"]), |
paulb@31 | 144 | "radiolist" : (_readfrom, ["--radiolist"], ["text", "entries3"]), |
paulb@31 | 145 | "checklist" : (_readfrom, ["--checklist"], ["text", "entries3"]), |
paulb@31 | 146 | "pulldown" : (_readfrom, ["--combobox"], ["text", "entries1"]), |
paulb@31 | 147 | }, |
paulb@31 | 148 | "X11" : { |
paulb@31 | 149 | "question" : (_status, ["--stdout", "--yesno"], ["text", "height", "width"]), |
paulb@31 | 150 | "message" : (_status, ["--stdout", "--msgbox"], ["text", "height", "width"]), |
paulb@31 | 151 | "input" : (_readfrom, ["--stdout", "--inputbox"], ["text", "height", "width", "input"]), |
paulb@31 | 152 | "password" : (_readfrom, ["--stdout", "--password", "--inputbox"], ["text", "height", "width"]), |
paulb@31 | 153 | "textfile" : (_readfrom, ["--stdout", "--textbox"], ["text", "height", "width"]), |
paulb@31 | 154 | "menu" : (_readfrom, ["--stdout", "--menubox"], ["text", "height", "width", "menu_height", "entries2"]), |
paulb@31 | 155 | "radiolist" : (_readfrom, ["--stdout", "--radiolist"], ["text", "height", "width", "list_height", "entries3"]), |
paulb@31 | 156 | "checklist" : (_readfrom, ["--stdout", "--checklist"], ["text", "height", "width", "list_height", "entries3"]), |
paulb@31 | 157 | "pulldown" : (_readfrom, ["--stdout", "--combobox"], ["text", "height", "width", "entries1"]), |
paulb@31 | 158 | }, |
paulb@31 | 159 | } |
paulb@31 | 160 | |
paulb@31 | 161 | _dialog_defaults = { |
paulb@31 | 162 | "width" : 40, "height" : 30, "menu_height" : 20, "list_height" : 20 |
paulb@31 | 163 | } |
paulb@31 | 164 | |
paulb@31 | 165 | # Introspection functions. |
paulb@31 | 166 | |
paulb@5 | 167 | def get_desktop(): |
paulb@5 | 168 | |
paulb@5 | 169 | """ |
paulb@5 | 170 | Detect the current desktop environment, returning the name of the |
paulb@5 | 171 | environment. If no environment could be detected, None is returned. |
paulb@5 | 172 | """ |
paulb@5 | 173 | |
paulb@5 | 174 | if os.environ.has_key("KDE_FULL_SESSION") or \ |
paulb@5 | 175 | os.environ.has_key("KDE_MULTIHEAD"): |
paulb@5 | 176 | return "KDE" |
paulb@5 | 177 | elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \ |
paulb@5 | 178 | os.environ.has_key("GNOME_KEYRING_SOCKET"): |
paulb@5 | 179 | return "GNOME" |
paulb@5 | 180 | elif sys.platform == "darwin": |
paulb@5 | 181 | return "Mac OS X" |
paulb@5 | 182 | elif hasattr(os, "startfile"): |
paulb@5 | 183 | return "Windows" |
paulb@31 | 184 | elif os.environ.has_key("DISPLAY"): |
paulb@31 | 185 | return "X11" |
paulb@31 | 186 | else: |
paulb@31 | 187 | return None |
paulb@31 | 188 | |
paulb@31 | 189 | def use_desktop(desktop): |
paulb@31 | 190 | |
paulb@31 | 191 | """ |
paulb@31 | 192 | Decide which desktop should be used, based on the detected desktop and a |
paulb@31 | 193 | supplied 'desktop' argument (which may be None). Return an identifier |
paulb@31 | 194 | indicating the desktop type as being either "standard" or one of the results |
paulb@31 | 195 | from the 'get_desktop' function. |
paulb@31 | 196 | """ |
paulb@31 | 197 | |
paulb@31 | 198 | # Attempt to detect a desktop environment. |
paulb@31 | 199 | |
paulb@31 | 200 | detected = get_desktop() |
paulb@31 | 201 | |
paulb@31 | 202 | # Start with desktops whose existence can be easily tested. |
paulb@31 | 203 | |
paulb@31 | 204 | if (desktop is None or desktop == "standard") and is_standard(): |
paulb@31 | 205 | return "standard" |
paulb@31 | 206 | elif (desktop is None or desktop == "Windows") and detected == "Windows": |
paulb@31 | 207 | return "Windows" |
paulb@31 | 208 | |
paulb@31 | 209 | # Test for desktops where the overriding is not verified. |
paulb@31 | 210 | |
paulb@31 | 211 | elif (desktop or detected) == "KDE": |
paulb@31 | 212 | return "KDE" |
paulb@31 | 213 | elif (desktop or detected) == "GNOME": |
paulb@31 | 214 | return "GNOME" |
paulb@31 | 215 | elif (desktop or detected) == "Mac OS X": |
paulb@31 | 216 | return "Mac OS X" |
paulb@31 | 217 | elif (desktop or detected) == "X11": |
paulb@31 | 218 | return "X11" |
paulb@5 | 219 | else: |
paulb@5 | 220 | return None |
paulb@5 | 221 | |
paulb@5 | 222 | def is_standard(): |
paulb@5 | 223 | |
paulb@5 | 224 | """ |
paulb@5 | 225 | Return whether the current desktop supports standardised application |
paulb@5 | 226 | launching. |
paulb@5 | 227 | """ |
paulb@5 | 228 | |
paulb@5 | 229 | return os.environ.has_key("DESKTOP_LAUNCH") |
paulb@5 | 230 | |
paulb@31 | 231 | # Activity functions. |
paulb@31 | 232 | |
paulb@18 | 233 | def open(url, desktop=None, wait=0): |
paulb@1 | 234 | |
paulb@1 | 235 | """ |
paulb@1 | 236 | Open the 'url' in the current desktop's preferred file browser. If the |
paulb@1 | 237 | optional 'desktop' parameter is specified then attempt to use that |
paulb@1 | 238 | particular desktop environment's mechanisms to open the 'url' instead of |
paulb@1 | 239 | guessing or detecting which environment is being used. |
paulb@1 | 240 | |
paulb@5 | 241 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X", |
paulb@5 | 242 | "Windows" where "standard" employs a DESKTOP_LAUNCH environment variable to |
paulb@6 | 243 | open the specified 'url'. DESKTOP_LAUNCH should be a command, possibly |
paulb@31 | 244 | followed by arguments, and must have any special characters shell-escaped. |
paulb@3 | 245 | |
paulb@3 | 246 | The process identifier of the "opener" (ie. viewer, editor, browser or |
paulb@3 | 247 | program) associated with the 'url' is returned by this function. If the |
paulb@3 | 248 | process identifier cannot be determined, None is returned. |
paulb@16 | 249 | |
paulb@16 | 250 | An optional 'wait' parameter is also available for advanced usage and, if |
paulb@18 | 251 | 'wait' is set to a true value, this function will wait for the launching |
paulb@18 | 252 | mechanism to complete before returning (as opposed to immediately returning |
paulb@18 | 253 | as is the default behaviour). |
paulb@1 | 254 | """ |
paulb@1 | 255 | |
paulb@31 | 256 | # Decide on the desktop environment in use. |
paulb@5 | 257 | |
paulb@31 | 258 | desktop_in_use = use_desktop(desktop) |
paulb@4 | 259 | |
paulb@31 | 260 | if desktop_in_use == "standard": |
paulb@6 | 261 | arg = "".join([os.environ["DESKTOP_LAUNCH"], commands.mkarg(url)]) |
paulb@27 | 262 | return _run(arg, 1, wait) |
paulb@4 | 263 | |
paulb@31 | 264 | elif desktop_in_use == "Windows": |
paulb@5 | 265 | # NOTE: This returns None in current implementations. |
paulb@5 | 266 | return os.startfile(url) |
paulb@1 | 267 | |
paulb@31 | 268 | elif desktop_in_use == "KDE": |
paulb@4 | 269 | cmd = ["kfmclient", "exec", url] |
paulb@1 | 270 | |
paulb@31 | 271 | elif desktop_in_use == "GNOME": |
paulb@1 | 272 | cmd = ["gnome-open", url] |
paulb@1 | 273 | |
paulb@31 | 274 | elif desktop_in_use == "Mac OS X": |
paulb@5 | 275 | cmd = ["open", url] |
paulb@4 | 276 | |
paulb@5 | 277 | # Finish with an error where no suitable desktop was identified. |
paulb@4 | 278 | |
paulb@1 | 279 | else: |
paulb@5 | 280 | raise OSError, "Desktop not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" |
paulb@1 | 281 | |
paulb@27 | 282 | return _run(cmd, 0, wait) |
paulb@1 | 283 | |
paulb@31 | 284 | def dialog(dialog_type, desktop=None, **options): |
paulb@31 | 285 | |
paulb@31 | 286 | """ |
paulb@31 | 287 | Open a dialogue box (dialog) using a program appropriate to the desktop |
paulb@31 | 288 | environment in use. The specified 'dialog_type' may be one of the following: |
paulb@31 | 289 | |
paulb@31 | 290 | question A dialogue asking a question and showing response buttons. |
paulb@31 | 291 | Options: text, width (in characters), height (in characters) |
paulb@31 | 292 | |
paulb@31 | 293 | message A message dialogue. |
paulb@31 | 294 | Options: text, level ("warning", "error", "info") |
paulb@31 | 295 | |
paulb@31 | 296 | menu A menu of options, one of which being selectable. |
paulb@31 | 297 | Options: text, width (in characters), height (in characters), |
paulb@31 | 298 | menu_height (in entries), entries (list of (value, text) tuples) |
paulb@31 | 299 | |
paulb@31 | 300 | radiolist A list of radio buttons, one of which being selectable. |
paulb@31 | 301 | Options: text, width (in characters), height (in characters), |
paulb@31 | 302 | list_height (in entries), entries (list of (value, text, status) |
paulb@31 | 303 | tuples) |
paulb@31 | 304 | |
paulb@31 | 305 | checklist A list of checkboxes, many being selectable. |
paulb@31 | 306 | Options: text, width (in characters), height (in characters), |
paulb@31 | 307 | list_height (in entries), entries (list of (value, text, status) |
paulb@31 | 308 | tuples) |
paulb@31 | 309 | |
paulb@31 | 310 | pulldown A pull-down menu of options, one of which being selectable. |
paulb@31 | 311 | Options: text, width (in characters), height (in characters), |
paulb@31 | 312 | entries (list of values) |
paulb@31 | 313 | |
paulb@31 | 314 | input An input dialogue, consisting of an input field. |
paulb@31 | 315 | Options: text, width (in characters), height (in characters), |
paulb@31 | 316 | input |
paulb@31 | 317 | |
paulb@31 | 318 | password A password dialogue, consisting of a password entry field. |
paulb@31 | 319 | Options: text, width (in characters), height (in characters), |
paulb@31 | 320 | input |
paulb@31 | 321 | |
paulb@31 | 322 | textfile A text file input box. |
paulb@31 | 323 | Options: text, width (in characters), height (in characters), |
paulb@31 | 324 | filename |
paulb@31 | 325 | |
paulb@31 | 326 | If the optional 'desktop' parameter is specified then attempt to use that |
paulb@31 | 327 | particular desktop environment's mechanisms to open the 'url' instead of |
paulb@31 | 328 | guessing or detecting which environment is being used. |
paulb@31 | 329 | |
paulb@31 | 330 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X", |
paulb@31 | 331 | "Windows". |
paulb@31 | 332 | |
paulb@31 | 333 | The result of the dialogue interaction may be a string indicating user |
paulb@31 | 334 | input (for input, password, menu, radiolist, pulldown), a list of strings |
paulb@31 | 335 | indicating selections of one or more items (for checklist), or a value |
paulb@31 | 336 | indicating true or false (for question). |
paulb@31 | 337 | """ |
paulb@31 | 338 | |
paulb@31 | 339 | # Decide on the desktop environment in use. |
paulb@31 | 340 | |
paulb@31 | 341 | desktop_in_use = use_desktop(desktop) |
paulb@31 | 342 | |
paulb@31 | 343 | # Get the base command. |
paulb@31 | 344 | |
paulb@31 | 345 | cmd = _dialog_commands[desktop_in_use] |
paulb@31 | 346 | |
paulb@31 | 347 | # Get the handler for the command, along with required command options and |
paulb@31 | 348 | # the fields that the command expects. |
paulb@31 | 349 | |
paulb@31 | 350 | handler, cmd_options, fields = _dialog_options[desktop_in_use][dialog_type] |
paulb@31 | 351 | |
paulb@31 | 352 | # Form a command list. |
paulb@31 | 353 | |
paulb@31 | 354 | l = [cmd] + cmd_options |
paulb@31 | 355 | |
paulb@31 | 356 | # Process the fields, adding values if found or defaults if available. |
paulb@31 | 357 | |
paulb@31 | 358 | for field in fields: |
paulb@31 | 359 | if field[-1].isdigit(): |
paulb@31 | 360 | n = int(field[-1]) |
paulb@31 | 361 | field = field[:-1] |
paulb@31 | 362 | else: |
paulb@31 | 363 | n = 0 # not list |
paulb@31 | 364 | |
paulb@31 | 365 | if options.has_key(field): |
paulb@31 | 366 | if n == 0: |
paulb@31 | 367 | l.append(str(options[field])) |
paulb@31 | 368 | else: |
paulb@31 | 369 | values = options[field] |
paulb@31 | 370 | for value in values: |
paulb@31 | 371 | if n == 1: |
paulb@31 | 372 | l.append(str(value)) |
paulb@31 | 373 | else: |
paulb@31 | 374 | for i in range(0, n): |
paulb@31 | 375 | l.append(str(value[i])) |
paulb@31 | 376 | elif _dialog_defaults.has_key(field): |
paulb@31 | 377 | l.append(str(_dialog_defaults[field])) |
paulb@31 | 378 | else: |
paulb@31 | 379 | raise ValueError, "Field '%s' missing from options." % field |
paulb@31 | 380 | |
paulb@31 | 381 | print l |
paulb@31 | 382 | return handler(l, 0) |
paulb@31 | 383 | |
paulb@1 | 384 | # vim: tabstop=4 expandtab shiftwidth=4 |