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@47 | 147 | "A radiolist/checklist item list parameter." |
paulb@47 | 148 | |
paulb@47 | 149 | def __init__(self, name, status_first=0): |
paulb@47 | 150 | String.__init__(self, name) |
paulb@47 | 151 | self.status_first = status_first |
paulb@46 | 152 | |
paulb@46 | 153 | def convert(self, value, program): |
paulb@46 | 154 | l = [] |
paulb@46 | 155 | for v in value: |
paulb@47 | 156 | boolean = Boolean(None) |
paulb@47 | 157 | status = boolean.convert(v.status, program) |
paulb@47 | 158 | if self.status_first: |
paulb@47 | 159 | l += status |
paulb@46 | 160 | l.append(v.value) |
paulb@46 | 161 | l.append(v.text) |
paulb@47 | 162 | if not self.status_first: |
paulb@47 | 163 | l += status |
paulb@46 | 164 | return l |
paulb@46 | 165 | |
paulb@46 | 166 | # Dialogue argument values. |
paulb@46 | 167 | |
paulb@46 | 168 | class MenuItem: |
paulb@47 | 169 | |
paulb@47 | 170 | "A menu item which can also be used with radiolists and checklists." |
paulb@47 | 171 | |
paulb@47 | 172 | def __init__(self, value, text, status=0): |
paulb@46 | 173 | self.value = value |
paulb@46 | 174 | self.text = text |
paulb@46 | 175 | self.status = status |
paulb@46 | 176 | |
paulb@46 | 177 | # Dialogue classes. |
paulb@46 | 178 | |
paulb@46 | 179 | class Dialogue: |
paulb@46 | 180 | |
paulb@46 | 181 | commands = { |
paulb@46 | 182 | "KDE" : "kdialog", |
paulb@46 | 183 | "GNOME" : "zenity", |
paulb@46 | 184 | "X11" : "Xdialog" |
paulb@46 | 185 | } |
paulb@46 | 186 | |
paulb@46 | 187 | def open(self, desktop=None): |
paulb@46 | 188 | |
paulb@46 | 189 | """ |
paulb@46 | 190 | Open a dialogue box (dialog) using a program appropriate to the desktop |
paulb@46 | 191 | environment in use. |
paulb@46 | 192 | |
paulb@46 | 193 | If the optional 'desktop' parameter is specified then attempt to use that |
paulb@46 | 194 | particular desktop environment's mechanisms to open the dialog instead of |
paulb@46 | 195 | guessing or detecting which environment is being used. |
paulb@46 | 196 | |
paulb@46 | 197 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X", |
paulb@46 | 198 | "Windows". |
paulb@46 | 199 | |
paulb@46 | 200 | The result of the dialogue interaction may be a string indicating user |
paulb@46 | 201 | input (for input, password, menu, radiolist, pulldown), a list of strings |
paulb@46 | 202 | indicating selections of one or more items (for checklist), or a value |
paulb@46 | 203 | indicating true or false (for question). |
paulb@46 | 204 | """ |
paulb@46 | 205 | |
paulb@46 | 206 | # Decide on the desktop environment in use. |
paulb@46 | 207 | |
paulb@46 | 208 | desktop_in_use = use_desktop(desktop) |
paulb@46 | 209 | |
paulb@46 | 210 | # Get the program. |
paulb@46 | 211 | |
paulb@46 | 212 | try: |
paulb@46 | 213 | program = self.commands[desktop_in_use] |
paulb@46 | 214 | except KeyError: |
paulb@46 | 215 | raise OSError, "Desktop '%s' not supported (no known dialogue box command could be suggested)" % desktop_in_use |
paulb@46 | 216 | |
paulb@46 | 217 | handler, options = self.info[program] |
paulb@46 | 218 | |
paulb@46 | 219 | cmd = [program] |
paulb@46 | 220 | for option in options: |
paulb@46 | 221 | if isinstance(option, str): |
paulb@46 | 222 | cmd.append(option) |
paulb@46 | 223 | else: |
paulb@46 | 224 | value = getattr(self, option.name, None) |
paulb@46 | 225 | cmd += option.convert(value, program) |
paulb@46 | 226 | |
paulb@46 | 227 | print cmd |
paulb@46 | 228 | return handler(cmd, 0) |
paulb@46 | 229 | |
paulb@46 | 230 | class Simple(Dialogue): |
paulb@46 | 231 | def __init__(self, text, width=None, height=None): |
paulb@46 | 232 | self.text = text |
paulb@46 | 233 | self.width = width |
paulb@46 | 234 | self.height = height |
paulb@46 | 235 | |
paulb@46 | 236 | class Question(Simple): |
paulb@46 | 237 | |
paulb@46 | 238 | """ |
paulb@46 | 239 | A dialogue asking a question and showing response buttons. |
paulb@46 | 240 | Options: text, width (in characters), height (in characters) |
paulb@46 | 241 | """ |
paulb@46 | 242 | |
paulb@46 | 243 | name = "question" |
paulb@46 | 244 | info = { |
paulb@46 | 245 | "kdialog" : (_status, ["--yesno", String("text")]), |
paulb@46 | 246 | "zenity" : (_status, ["--question", StringKeyword("--text", "text")]), |
paulb@46 | 247 | "Xdialog" : (_status, ["--stdout", "--yesno", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 248 | } |
paulb@46 | 249 | |
paulb@46 | 250 | class Warning(Simple): |
paulb@46 | 251 | |
paulb@46 | 252 | """ |
paulb@46 | 253 | A dialogue asking a question and showing response buttons. |
paulb@46 | 254 | Options: text, width (in characters), height (in characters) |
paulb@46 | 255 | """ |
paulb@46 | 256 | |
paulb@46 | 257 | name = "warning" |
paulb@46 | 258 | info = { |
paulb@46 | 259 | "kdialog" : (_status, ["--warningyesno", String("text")]), |
paulb@46 | 260 | "zenity" : (_status, ["--warning", StringKeyword("--text", "text")]), |
paulb@46 | 261 | "Xdialog" : (_status, ["--stdout", "--yesno", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 262 | } |
paulb@46 | 263 | |
paulb@46 | 264 | class Message(Simple): |
paulb@46 | 265 | |
paulb@46 | 266 | """ |
paulb@46 | 267 | A message dialogue. |
paulb@46 | 268 | Options: text, width (in characters), height (in characters) |
paulb@46 | 269 | """ |
paulb@46 | 270 | |
paulb@46 | 271 | name = "message" |
paulb@46 | 272 | info = { |
paulb@46 | 273 | "kdialog" : (_status, ["--msgbox", String("text")]), |
paulb@46 | 274 | "zenity" : (_status, ["--info", StringKeyword("--text", "text")]), |
paulb@46 | 275 | "Xdialog" : (_status, ["--stdout", "--msgbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 276 | } |
paulb@46 | 277 | |
paulb@46 | 278 | class Error(Simple): |
paulb@46 | 279 | |
paulb@46 | 280 | """ |
paulb@46 | 281 | An error dialogue. |
paulb@46 | 282 | Options: text, width (in characters), height (in characters) |
paulb@46 | 283 | """ |
paulb@46 | 284 | |
paulb@46 | 285 | name = "error" |
paulb@46 | 286 | info = { |
paulb@46 | 287 | "kdialog" : (_status, ["--error", String("text")]), |
paulb@46 | 288 | "zenity" : (_status, ["--error", StringKeyword("--text", "text")]), |
paulb@46 | 289 | "Xdialog" : (_status, ["--stdout", "--msgbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 290 | } |
paulb@46 | 291 | |
paulb@46 | 292 | class Menu(Simple): |
paulb@46 | 293 | |
paulb@46 | 294 | """ |
paulb@46 | 295 | A menu of options, one of which being selectable. |
paulb@46 | 296 | Options: text, width (in characters), height (in characters), |
paulb@46 | 297 | list_height (in items), items (MenuItem objects) |
paulb@46 | 298 | """ |
paulb@46 | 299 | |
paulb@46 | 300 | name = "menu" |
paulb@46 | 301 | info = { |
paulb@46 | 302 | "kdialog" : (_readfrom, ["--menu", String("text"), MenuItemList("items")]), |
paulb@46 | 303 | "zenity" : (_readfrom, ["--list", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@46 | 304 | MenuItemList("items")] |
paulb@46 | 305 | ), |
paulb@46 | 306 | "Xdialog" : (_readfrom, ["--stdout", "--menubox", |
paulb@46 | 307 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), MenuItemList("items")] |
paulb@46 | 308 | ), |
paulb@46 | 309 | } |
paulb@47 | 310 | item = MenuItem |
paulb@46 | 311 | |
paulb@47 | 312 | def __init__(self, text, titles, items=None, width=None, height=None, list_height=None): |
paulb@47 | 313 | |
paulb@47 | 314 | """ |
paulb@47 | 315 | Initialise a menu with the given heading 'text', column 'titles', and |
paulb@47 | 316 | optional 'items' (which may be added later), 'width' (in characters), |
paulb@47 | 317 | 'height' (in characters) and 'list_height' (in items). |
paulb@47 | 318 | """ |
paulb@47 | 319 | |
paulb@46 | 320 | Simple.__init__(self, text, width, height) |
paulb@46 | 321 | self.titles = titles |
paulb@47 | 322 | self.items = items or [] |
paulb@46 | 323 | self.list_height = list_height |
paulb@46 | 324 | |
paulb@47 | 325 | def add(self, *args, **kw): |
paulb@47 | 326 | |
paulb@47 | 327 | """ |
paulb@47 | 328 | Add an item, passing the given arguments to the appropriate item class. |
paulb@47 | 329 | """ |
paulb@47 | 330 | |
paulb@47 | 331 | self.items.append(self.item(*args, **kw)) |
paulb@47 | 332 | |
paulb@46 | 333 | class RadioList(Menu): |
paulb@46 | 334 | |
paulb@46 | 335 | """ |
paulb@46 | 336 | A list of radio buttons, one of which being selectable. |
paulb@46 | 337 | Options: text, width (in characters), height (in characters), |
paulb@47 | 338 | list_height (in items), items (MenuItem objects), titles |
paulb@46 | 339 | """ |
paulb@46 | 340 | |
paulb@46 | 341 | name = "radiolist" |
paulb@46 | 342 | info = { |
paulb@46 | 343 | "kdialog" : (_readfrom, ["--radiolist", String("text"), ListItemList("items")]), |
paulb@46 | 344 | "zenity" : (_readfrom, |
paulb@46 | 345 | ["--list", "--radiolist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@47 | 346 | ListItemList("items", 1)] |
paulb@46 | 347 | ), |
paulb@46 | 348 | "Xdialog" : (_readfrom, ["--stdout", "--radiolist", |
paulb@46 | 349 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), ListItemList("items")] |
paulb@46 | 350 | ), |
paulb@46 | 351 | } |
paulb@46 | 352 | |
paulb@46 | 353 | class CheckList(Menu): |
paulb@46 | 354 | |
paulb@46 | 355 | """ |
paulb@46 | 356 | A list of checkboxes, many being selectable. |
paulb@46 | 357 | Options: text, width (in characters), height (in characters), |
paulb@47 | 358 | list_height (in items), items (MenuItem objects), titles |
paulb@46 | 359 | """ |
paulb@46 | 360 | |
paulb@46 | 361 | name = "checklist" |
paulb@46 | 362 | info = { |
paulb@46 | 363 | "kdialog" : (_readfrom, ["--checklist", String("text"), ListItemList("items")]), |
paulb@46 | 364 | "zenity" : (_readfrom, |
paulb@46 | 365 | ["--list", "--checklist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@47 | 366 | ListItemList("items", 1)] |
paulb@46 | 367 | ), |
paulb@46 | 368 | "Xdialog" : (_readfrom, ["--stdout", "--checklist", |
paulb@46 | 369 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), ListItemList("items")] |
paulb@46 | 370 | ), |
paulb@46 | 371 | } |
paulb@46 | 372 | |
paulb@46 | 373 | class Pulldown(Menu): |
paulb@46 | 374 | |
paulb@46 | 375 | """ |
paulb@46 | 376 | A pull-down menu of options, one of which being selectable. |
paulb@46 | 377 | Options: text, width (in characters), height (in characters), |
paulb@46 | 378 | entries (list of values) |
paulb@46 | 379 | """ |
paulb@46 | 380 | |
paulb@46 | 381 | name = "pulldown" |
paulb@46 | 382 | info = { |
paulb@46 | 383 | "kdialog" : (_readfrom, ["--combobox", String("text"), Strings("items")]), |
paulb@46 | 384 | "zenity" : (_readfrom, ["--list", "--radiolist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@46 | 385 | Strings("items")] |
paulb@46 | 386 | ), |
paulb@46 | 387 | "Xdialog" : (_readfrom, ["--stdout", "--combobox", String("text"), Integer("height"), Integer("width"), Strings("items")]), |
paulb@46 | 388 | } |
paulb@46 | 389 | |
paulb@46 | 390 | class Input(Simple): |
paulb@46 | 391 | |
paulb@46 | 392 | """ |
paulb@46 | 393 | An input dialogue, consisting of an input field. |
paulb@46 | 394 | Options: text, width (in characters), height (in characters), |
paulb@46 | 395 | input |
paulb@46 | 396 | """ |
paulb@46 | 397 | |
paulb@46 | 398 | name = "input" |
paulb@46 | 399 | info = { |
paulb@46 | 400 | "kdialog" : (_readfrom, ["--inputbox", String("text"), String("data")]), |
paulb@46 | 401 | "zenity" : (_readfrom, ["--entry", StringKeyword("--text", "text"), StringKeyword("--entry-text", "data")]), |
paulb@46 | 402 | "Xdialog" : (_readfrom, ["--stdout", "--inputbox", String("text"), Integer("height"), Integer("width"), String("data")]), |
paulb@46 | 403 | } |
paulb@46 | 404 | |
paulb@46 | 405 | def __init__(self, text, data, width=None, height=None): |
paulb@46 | 406 | Simple.__init__(self, text, width, height) |
paulb@46 | 407 | self.data = data |
paulb@46 | 408 | |
paulb@46 | 409 | class Password(Input): |
paulb@46 | 410 | |
paulb@46 | 411 | """ |
paulb@46 | 412 | A password dialogue, consisting of a password entry field. |
paulb@46 | 413 | Options: text, width (in characters), height (in characters), |
paulb@46 | 414 | input |
paulb@46 | 415 | """ |
paulb@46 | 416 | |
paulb@46 | 417 | name = "password" |
paulb@46 | 418 | info = { |
paulb@46 | 419 | "kdialog" : (_readfrom, ["--password", String("text")]), |
paulb@46 | 420 | "zenity" : (_readfrom, ["--password", StringKeyword("--text", "text"), "--hide-text"]), |
paulb@46 | 421 | "Xdialog" : (_readfrom, ["--stdout", "--password", "--inputbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 422 | } |
paulb@46 | 423 | |
paulb@46 | 424 | class TextFile(Simple): |
paulb@46 | 425 | |
paulb@46 | 426 | """ |
paulb@46 | 427 | A text file input box. |
paulb@46 | 428 | Options: text, width (in characters), height (in characters), |
paulb@46 | 429 | filename |
paulb@46 | 430 | """ |
paulb@46 | 431 | |
paulb@46 | 432 | name = "textfile" |
paulb@46 | 433 | info = { |
paulb@46 | 434 | "kdialog" : (_readfrom, ["--textbox", String("filename"), String("width"), String("height")]), |
paulb@46 | 435 | "zenity" : (_readfrom, ["--textbox", StringKeyword("--filename", "filename"), IntegerKeyword("--width", "width"), |
paulb@46 | 436 | IntegerKeyword("--height", "height")] |
paulb@46 | 437 | ), |
paulb@46 | 438 | "Xdialog" : (_readfrom, ["--stdout", "--textbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 439 | } |
paulb@46 | 440 | |
paulb@46 | 441 | def __init__(self, text, filename, width=None, height=None): |
paulb@46 | 442 | Simple.__init__(self, text, width, height) |
paulb@46 | 443 | self.filename = filename |
paulb@46 | 444 | |
paulb@47 | 445 | # Available dialogues. |
paulb@47 | 446 | |
paulb@47 | 447 | available = [Question, Warning, Message, Error, Menu, CheckList, RadioList, Input, Password, Pulldown, TextFile] |
paulb@47 | 448 | |
paulb@46 | 449 | # vim: tabstop=4 expandtab shiftwidth=4 |