paulb@46 | 1 | #!/usr/bin/env python |
paulb@46 | 2 | |
paulb@46 | 3 | """ |
paulb@46 | 4 | Simple desktop dialogue box support for Python. |
paulb@46 | 5 | |
paulb@46 | 6 | Copyright (C) 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@46 | 7 | |
paulb@46 | 8 | This library is free software; you can redistribute it and/or |
paulb@46 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@46 | 10 | License as published by the Free Software Foundation; either |
paulb@46 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@46 | 12 | |
paulb@46 | 13 | This library is distributed in the hope that it will be useful, |
paulb@46 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@46 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@46 | 16 | Lesser General Public License for more details. |
paulb@46 | 17 | |
paulb@46 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@46 | 19 | License along with this library; if not, write to the Free Software |
paulb@46 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 21 | |
paulb@46 | 22 | -------- |
paulb@46 | 23 | |
paulb@46 | 24 | Opening Dialogue Boxes (Dialogs) |
paulb@46 | 25 | -------------------------------- |
paulb@46 | 26 | |
paulb@46 | 27 | To open a dialogue box (dialog) in the current desktop environment, relying on |
paulb@46 | 28 | the automatic detection of that environment, use the appropriate dialogue box |
paulb@46 | 29 | class: |
paulb@46 | 30 | |
paulb@46 | 31 | question = desktop.dialog.Question("Are you sure?") |
paulb@46 | 32 | question.open() |
paulb@46 | 33 | |
paulb@46 | 34 | To override the detected desktop, specify the desktop parameter to the open |
paulb@46 | 35 | function as follows: |
paulb@46 | 36 | |
paulb@46 | 37 | question.open("KDE") # Insists on KDE |
paulb@46 | 38 | question.open("GNOME") # Insists on GNOME |
paulb@46 | 39 | |
paulb@46 | 40 | The dialogue box options are documented in each class's docstring. |
paulb@46 | 41 | """ |
paulb@46 | 42 | |
paulb@46 | 43 | from desktop import use_desktop, _run, _readfrom, _status |
paulb@46 | 44 | |
paulb@46 | 45 | # Dialogue parameter classes. |
paulb@46 | 46 | |
paulb@46 | 47 | class String: |
paulb@46 | 48 | |
paulb@46 | 49 | "A generic parameter." |
paulb@46 | 50 | |
paulb@46 | 51 | def __init__(self, name): |
paulb@46 | 52 | self.name = name |
paulb@46 | 53 | |
paulb@46 | 54 | def convert(self, value, program): |
paulb@46 | 55 | return [value or ""] |
paulb@46 | 56 | |
paulb@46 | 57 | class Strings(String): |
paulb@46 | 58 | |
paulb@46 | 59 | "Multiple string parameters." |
paulb@46 | 60 | |
paulb@46 | 61 | def convert(self, value, program): |
paulb@46 | 62 | return value or [] |
paulb@46 | 63 | |
paulb@46 | 64 | class StringKeyword: |
paulb@46 | 65 | |
paulb@46 | 66 | "A keyword parameter." |
paulb@46 | 67 | |
paulb@46 | 68 | def __init__(self, keyword, name): |
paulb@46 | 69 | self.keyword = keyword |
paulb@46 | 70 | self.name = name |
paulb@46 | 71 | |
paulb@46 | 72 | def convert(self, value, program): |
paulb@46 | 73 | return [self.keyword + "=" + (value or "")] |
paulb@46 | 74 | |
paulb@46 | 75 | class StringKeywords: |
paulb@46 | 76 | |
paulb@46 | 77 | "Multiple keyword parameters." |
paulb@46 | 78 | |
paulb@46 | 79 | def __init__(self, keyword, name): |
paulb@46 | 80 | self.keyword = keyword |
paulb@46 | 81 | self.name = name |
paulb@46 | 82 | |
paulb@46 | 83 | def convert(self, value, program): |
paulb@46 | 84 | l = [] |
paulb@46 | 85 | for v in value or []: |
paulb@46 | 86 | l.append(self.keyword + "=" + v) |
paulb@46 | 87 | return l |
paulb@46 | 88 | |
paulb@46 | 89 | class Integer(String): |
paulb@46 | 90 | |
paulb@46 | 91 | "An integer parameter." |
paulb@46 | 92 | |
paulb@46 | 93 | defaults = { |
paulb@46 | 94 | "width" : 40, |
paulb@46 | 95 | "height" : 15, |
paulb@46 | 96 | "list_height" : 10 |
paulb@46 | 97 | } |
paulb@46 | 98 | |
paulb@46 | 99 | def convert(self, value, program): |
paulb@46 | 100 | if value is None: |
paulb@46 | 101 | value = self.defaults[self.name] |
paulb@46 | 102 | return [str(int(value))] |
paulb@46 | 103 | |
paulb@46 | 104 | class IntegerKeyword(Integer): |
paulb@46 | 105 | |
paulb@46 | 106 | "An integer keyword parameter." |
paulb@46 | 107 | |
paulb@46 | 108 | def __init__(self, keyword, name): |
paulb@46 | 109 | self.keyword = keyword |
paulb@46 | 110 | self.name = name |
paulb@46 | 111 | |
paulb@46 | 112 | def convert(self, value, program): |
paulb@46 | 113 | if value is None: |
paulb@46 | 114 | value = self.defaults[self.name] |
paulb@46 | 115 | return [self.keyword + "=" + str(int(value))] |
paulb@46 | 116 | |
paulb@46 | 117 | class Boolean(String): |
paulb@46 | 118 | |
paulb@46 | 119 | "A boolean parameter." |
paulb@46 | 120 | |
paulb@46 | 121 | values = { |
paulb@46 | 122 | "kdialog" : ["off", "on"], |
paulb@46 | 123 | "zenity" : ["FALSE", "TRUE"], |
paulb@46 | 124 | "Xdialog" : ["off", "on"] |
paulb@46 | 125 | } |
paulb@46 | 126 | |
paulb@46 | 127 | def convert(self, value, program): |
paulb@46 | 128 | values = self.values[program] |
paulb@46 | 129 | if value: |
paulb@46 | 130 | return [values[1]] |
paulb@46 | 131 | else: |
paulb@46 | 132 | return [values[0]] |
paulb@46 | 133 | |
paulb@46 | 134 | class MenuItemList(String): |
paulb@46 | 135 | |
paulb@46 | 136 | "A menu item list parameter." |
paulb@46 | 137 | |
paulb@46 | 138 | def convert(self, value, program): |
paulb@46 | 139 | l = [] |
paulb@46 | 140 | for v in value: |
paulb@46 | 141 | l.append(v.value) |
paulb@46 | 142 | l.append(v.text) |
paulb@46 | 143 | return l |
paulb@46 | 144 | |
paulb@46 | 145 | class ListItemList(String): |
paulb@46 | 146 | |
paulb@46 | 147 | "A menu item list parameter." |
paulb@46 | 148 | |
paulb@46 | 149 | def convert(self, value, program): |
paulb@46 | 150 | l = [] |
paulb@46 | 151 | for v in value: |
paulb@46 | 152 | l.append(v.value) |
paulb@46 | 153 | l.append(v.text) |
paulb@46 | 154 | boolean = Boolean(None) |
paulb@46 | 155 | l.append(boolean.convert(v.status, program)) |
paulb@46 | 156 | return l |
paulb@46 | 157 | |
paulb@46 | 158 | # Dialogue argument values. |
paulb@46 | 159 | |
paulb@46 | 160 | class MenuItem: |
paulb@46 | 161 | def __init__(self, value, text): |
paulb@46 | 162 | self.value = value |
paulb@46 | 163 | self.text = text |
paulb@46 | 164 | |
paulb@46 | 165 | class ListItem(MenuItem): |
paulb@46 | 166 | def __init__(self, value, text, status): |
paulb@46 | 167 | MenuItem.__init__(self, value, text) |
paulb@46 | 168 | self.status = status |
paulb@46 | 169 | |
paulb@46 | 170 | # Dialogue classes. |
paulb@46 | 171 | |
paulb@46 | 172 | class Dialogue: |
paulb@46 | 173 | |
paulb@46 | 174 | commands = { |
paulb@46 | 175 | "KDE" : "kdialog", |
paulb@46 | 176 | "GNOME" : "zenity", |
paulb@46 | 177 | "X11" : "Xdialog" |
paulb@46 | 178 | } |
paulb@46 | 179 | |
paulb@46 | 180 | def open(self, desktop=None): |
paulb@46 | 181 | |
paulb@46 | 182 | """ |
paulb@46 | 183 | Open a dialogue box (dialog) using a program appropriate to the desktop |
paulb@46 | 184 | environment in use. |
paulb@46 | 185 | |
paulb@46 | 186 | If the optional 'desktop' parameter is specified then attempt to use that |
paulb@46 | 187 | particular desktop environment's mechanisms to open the dialog instead of |
paulb@46 | 188 | guessing or detecting which environment is being used. |
paulb@46 | 189 | |
paulb@46 | 190 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X", |
paulb@46 | 191 | "Windows". |
paulb@46 | 192 | |
paulb@46 | 193 | The result of the dialogue interaction may be a string indicating user |
paulb@46 | 194 | input (for input, password, menu, radiolist, pulldown), a list of strings |
paulb@46 | 195 | indicating selections of one or more items (for checklist), or a value |
paulb@46 | 196 | indicating true or false (for question). |
paulb@46 | 197 | """ |
paulb@46 | 198 | |
paulb@46 | 199 | # Decide on the desktop environment in use. |
paulb@46 | 200 | |
paulb@46 | 201 | desktop_in_use = use_desktop(desktop) |
paulb@46 | 202 | |
paulb@46 | 203 | # Get the program. |
paulb@46 | 204 | |
paulb@46 | 205 | try: |
paulb@46 | 206 | program = self.commands[desktop_in_use] |
paulb@46 | 207 | except KeyError: |
paulb@46 | 208 | raise OSError, "Desktop '%s' not supported (no known dialogue box command could be suggested)" % desktop_in_use |
paulb@46 | 209 | |
paulb@46 | 210 | handler, options = self.info[program] |
paulb@46 | 211 | |
paulb@46 | 212 | cmd = [program] |
paulb@46 | 213 | for option in options: |
paulb@46 | 214 | if isinstance(option, str): |
paulb@46 | 215 | cmd.append(option) |
paulb@46 | 216 | else: |
paulb@46 | 217 | value = getattr(self, option.name, None) |
paulb@46 | 218 | cmd += option.convert(value, program) |
paulb@46 | 219 | |
paulb@46 | 220 | print cmd |
paulb@46 | 221 | return handler(cmd, 0) |
paulb@46 | 222 | |
paulb@46 | 223 | class Simple(Dialogue): |
paulb@46 | 224 | def __init__(self, text, width=None, height=None): |
paulb@46 | 225 | self.text = text |
paulb@46 | 226 | self.width = width |
paulb@46 | 227 | self.height = height |
paulb@46 | 228 | |
paulb@46 | 229 | class Question(Simple): |
paulb@46 | 230 | |
paulb@46 | 231 | """ |
paulb@46 | 232 | A dialogue asking a question and showing response buttons. |
paulb@46 | 233 | Options: text, width (in characters), height (in characters) |
paulb@46 | 234 | """ |
paulb@46 | 235 | |
paulb@46 | 236 | name = "question" |
paulb@46 | 237 | info = { |
paulb@46 | 238 | "kdialog" : (_status, ["--yesno", String("text")]), |
paulb@46 | 239 | "zenity" : (_status, ["--question", StringKeyword("--text", "text")]), |
paulb@46 | 240 | "Xdialog" : (_status, ["--stdout", "--yesno", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 241 | } |
paulb@46 | 242 | |
paulb@46 | 243 | class Warning(Simple): |
paulb@46 | 244 | |
paulb@46 | 245 | """ |
paulb@46 | 246 | A dialogue asking a question and showing response buttons. |
paulb@46 | 247 | Options: text, width (in characters), height (in characters) |
paulb@46 | 248 | """ |
paulb@46 | 249 | |
paulb@46 | 250 | name = "warning" |
paulb@46 | 251 | info = { |
paulb@46 | 252 | "kdialog" : (_status, ["--warningyesno", String("text")]), |
paulb@46 | 253 | "zenity" : (_status, ["--warning", StringKeyword("--text", "text")]), |
paulb@46 | 254 | "Xdialog" : (_status, ["--stdout", "--yesno", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 255 | } |
paulb@46 | 256 | |
paulb@46 | 257 | class Message(Simple): |
paulb@46 | 258 | |
paulb@46 | 259 | """ |
paulb@46 | 260 | A message dialogue. |
paulb@46 | 261 | Options: text, width (in characters), height (in characters) |
paulb@46 | 262 | """ |
paulb@46 | 263 | |
paulb@46 | 264 | name = "message" |
paulb@46 | 265 | info = { |
paulb@46 | 266 | "kdialog" : (_status, ["--msgbox", String("text")]), |
paulb@46 | 267 | "zenity" : (_status, ["--info", StringKeyword("--text", "text")]), |
paulb@46 | 268 | "Xdialog" : (_status, ["--stdout", "--msgbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 269 | } |
paulb@46 | 270 | |
paulb@46 | 271 | class Error(Simple): |
paulb@46 | 272 | |
paulb@46 | 273 | """ |
paulb@46 | 274 | An error dialogue. |
paulb@46 | 275 | Options: text, width (in characters), height (in characters) |
paulb@46 | 276 | """ |
paulb@46 | 277 | |
paulb@46 | 278 | name = "error" |
paulb@46 | 279 | info = { |
paulb@46 | 280 | "kdialog" : (_status, ["--error", String("text")]), |
paulb@46 | 281 | "zenity" : (_status, ["--error", StringKeyword("--text", "text")]), |
paulb@46 | 282 | "Xdialog" : (_status, ["--stdout", "--msgbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 283 | } |
paulb@46 | 284 | |
paulb@46 | 285 | class Menu(Simple): |
paulb@46 | 286 | |
paulb@46 | 287 | """ |
paulb@46 | 288 | A menu of options, one of which being selectable. |
paulb@46 | 289 | Options: text, width (in characters), height (in characters), |
paulb@46 | 290 | list_height (in items), items (MenuItem objects) |
paulb@46 | 291 | """ |
paulb@46 | 292 | |
paulb@46 | 293 | name = "menu" |
paulb@46 | 294 | info = { |
paulb@46 | 295 | "kdialog" : (_readfrom, ["--menu", String("text"), MenuItemList("items")]), |
paulb@46 | 296 | "zenity" : (_readfrom, ["--list", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@46 | 297 | MenuItemList("items")] |
paulb@46 | 298 | ), |
paulb@46 | 299 | "Xdialog" : (_readfrom, ["--stdout", "--menubox", |
paulb@46 | 300 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), MenuItemList("items")] |
paulb@46 | 301 | ), |
paulb@46 | 302 | } |
paulb@46 | 303 | |
paulb@46 | 304 | def __init__(self, text, titles, items, width=None, height=None, list_height=None): |
paulb@46 | 305 | Simple.__init__(self, text, width, height) |
paulb@46 | 306 | self.titles = titles |
paulb@46 | 307 | self.items = items |
paulb@46 | 308 | self.list_height = list_height |
paulb@46 | 309 | |
paulb@46 | 310 | class RadioList(Menu): |
paulb@46 | 311 | |
paulb@46 | 312 | """ |
paulb@46 | 313 | A list of radio buttons, one of which being selectable. |
paulb@46 | 314 | Options: text, width (in characters), height (in characters), |
paulb@46 | 315 | list_height (in items), items (ListItem objects), titles |
paulb@46 | 316 | """ |
paulb@46 | 317 | |
paulb@46 | 318 | name = "radiolist" |
paulb@46 | 319 | info = { |
paulb@46 | 320 | "kdialog" : (_readfrom, ["--radiolist", String("text"), ListItemList("items")]), |
paulb@46 | 321 | "zenity" : (_readfrom, |
paulb@46 | 322 | ["--list", "--radiolist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@46 | 323 | ListItemList("items")] |
paulb@46 | 324 | ), |
paulb@46 | 325 | "Xdialog" : (_readfrom, ["--stdout", "--radiolist", |
paulb@46 | 326 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), ListItemList("items")] |
paulb@46 | 327 | ), |
paulb@46 | 328 | } |
paulb@46 | 329 | |
paulb@46 | 330 | class CheckList(Menu): |
paulb@46 | 331 | |
paulb@46 | 332 | """ |
paulb@46 | 333 | A list of checkboxes, many being selectable. |
paulb@46 | 334 | Options: text, width (in characters), height (in characters), |
paulb@46 | 335 | list_height (in items), items (ListItem objects), titles |
paulb@46 | 336 | """ |
paulb@46 | 337 | |
paulb@46 | 338 | name = "checklist" |
paulb@46 | 339 | info = { |
paulb@46 | 340 | "kdialog" : (_readfrom, ["--checklist", String("text"), ListItemList("items")]), |
paulb@46 | 341 | "zenity" : (_readfrom, |
paulb@46 | 342 | ["--list", "--checklist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@46 | 343 | ListItemList("items")] |
paulb@46 | 344 | ), |
paulb@46 | 345 | "Xdialog" : (_readfrom, ["--stdout", "--checklist", |
paulb@46 | 346 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), ListItemList("items")] |
paulb@46 | 347 | ), |
paulb@46 | 348 | } |
paulb@46 | 349 | |
paulb@46 | 350 | class Pulldown(Menu): |
paulb@46 | 351 | |
paulb@46 | 352 | """ |
paulb@46 | 353 | A pull-down menu of options, one of which being selectable. |
paulb@46 | 354 | Options: text, width (in characters), height (in characters), |
paulb@46 | 355 | entries (list of values) |
paulb@46 | 356 | """ |
paulb@46 | 357 | |
paulb@46 | 358 | name = "pulldown" |
paulb@46 | 359 | info = { |
paulb@46 | 360 | "kdialog" : (_readfrom, ["--combobox", String("text"), Strings("items")]), |
paulb@46 | 361 | "zenity" : (_readfrom, ["--list", "--radiolist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@46 | 362 | Strings("items")] |
paulb@46 | 363 | ), |
paulb@46 | 364 | "Xdialog" : (_readfrom, ["--stdout", "--combobox", String("text"), Integer("height"), Integer("width"), Strings("items")]), |
paulb@46 | 365 | } |
paulb@46 | 366 | |
paulb@46 | 367 | class Input(Simple): |
paulb@46 | 368 | |
paulb@46 | 369 | """ |
paulb@46 | 370 | An input dialogue, consisting of an input field. |
paulb@46 | 371 | Options: text, width (in characters), height (in characters), |
paulb@46 | 372 | input |
paulb@46 | 373 | """ |
paulb@46 | 374 | |
paulb@46 | 375 | name = "input" |
paulb@46 | 376 | info = { |
paulb@46 | 377 | "kdialog" : (_readfrom, ["--inputbox", String("text"), String("data")]), |
paulb@46 | 378 | "zenity" : (_readfrom, ["--entry", StringKeyword("--text", "text"), StringKeyword("--entry-text", "data")]), |
paulb@46 | 379 | "Xdialog" : (_readfrom, ["--stdout", "--inputbox", String("text"), Integer("height"), Integer("width"), String("data")]), |
paulb@46 | 380 | } |
paulb@46 | 381 | |
paulb@46 | 382 | def __init__(self, text, data, width=None, height=None): |
paulb@46 | 383 | Simple.__init__(self, text, width, height) |
paulb@46 | 384 | self.data = data |
paulb@46 | 385 | |
paulb@46 | 386 | class Password(Input): |
paulb@46 | 387 | |
paulb@46 | 388 | """ |
paulb@46 | 389 | A password dialogue, consisting of a password entry field. |
paulb@46 | 390 | Options: text, width (in characters), height (in characters), |
paulb@46 | 391 | input |
paulb@46 | 392 | """ |
paulb@46 | 393 | |
paulb@46 | 394 | name = "password" |
paulb@46 | 395 | info = { |
paulb@46 | 396 | "kdialog" : (_readfrom, ["--password", String("text")]), |
paulb@46 | 397 | "zenity" : (_readfrom, ["--password", StringKeyword("--text", "text"), "--hide-text"]), |
paulb@46 | 398 | "Xdialog" : (_readfrom, ["--stdout", "--password", "--inputbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 399 | } |
paulb@46 | 400 | |
paulb@46 | 401 | class TextFile(Simple): |
paulb@46 | 402 | |
paulb@46 | 403 | """ |
paulb@46 | 404 | A text file input box. |
paulb@46 | 405 | Options: text, width (in characters), height (in characters), |
paulb@46 | 406 | filename |
paulb@46 | 407 | """ |
paulb@46 | 408 | |
paulb@46 | 409 | name = "textfile" |
paulb@46 | 410 | info = { |
paulb@46 | 411 | "kdialog" : (_readfrom, ["--textbox", String("filename"), String("width"), String("height")]), |
paulb@46 | 412 | "zenity" : (_readfrom, ["--textbox", StringKeyword("--filename", "filename"), IntegerKeyword("--width", "width"), |
paulb@46 | 413 | IntegerKeyword("--height", "height")] |
paulb@46 | 414 | ), |
paulb@46 | 415 | "Xdialog" : (_readfrom, ["--stdout", "--textbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 416 | } |
paulb@46 | 417 | |
paulb@46 | 418 | def __init__(self, text, filename, width=None, height=None): |
paulb@46 | 419 | Simple.__init__(self, text, width, height) |
paulb@46 | 420 | self.filename = filename |
paulb@46 | 421 | |
paulb@46 | 422 | # vim: tabstop=4 expandtab shiftwidth=4 |