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 | |
paul@84 | 6 | Copyright (C) 2007, 2009, 2014 Paul Boddie <paul@boddie.org.uk> |
paulb@46 | 7 | |
paul@68 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@68 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paul@68 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paul@68 | 11 | later version. |
paulb@46 | 12 | |
paul@68 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@68 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@68 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paul@68 | 16 | details. |
paulb@46 | 17 | |
paul@68 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paul@68 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@46 | 20 | |
paulb@46 | 21 | -------- |
paulb@46 | 22 | |
paulb@46 | 23 | Opening Dialogue Boxes (Dialogs) |
paulb@46 | 24 | -------------------------------- |
paulb@46 | 25 | |
paulb@46 | 26 | To open a dialogue box (dialog) in the current desktop environment, relying on |
paulb@46 | 27 | the automatic detection of that environment, use the appropriate dialogue box |
paulb@46 | 28 | class: |
paulb@46 | 29 | |
paulb@46 | 30 | question = desktop.dialog.Question("Are you sure?") |
paulb@57 | 31 | result = question.open() |
paulb@46 | 32 | |
paulb@46 | 33 | To override the detected desktop, specify the desktop parameter to the open |
paulb@46 | 34 | function as follows: |
paulb@46 | 35 | |
paulb@46 | 36 | question.open("KDE") # Insists on KDE |
paulb@46 | 37 | question.open("GNOME") # Insists on GNOME |
paulb@46 | 38 | |
paulb@46 | 39 | The dialogue box options are documented in each class's docstring. |
paulb@51 | 40 | |
paulb@51 | 41 | Available dialogue box classes are listed in the desktop.dialog.available |
paulb@51 | 42 | attribute. |
paulb@57 | 43 | |
paulb@57 | 44 | Supported desktop environments are listed in the desktop.dialog.supported |
paulb@57 | 45 | attribute. |
paulb@46 | 46 | """ |
paulb@46 | 47 | |
paulb@46 | 48 | from desktop import use_desktop, _run, _readfrom, _status |
paul@89 | 49 | from time import strptime |
paulb@46 | 50 | |
paulb@49 | 51 | class _wrapper: |
paulb@49 | 52 | def __init__(self, handler): |
paulb@49 | 53 | self.handler = handler |
paulb@49 | 54 | |
paulb@49 | 55 | class _readvalue(_wrapper): |
paulb@49 | 56 | def __call__(self, cmd, shell): |
paulb@49 | 57 | return self.handler(cmd, shell).strip() |
paulb@49 | 58 | |
paulb@49 | 59 | class _readinput(_wrapper): |
paulb@49 | 60 | def __call__(self, cmd, shell): |
paulb@49 | 61 | return self.handler(cmd, shell)[:-1] |
paulb@49 | 62 | |
paulb@49 | 63 | class _readvalues_kdialog(_wrapper): |
paulb@49 | 64 | def __call__(self, cmd, shell): |
paulb@49 | 65 | result = self.handler(cmd, shell).strip().strip('"') |
paulb@49 | 66 | if result: |
paulb@49 | 67 | return result.split('" "') |
paulb@49 | 68 | else: |
paulb@49 | 69 | return [] |
paulb@49 | 70 | |
paulb@49 | 71 | class _readvalues_zenity(_wrapper): |
paulb@49 | 72 | def __call__(self, cmd, shell): |
paulb@49 | 73 | result = self.handler(cmd, shell).strip() |
paulb@49 | 74 | if result: |
paulb@49 | 75 | return result.split("|") |
paulb@49 | 76 | else: |
paulb@49 | 77 | return [] |
paulb@49 | 78 | |
paulb@49 | 79 | class _readvalues_Xdialog(_wrapper): |
paulb@49 | 80 | def __call__(self, cmd, shell): |
paulb@49 | 81 | result = self.handler(cmd, shell).strip() |
paulb@49 | 82 | if result: |
paulb@49 | 83 | return result.split("/") |
paulb@49 | 84 | else: |
paulb@49 | 85 | return [] |
paulb@49 | 86 | |
paul@89 | 87 | class _readdate_kdialog(_wrapper): |
paul@89 | 88 | def __call__(self, cmd, shell): |
paul@89 | 89 | result = self.handler(cmd, shell).strip() |
paul@89 | 90 | if result: |
paul@89 | 91 | return strptime(result, "%a %b %d %Y") |
paul@89 | 92 | else: |
paul@89 | 93 | return None |
paul@89 | 94 | |
paul@89 | 95 | class _readdate_zenity(_wrapper): |
paul@89 | 96 | def __call__(self, cmd, shell): |
paul@89 | 97 | result = self.handler(cmd, shell).strip() |
paul@89 | 98 | if result: |
paul@89 | 99 | return strptime(result, "%Y %m %d") |
paul@89 | 100 | else: |
paul@89 | 101 | return None |
paul@89 | 102 | |
paulb@46 | 103 | # Dialogue parameter classes. |
paulb@46 | 104 | |
paulb@46 | 105 | class String: |
paulb@46 | 106 | |
paulb@46 | 107 | "A generic parameter." |
paulb@46 | 108 | |
paulb@46 | 109 | def __init__(self, name): |
paulb@46 | 110 | self.name = name |
paulb@46 | 111 | |
paulb@46 | 112 | def convert(self, value, program): |
paulb@46 | 113 | return [value or ""] |
paulb@46 | 114 | |
paulb@46 | 115 | class Strings(String): |
paulb@46 | 116 | |
paulb@46 | 117 | "Multiple string parameters." |
paulb@46 | 118 | |
paulb@46 | 119 | def convert(self, value, program): |
paulb@46 | 120 | return value or [] |
paulb@46 | 121 | |
paulb@49 | 122 | class StringPairs(String): |
paulb@49 | 123 | |
paulb@49 | 124 | "Multiple string parameters duplicated to make identifiers." |
paulb@49 | 125 | |
paulb@49 | 126 | def convert(self, value, program): |
paulb@49 | 127 | l = [] |
paulb@49 | 128 | for v in value: |
paulb@49 | 129 | l.append(v) |
paulb@49 | 130 | l.append(v) |
paulb@49 | 131 | return l |
paulb@49 | 132 | |
paulb@46 | 133 | class StringKeyword: |
paulb@46 | 134 | |
paulb@46 | 135 | "A keyword parameter." |
paulb@46 | 136 | |
paulb@46 | 137 | def __init__(self, keyword, name): |
paulb@46 | 138 | self.keyword = keyword |
paulb@46 | 139 | self.name = name |
paulb@46 | 140 | |
paulb@46 | 141 | def convert(self, value, program): |
paulb@46 | 142 | return [self.keyword + "=" + (value or "")] |
paulb@46 | 143 | |
paulb@46 | 144 | class StringKeywords: |
paulb@46 | 145 | |
paulb@46 | 146 | "Multiple keyword parameters." |
paulb@46 | 147 | |
paulb@46 | 148 | def __init__(self, keyword, name): |
paulb@46 | 149 | self.keyword = keyword |
paulb@46 | 150 | self.name = name |
paulb@46 | 151 | |
paulb@46 | 152 | def convert(self, value, program): |
paulb@46 | 153 | l = [] |
paulb@46 | 154 | for v in value or []: |
paulb@46 | 155 | l.append(self.keyword + "=" + v) |
paulb@46 | 156 | return l |
paulb@46 | 157 | |
paulb@46 | 158 | class Integer(String): |
paulb@46 | 159 | |
paulb@46 | 160 | "An integer parameter." |
paulb@46 | 161 | |
paulb@46 | 162 | defaults = { |
paulb@46 | 163 | "width" : 40, |
paulb@46 | 164 | "height" : 15, |
paulb@46 | 165 | "list_height" : 10 |
paulb@46 | 166 | } |
paulb@49 | 167 | scale = 8 |
paulb@49 | 168 | |
paulb@49 | 169 | def __init__(self, name, pixels=0): |
paulb@49 | 170 | String.__init__(self, name) |
paulb@49 | 171 | if pixels: |
paulb@49 | 172 | self.factor = self.scale |
paulb@49 | 173 | else: |
paulb@49 | 174 | self.factor = 1 |
paulb@46 | 175 | |
paulb@46 | 176 | def convert(self, value, program): |
paulb@46 | 177 | if value is None: |
paulb@46 | 178 | value = self.defaults[self.name] |
paulb@49 | 179 | return [str(int(value) * self.factor)] |
paulb@46 | 180 | |
paulb@46 | 181 | class IntegerKeyword(Integer): |
paulb@46 | 182 | |
paulb@46 | 183 | "An integer keyword parameter." |
paulb@46 | 184 | |
paulb@49 | 185 | def __init__(self, keyword, name, pixels=0): |
paulb@49 | 186 | Integer.__init__(self, name, pixels) |
paulb@46 | 187 | self.keyword = keyword |
paulb@46 | 188 | |
paulb@46 | 189 | def convert(self, value, program): |
paulb@46 | 190 | if value is None: |
paulb@46 | 191 | value = self.defaults[self.name] |
paulb@49 | 192 | return [self.keyword + "=" + str(int(value) * self.factor)] |
paulb@46 | 193 | |
paulb@46 | 194 | class Boolean(String): |
paulb@46 | 195 | |
paulb@46 | 196 | "A boolean parameter." |
paulb@46 | 197 | |
paulb@46 | 198 | values = { |
paulb@46 | 199 | "kdialog" : ["off", "on"], |
paulb@46 | 200 | "zenity" : ["FALSE", "TRUE"], |
paulb@46 | 201 | "Xdialog" : ["off", "on"] |
paulb@46 | 202 | } |
paulb@46 | 203 | |
paulb@46 | 204 | def convert(self, value, program): |
paulb@46 | 205 | values = self.values[program] |
paulb@46 | 206 | if value: |
paulb@46 | 207 | return [values[1]] |
paulb@46 | 208 | else: |
paulb@46 | 209 | return [values[0]] |
paulb@46 | 210 | |
paulb@46 | 211 | class MenuItemList(String): |
paulb@46 | 212 | |
paulb@46 | 213 | "A menu item list parameter." |
paulb@46 | 214 | |
paulb@46 | 215 | def convert(self, value, program): |
paulb@46 | 216 | l = [] |
paulb@46 | 217 | for v in value: |
paulb@46 | 218 | l.append(v.value) |
paulb@46 | 219 | l.append(v.text) |
paulb@46 | 220 | return l |
paulb@46 | 221 | |
paulb@46 | 222 | class ListItemList(String): |
paulb@46 | 223 | |
paulb@47 | 224 | "A radiolist/checklist item list parameter." |
paulb@47 | 225 | |
paulb@47 | 226 | def __init__(self, name, status_first=0): |
paulb@47 | 227 | String.__init__(self, name) |
paulb@47 | 228 | self.status_first = status_first |
paulb@46 | 229 | |
paulb@46 | 230 | def convert(self, value, program): |
paulb@46 | 231 | l = [] |
paulb@46 | 232 | for v in value: |
paulb@47 | 233 | boolean = Boolean(None) |
paulb@47 | 234 | status = boolean.convert(v.status, program) |
paulb@47 | 235 | if self.status_first: |
paulb@47 | 236 | l += status |
paulb@46 | 237 | l.append(v.value) |
paulb@46 | 238 | l.append(v.text) |
paulb@47 | 239 | if not self.status_first: |
paulb@47 | 240 | l += status |
paulb@46 | 241 | return l |
paulb@46 | 242 | |
paulb@46 | 243 | # Dialogue argument values. |
paulb@46 | 244 | |
paulb@46 | 245 | class MenuItem: |
paulb@47 | 246 | |
paulb@47 | 247 | "A menu item which can also be used with radiolists and checklists." |
paulb@47 | 248 | |
paulb@47 | 249 | def __init__(self, value, text, status=0): |
paulb@46 | 250 | self.value = value |
paulb@46 | 251 | self.text = text |
paulb@46 | 252 | self.status = status |
paulb@46 | 253 | |
paulb@46 | 254 | # Dialogue classes. |
paulb@46 | 255 | |
paulb@46 | 256 | class Dialogue: |
paulb@46 | 257 | |
paulb@46 | 258 | commands = { |
paulb@46 | 259 | "KDE" : "kdialog", |
paul@84 | 260 | "KDE4" : "kdialog", |
paulb@46 | 261 | "GNOME" : "zenity", |
paulb@57 | 262 | "XFCE" : "zenity", # NOTE: Based on observations with Xubuntu. |
paulb@46 | 263 | "X11" : "Xdialog" |
paulb@46 | 264 | } |
paulb@46 | 265 | |
paulb@46 | 266 | def open(self, desktop=None): |
paulb@46 | 267 | |
paulb@46 | 268 | """ |
paulb@46 | 269 | Open a dialogue box (dialog) using a program appropriate to the desktop |
paulb@46 | 270 | environment in use. |
paulb@46 | 271 | |
paulb@57 | 272 | If the optional 'desktop' parameter is specified then attempt to use |
paulb@57 | 273 | that particular desktop environment's mechanisms to open the dialog |
paulb@57 | 274 | instead of guessing or detecting which environment is being used. |
paulb@46 | 275 | |
paul@84 | 276 | Suggested values for 'desktop' are "standard", "KDE", "KDE4", "GNOME", |
paulb@57 | 277 | "Mac OS X", "Windows". |
paulb@46 | 278 | |
paulb@46 | 279 | The result of the dialogue interaction may be a string indicating user |
paulb@57 | 280 | input (for Input, Password, Menu, Pulldown), a list of strings |
paulb@57 | 281 | indicating selections of one or more items (for RadioList, CheckList), |
paulb@57 | 282 | or a value indicating true or false (for Question, Warning, Message, |
paulb@57 | 283 | Error). |
paulb@58 | 284 | |
paulb@58 | 285 | Where a string value may be expected but no choice is made, an empty |
paulb@58 | 286 | string may be returned. Similarly, where a list of values is expected |
paulb@58 | 287 | but no choice is made, an empty list may be returned. |
paulb@46 | 288 | """ |
paulb@46 | 289 | |
paulb@46 | 290 | # Decide on the desktop environment in use. |
paulb@46 | 291 | |
paulb@46 | 292 | desktop_in_use = use_desktop(desktop) |
paulb@46 | 293 | |
paulb@46 | 294 | # Get the program. |
paulb@46 | 295 | |
paulb@46 | 296 | try: |
paulb@46 | 297 | program = self.commands[desktop_in_use] |
paulb@46 | 298 | except KeyError: |
paulb@46 | 299 | raise OSError, "Desktop '%s' not supported (no known dialogue box command could be suggested)" % desktop_in_use |
paulb@46 | 300 | |
paulb@57 | 301 | # The handler is one of the functions communicating with the subprocess. |
paulb@57 | 302 | # Some handlers return boolean values, others strings. |
paulb@57 | 303 | |
paulb@46 | 304 | handler, options = self.info[program] |
paulb@46 | 305 | |
paulb@46 | 306 | cmd = [program] |
paulb@46 | 307 | for option in options: |
paulb@46 | 308 | if isinstance(option, str): |
paulb@46 | 309 | cmd.append(option) |
paulb@46 | 310 | else: |
paulb@46 | 311 | value = getattr(self, option.name, None) |
paulb@46 | 312 | cmd += option.convert(value, program) |
paulb@46 | 313 | |
paulb@46 | 314 | return handler(cmd, 0) |
paulb@46 | 315 | |
paulb@46 | 316 | class Simple(Dialogue): |
paulb@46 | 317 | def __init__(self, text, width=None, height=None): |
paulb@46 | 318 | self.text = text |
paulb@46 | 319 | self.width = width |
paulb@46 | 320 | self.height = height |
paulb@46 | 321 | |
paulb@46 | 322 | class Question(Simple): |
paulb@46 | 323 | |
paulb@46 | 324 | """ |
paulb@46 | 325 | A dialogue asking a question and showing response buttons. |
paulb@46 | 326 | Options: text, width (in characters), height (in characters) |
paulb@57 | 327 | Response: a boolean value indicating an affirmative response (true) or a |
paulb@57 | 328 | negative response |
paulb@46 | 329 | """ |
paulb@46 | 330 | |
paulb@46 | 331 | name = "question" |
paulb@46 | 332 | info = { |
paulb@46 | 333 | "kdialog" : (_status, ["--yesno", String("text")]), |
paulb@46 | 334 | "zenity" : (_status, ["--question", StringKeyword("--text", "text")]), |
paulb@46 | 335 | "Xdialog" : (_status, ["--stdout", "--yesno", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 336 | } |
paulb@46 | 337 | |
paulb@46 | 338 | class Warning(Simple): |
paulb@46 | 339 | |
paulb@46 | 340 | """ |
paulb@46 | 341 | A dialogue asking a question and showing response buttons. |
paulb@46 | 342 | Options: text, width (in characters), height (in characters) |
paulb@57 | 343 | Response: a boolean value indicating an affirmative response (true) or a |
paulb@57 | 344 | negative response |
paulb@46 | 345 | """ |
paulb@46 | 346 | |
paulb@46 | 347 | name = "warning" |
paulb@46 | 348 | info = { |
paulb@46 | 349 | "kdialog" : (_status, ["--warningyesno", String("text")]), |
paulb@46 | 350 | "zenity" : (_status, ["--warning", StringKeyword("--text", "text")]), |
paulb@46 | 351 | "Xdialog" : (_status, ["--stdout", "--yesno", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 352 | } |
paulb@46 | 353 | |
paulb@46 | 354 | class Message(Simple): |
paulb@46 | 355 | |
paulb@46 | 356 | """ |
paulb@46 | 357 | A message dialogue. |
paulb@46 | 358 | Options: text, width (in characters), height (in characters) |
paulb@57 | 359 | Response: a boolean value indicating an affirmative response (true) or a |
paulb@57 | 360 | negative response |
paulb@46 | 361 | """ |
paulb@46 | 362 | |
paulb@46 | 363 | name = "message" |
paulb@46 | 364 | info = { |
paulb@46 | 365 | "kdialog" : (_status, ["--msgbox", String("text")]), |
paulb@46 | 366 | "zenity" : (_status, ["--info", StringKeyword("--text", "text")]), |
paulb@46 | 367 | "Xdialog" : (_status, ["--stdout", "--msgbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 368 | } |
paulb@46 | 369 | |
paulb@46 | 370 | class Error(Simple): |
paulb@46 | 371 | |
paulb@46 | 372 | """ |
paulb@46 | 373 | An error dialogue. |
paulb@46 | 374 | Options: text, width (in characters), height (in characters) |
paulb@57 | 375 | Response: a boolean value indicating an affirmative response (true) or a |
paulb@57 | 376 | negative response |
paulb@46 | 377 | """ |
paulb@46 | 378 | |
paulb@46 | 379 | name = "error" |
paulb@46 | 380 | info = { |
paulb@46 | 381 | "kdialog" : (_status, ["--error", String("text")]), |
paulb@46 | 382 | "zenity" : (_status, ["--error", StringKeyword("--text", "text")]), |
paulb@46 | 383 | "Xdialog" : (_status, ["--stdout", "--msgbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 384 | } |
paulb@46 | 385 | |
paulb@46 | 386 | class Menu(Simple): |
paulb@46 | 387 | |
paulb@46 | 388 | """ |
paulb@46 | 389 | A menu of options, one of which being selectable. |
paulb@46 | 390 | Options: text, width (in characters), height (in characters), |
paulb@57 | 391 | list_height (in items), items (MenuItem objects) |
paulb@57 | 392 | Response: a value corresponding to the chosen item |
paulb@46 | 393 | """ |
paulb@46 | 394 | |
paulb@46 | 395 | name = "menu" |
paulb@46 | 396 | info = { |
paulb@49 | 397 | "kdialog" : (_readvalue(_readfrom), ["--menu", String("text"), MenuItemList("items")]), |
paulb@49 | 398 | "zenity" : (_readvalue(_readfrom), ["--list", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@46 | 399 | MenuItemList("items")] |
paulb@46 | 400 | ), |
paulb@49 | 401 | "Xdialog" : (_readvalue(_readfrom), ["--stdout", "--menubox", |
paulb@46 | 402 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), MenuItemList("items")] |
paulb@46 | 403 | ), |
paulb@46 | 404 | } |
paulb@47 | 405 | item = MenuItem |
paulb@49 | 406 | number_of_titles = 2 |
paulb@46 | 407 | |
paulb@47 | 408 | def __init__(self, text, titles, items=None, width=None, height=None, list_height=None): |
paulb@47 | 409 | |
paulb@47 | 410 | """ |
paulb@47 | 411 | Initialise a menu with the given heading 'text', column 'titles', and |
paulb@47 | 412 | optional 'items' (which may be added later), 'width' (in characters), |
paulb@47 | 413 | 'height' (in characters) and 'list_height' (in items). |
paulb@47 | 414 | """ |
paulb@47 | 415 | |
paulb@46 | 416 | Simple.__init__(self, text, width, height) |
paulb@49 | 417 | self.titles = ([""] * self.number_of_titles + titles)[-self.number_of_titles:] |
paulb@47 | 418 | self.items = items or [] |
paulb@46 | 419 | self.list_height = list_height |
paulb@46 | 420 | |
paulb@47 | 421 | def add(self, *args, **kw): |
paulb@47 | 422 | |
paulb@47 | 423 | """ |
paulb@47 | 424 | Add an item, passing the given arguments to the appropriate item class. |
paulb@47 | 425 | """ |
paulb@47 | 426 | |
paulb@47 | 427 | self.items.append(self.item(*args, **kw)) |
paulb@47 | 428 | |
paulb@46 | 429 | class RadioList(Menu): |
paulb@46 | 430 | |
paulb@46 | 431 | """ |
paulb@46 | 432 | A list of radio buttons, one of which being selectable. |
paulb@46 | 433 | Options: text, width (in characters), height (in characters), |
paulb@57 | 434 | list_height (in items), items (MenuItem objects), titles |
paulb@57 | 435 | Response: a list of values corresponding to chosen items (since some |
paulb@57 | 436 | programs, eg. zenity, appear to support multiple default |
paulb@57 | 437 | selections) |
paulb@46 | 438 | """ |
paulb@46 | 439 | |
paulb@46 | 440 | name = "radiolist" |
paulb@46 | 441 | info = { |
paulb@49 | 442 | "kdialog" : (_readvalues_kdialog(_readfrom), ["--radiolist", String("text"), ListItemList("items")]), |
paulb@49 | 443 | "zenity" : (_readvalues_zenity(_readfrom), |
paulb@46 | 444 | ["--list", "--radiolist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@47 | 445 | ListItemList("items", 1)] |
paulb@46 | 446 | ), |
paulb@49 | 447 | "Xdialog" : (_readvalues_Xdialog(_readfrom), ["--stdout", "--radiolist", |
paulb@46 | 448 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), ListItemList("items")] |
paulb@46 | 449 | ), |
paulb@46 | 450 | } |
paulb@49 | 451 | number_of_titles = 3 |
paulb@46 | 452 | |
paulb@46 | 453 | class CheckList(Menu): |
paulb@46 | 454 | |
paulb@46 | 455 | """ |
paulb@46 | 456 | A list of checkboxes, many being selectable. |
paulb@46 | 457 | Options: text, width (in characters), height (in characters), |
paulb@57 | 458 | list_height (in items), items (MenuItem objects), titles |
paulb@57 | 459 | Response: a list of values corresponding to chosen items |
paulb@46 | 460 | """ |
paulb@46 | 461 | |
paulb@46 | 462 | name = "checklist" |
paulb@46 | 463 | info = { |
paulb@49 | 464 | "kdialog" : (_readvalues_kdialog(_readfrom), ["--checklist", String("text"), ListItemList("items")]), |
paulb@49 | 465 | "zenity" : (_readvalues_zenity(_readfrom), |
paulb@46 | 466 | ["--list", "--checklist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@47 | 467 | ListItemList("items", 1)] |
paulb@46 | 468 | ), |
paulb@49 | 469 | "Xdialog" : (_readvalues_Xdialog(_readfrom), ["--stdout", "--checklist", |
paulb@46 | 470 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), ListItemList("items")] |
paulb@46 | 471 | ), |
paulb@46 | 472 | } |
paulb@49 | 473 | number_of_titles = 3 |
paulb@46 | 474 | |
paulb@46 | 475 | class Pulldown(Menu): |
paulb@46 | 476 | |
paulb@46 | 477 | """ |
paulb@46 | 478 | A pull-down menu of options, one of which being selectable. |
paulb@46 | 479 | Options: text, width (in characters), height (in characters), |
paulb@57 | 480 | items (list of values) |
paulb@57 | 481 | Response: a value corresponding to the chosen item |
paulb@46 | 482 | """ |
paulb@46 | 483 | |
paulb@46 | 484 | name = "pulldown" |
paulb@46 | 485 | info = { |
paulb@49 | 486 | "kdialog" : (_readvalue(_readfrom), ["--combobox", String("text"), Strings("items")]), |
paulb@49 | 487 | "zenity" : (_readvalue(_readfrom), |
paulb@49 | 488 | ["--list", "--radiolist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@49 | 489 | StringPairs("items")] |
paulb@46 | 490 | ), |
paulb@49 | 491 | "Xdialog" : (_readvalue(_readfrom), |
paulb@49 | 492 | ["--stdout", "--combobox", String("text"), Integer("height"), Integer("width"), Strings("items")]), |
paulb@46 | 493 | } |
paulb@49 | 494 | item = unicode |
paulb@49 | 495 | number_of_titles = 2 |
paulb@46 | 496 | |
paulb@46 | 497 | class Input(Simple): |
paulb@46 | 498 | |
paulb@46 | 499 | """ |
paulb@46 | 500 | An input dialogue, consisting of an input field. |
paulb@49 | 501 | Options: text, input, width (in characters), height (in characters) |
paulb@57 | 502 | Response: the text entered into the dialogue by the user |
paulb@46 | 503 | """ |
paulb@46 | 504 | |
paulb@46 | 505 | name = "input" |
paulb@46 | 506 | info = { |
paulb@49 | 507 | "kdialog" : (_readinput(_readfrom), |
paulb@49 | 508 | ["--inputbox", String("text"), String("data")]), |
paulb@49 | 509 | "zenity" : (_readinput(_readfrom), |
paulb@49 | 510 | ["--entry", StringKeyword("--text", "text"), StringKeyword("--entry-text", "data")]), |
paulb@49 | 511 | "Xdialog" : (_readinput(_readfrom), |
paulb@49 | 512 | ["--stdout", "--inputbox", String("text"), Integer("height"), Integer("width"), String("data")]), |
paulb@46 | 513 | } |
paulb@46 | 514 | |
paulb@49 | 515 | def __init__(self, text, data="", width=None, height=None): |
paulb@46 | 516 | Simple.__init__(self, text, width, height) |
paulb@46 | 517 | self.data = data |
paulb@46 | 518 | |
paulb@46 | 519 | class Password(Input): |
paulb@46 | 520 | |
paulb@46 | 521 | """ |
paulb@46 | 522 | A password dialogue, consisting of a password entry field. |
paulb@49 | 523 | Options: text, width (in characters), height (in characters) |
paulb@57 | 524 | Response: the text entered into the dialogue by the user |
paulb@46 | 525 | """ |
paulb@46 | 526 | |
paulb@46 | 527 | name = "password" |
paulb@46 | 528 | info = { |
paulb@49 | 529 | "kdialog" : (_readinput(_readfrom), |
paulb@49 | 530 | ["--password", String("text")]), |
paulb@49 | 531 | "zenity" : (_readinput(_readfrom), |
paulb@49 | 532 | ["--entry", StringKeyword("--text", "text"), "--hide-text"]), |
paulb@49 | 533 | "Xdialog" : (_readinput(_readfrom), |
paulb@49 | 534 | ["--stdout", "--password", "--inputbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 535 | } |
paulb@46 | 536 | |
paulb@46 | 537 | class TextFile(Simple): |
paulb@46 | 538 | |
paulb@46 | 539 | """ |
paulb@46 | 540 | A text file input box. |
paulb@49 | 541 | Options: filename, text, width (in characters), height (in characters) |
paulb@58 | 542 | Response: any text returned by the dialogue program (typically an empty |
paulb@58 | 543 | string) |
paulb@46 | 544 | """ |
paulb@46 | 545 | |
paulb@46 | 546 | name = "textfile" |
paulb@46 | 547 | info = { |
paulb@49 | 548 | "kdialog" : (_readfrom, ["--textbox", String("filename"), Integer("width", pixels=1), Integer("height", pixels=1)]), |
paulb@49 | 549 | "zenity" : (_readfrom, ["--text-info", StringKeyword("--filename", "filename"), IntegerKeyword("--width", "width", pixels=1), |
paulb@49 | 550 | IntegerKeyword("--height", "height", pixels=1)] |
paulb@46 | 551 | ), |
paulb@49 | 552 | "Xdialog" : (_readfrom, ["--stdout", "--textbox", String("filename"), Integer("height"), Integer("width")]), |
paulb@46 | 553 | } |
paulb@46 | 554 | |
paulb@49 | 555 | def __init__(self, filename, text="", width=None, height=None): |
paulb@46 | 556 | Simple.__init__(self, text, width, height) |
paulb@46 | 557 | self.filename = filename |
paulb@46 | 558 | |
paul@89 | 559 | class FileSelector(Simple): |
paul@89 | 560 | |
paul@89 | 561 | """ |
paul@89 | 562 | A file selector dialogue. |
paul@89 | 563 | Options: directory to start in |
paul@89 | 564 | Response: a filename |
paul@89 | 565 | """ |
paul@89 | 566 | |
paul@89 | 567 | name = "fileselector" |
paul@89 | 568 | info = { |
paul@89 | 569 | "kdialog" : (_readvalue(_readfrom), ["--getopenfilename", String("directory")]), |
paul@89 | 570 | "zenity" : (_readvalue(_readfrom), ["--file-selection", StringKeyword("--filename", "directory")]), |
paul@89 | 571 | "Xdialog" : (_readvalue(_readfrom), ["--fselect", String("directory")]), |
paul@89 | 572 | } |
paul@89 | 573 | |
paul@89 | 574 | def __init__(self, directory, text="", width=None, height=None): |
paul@89 | 575 | Simple.__init__(self, text, width, height) |
paul@89 | 576 | self.directory = directory |
paul@89 | 577 | |
paul@89 | 578 | class DirectorySelector(Simple): |
paul@89 | 579 | |
paul@89 | 580 | """ |
paul@89 | 581 | A directory selector dialogue. |
paul@89 | 582 | Options: directory to start in |
paul@89 | 583 | Response: a filename |
paul@89 | 584 | """ |
paul@89 | 585 | |
paul@89 | 586 | name = "directoryselector" |
paul@89 | 587 | info = { |
paul@89 | 588 | "kdialog" : (_readvalue(_readfrom), ["--getexistingdirectory", String("directory")]), |
paul@89 | 589 | "zenity" : (_readvalue(_readfrom), ["--file-selection", "--directory", StringKeyword("--filename", "directory")]), |
paul@89 | 590 | "Xdialog" : (_readvalue(_readfrom), ["--dselect", String("directory")]), |
paul@89 | 591 | } |
paul@89 | 592 | |
paul@89 | 593 | def __init__(self, directory, text="", width=None, height=None): |
paul@89 | 594 | Simple.__init__(self, text, width, height) |
paul@89 | 595 | self.directory = directory |
paul@89 | 596 | |
paul@89 | 597 | class Calendar(Simple): |
paul@89 | 598 | |
paul@89 | 599 | """ |
paul@89 | 600 | A calendar dialogue. |
paul@89 | 601 | Response: a tuple of the form (year, month, day number) |
paul@89 | 602 | """ |
paul@89 | 603 | |
paul@89 | 604 | name = "calendar" |
paul@89 | 605 | info = { |
paul@89 | 606 | "kdialog" : (_readdate_kdialog(_readfrom), ["--calendar", String("text")]), |
paul@89 | 607 | "zenity" : (_readdate_zenity(_readfrom), ["--calendar", "--date-format", "%Y %m %d"]), |
paul@89 | 608 | "Xdialog" : (_readdate_kdialog(_readfrom), ["--calendar", String("text")]), |
paul@89 | 609 | } |
paul@89 | 610 | |
paulb@47 | 611 | # Available dialogues. |
paulb@47 | 612 | |
paul@89 | 613 | available = [Question, Warning, Message, Error, Menu, CheckList, RadioList, Input, Password, Pulldown, TextFile, Calendar, |
paul@89 | 614 | FileSelector, DirectorySelector] |
paulb@47 | 615 | |
paulb@57 | 616 | # Supported desktop environments. |
paulb@57 | 617 | |
paulb@57 | 618 | supported = Dialogue.commands.keys() |
paulb@57 | 619 | |
paulb@46 | 620 | # vim: tabstop=4 expandtab shiftwidth=4 |