1"""Easy to use dialogs. Modified to avoid talking to the window server. 2Only Message() is supported, and prints to stdout. The other routines will 3throw a NotImplementedError exception. 4 5Message(msg) -- display a message and an OK button. 6AskString(prompt, default) -- ask for a string, display OK and Cancel buttons. 7AskPassword(prompt, default) -- like AskString(), but shows text as bullets. 8AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons. 9GetArgv(optionlist, commandlist) -- fill a sys.argv-like list using a dialog 10AskFileForOpen(...) -- Ask the user for an existing file 11AskFileForSave(...) -- Ask the user for an output file 12AskFolder(...) -- Ask the user to select a folder 13bar = Progress(label, maxvalue) -- Display a progress bar 14bar.set(value) -- Set value 15bar.inc( *amount ) -- increment value by amount (default=1) 16bar.label( *newlabel ) -- get or set text label. 17 18More documentation in each function. 19This module uses DLOG resources 260 and on. 20Based upon STDWIN dialogs with the same names and functions. 21""" 22 23import os 24import sys 25 26__all__ = ['Message', 'AskString', 'AskPassword', 'AskYesNoCancel', 27 'GetArgv', 'AskFileForOpen', 'AskFileForSave', 'AskFolder', 28 'ProgressBar'] 29 30def cr2lf(text): 31 if '\r' in text: 32 text = string.join(string.split(text, '\r'), '\n') 33 return text 34 35def lf2cr(text): 36 if '\n' in text: 37 text = string.join(string.split(text, '\n'), '\r') 38 if len(text) > 253: 39 text = text[:253] + '\311' 40 return text 41 42def Message(msg, id=260, ok=None): 43 """Display a MESSAGE string. 44 45 Return when the user clicks the OK button or presses Return. 46 47 The MESSAGE string can be at most 255 characters long. 48 """ 49 sys.stderr.write(msg+'\n') 50 51 52def AskString(prompt, default = "", id=261, ok=None, cancel=None): 53 """Display a PROMPT string and a text entry field with a DEFAULT string. 54 55 Return the contents of the text entry field when the user clicks the 56 OK button or presses Return. 57 Return None when the user clicks the Cancel button. 58 59 If omitted, DEFAULT is empty. 60 61 The PROMPT and DEFAULT strings, as well as the return value, 62 can be at most 255 characters long. 63 """ 64 65 raise NotImplementedError("AskString") 66 67def AskPassword(prompt, default='', id=264, ok=None, cancel=None): 68 """Display a PROMPT string and a text entry field with a DEFAULT string. 69 The string is displayed as bullets only. 70 71 Return the contents of the text entry field when the user clicks the 72 OK button or presses Return. 73 Return None when the user clicks the Cancel button. 74 75 If omitted, DEFAULT is empty. 76 77 The PROMPT and DEFAULT strings, as well as the return value, 78 can be at most 255 characters long. 79 """ 80 raise NotImplementedError("AskPassword") 81 82def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=262): 83 """Display a QUESTION string which can be answered with Yes or No. 84 85 Return 1 when the user clicks the Yes button. 86 Return 0 when the user clicks the No button. 87 Return -1 when the user clicks the Cancel button. 88 89 When the user presses Return, the DEFAULT value is returned. 90 If omitted, this is 0 (No). 91 92 The QUESTION string can be at most 255 characters. 93 """ 94 95 raise NotImplementedError("AskYesNoCancel") 96 97class ProgressBar: 98 def __init__(self, title="Working...", maxval=0, label="", id=263): 99 raise NotImplementedError("ProgressBar") 100 101ARGV_ID=265 102ARGV_ITEM_OK=1 103ARGV_ITEM_CANCEL=2 104ARGV_OPTION_GROUP=3 105ARGV_OPTION_EXPLAIN=4 106ARGV_OPTION_VALUE=5 107ARGV_OPTION_ADD=6 108ARGV_COMMAND_GROUP=7 109ARGV_COMMAND_EXPLAIN=8 110ARGV_COMMAND_ADD=9 111ARGV_ADD_OLDFILE=10 112ARGV_ADD_NEWFILE=11 113ARGV_ADD_FOLDER=12 114ARGV_CMDLINE_GROUP=13 115ARGV_CMDLINE_DATA=14 116 117def GetArgv(optionlist=None, commandlist=None, addoldfile=1, addnewfile=1, addfolder=1, id=ARGV_ID): 118 raise NotImplementedError("GetArgv") 119 120def SetDefaultEventProc(proc): 121 raise NotImplementedError("SetDefaultEventProc") 122 123def AskFileForOpen( 124 message=None, 125 typeList=None, 126 # From here on the order is not documented 127 version=None, 128 defaultLocation=None, 129 dialogOptionFlags=None, 130 location=None, 131 clientName=None, 132 windowTitle=None, 133 actionButtonLabel=None, 134 cancelButtonLabel=None, 135 preferenceKey=None, 136 popupExtension=None, 137 eventProc=None, 138 previewProc=None, 139 filterProc=None, 140 wanted=None, 141 multiple=None): 142 """Display a dialog asking the user for a file to open. 143 144 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default) 145 the other arguments can be looked up in Apple's Navigation Services documentation""" 146 147 raise NotImplementedError("AskFileForOpen") 148 149def AskFileForSave( 150 message=None, 151 savedFileName=None, 152 # From here on the order is not documented 153 version=None, 154 defaultLocation=None, 155 dialogOptionFlags=None, 156 location=None, 157 clientName=None, 158 windowTitle=None, 159 actionButtonLabel=None, 160 cancelButtonLabel=None, 161 preferenceKey=None, 162 popupExtension=None, 163 eventProc=None, 164 fileType=None, 165 fileCreator=None, 166 wanted=None, 167 multiple=None): 168 """Display a dialog asking the user for a filename to save to. 169 170 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default) 171 the other arguments can be looked up in Apple's Navigation Services documentation""" 172 173 raise NotImplementedError("AskFileForSave") 174 175def AskFolder( 176 message=None, 177 # From here on the order is not documented 178 version=None, 179 defaultLocation=None, 180 dialogOptionFlags=None, 181 location=None, 182 clientName=None, 183 windowTitle=None, 184 actionButtonLabel=None, 185 cancelButtonLabel=None, 186 preferenceKey=None, 187 popupExtension=None, 188 eventProc=None, 189 filterProc=None, 190 wanted=None, 191 multiple=None): 192 """Display a dialog asking the user for select a folder. 193 194 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default) 195 the other arguments can be looked up in Apple's Navigation Services documentation""" 196 197 raise NotImplementedError("AskFolder") 198 199 200def test(): 201 import time 202 203 Message("Testing EasyDialogs.") 204 205if __name__ == '__main__': 206 try: 207 test() 208 except KeyboardInterrupt: 209 Message("Operation Canceled.") 210