Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 1 | """Edit the Python Preferences file.""" |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 2 | # |
| 3 | # This program is getting more and more clunky. It should really |
| 4 | # be rewritten in a modeless way some time soon. |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 5 | |
| 6 | from Dlg import * |
| 7 | from Events import * |
| 8 | from Res import * |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 9 | import Controls |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 10 | import string |
| 11 | import struct |
| 12 | import macfs |
| 13 | import MacOS |
| 14 | import os |
| 15 | import sys |
| 16 | import Res # For Res.Error |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 17 | import pythonprefs |
| 18 | import EasyDialogs |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 19 | import Help |
Jack Jansen | 8487229 | 1996-10-22 15:33:02 +0000 | [diff] [blame] | 20 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 21 | # resource IDs in our own resources (dialogs, etc) |
| 22 | MESSAGE_ID = 256 |
| 23 | |
Jack Jansen | 145c92d | 1996-10-11 11:30:26 +0000 | [diff] [blame] | 24 | DIALOG_ID = 511 |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 25 | TEXT_ITEM = 1 |
| 26 | OK_ITEM = 2 |
| 27 | CANCEL_ITEM = 3 |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 28 | DIR_ITEM = 4 |
| 29 | TITLE_ITEM = 5 |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 30 | OPTIONS_ITEM = 7 |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 31 | HELP_ITEM = 9 |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 32 | |
| 33 | # The options dialog. There is a correspondence between |
| 34 | # the dialog item numbers and the option. |
Jack Jansen | 145c92d | 1996-10-11 11:30:26 +0000 | [diff] [blame] | 35 | OPT_DIALOG_ID = 510 |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 36 | |
| 37 | # Map dialog item numbers to option names (and the reverse) |
| 38 | opt_dialog_map = [ |
| 39 | None, |
| 40 | "inspect", |
| 41 | "verbose", |
| 42 | "optimize", |
| 43 | "unbuffered", |
| 44 | "debugging", |
| 45 | "keepopen", |
| 46 | "keeperror", |
| 47 | "nointopt", |
| 48 | "noargs", |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 49 | "delayconsole", |
| 50 | None, None, None, None, None, None, None, None, # 11-18 are different |
| 51 | "oldexc", |
| 52 | "nosite"] |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 53 | opt_dialog_dict = {} |
| 54 | for i in range(len(opt_dialog_map)): |
| 55 | if opt_dialog_map[i]: |
| 56 | opt_dialog_dict[opt_dialog_map[i]] = i |
| 57 | # 1 thru 10 are the options |
Jack Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 58 | # The GUSI creator/type and delay-console |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 59 | OD_CREATOR_ITEM = 11 |
| 60 | OD_TYPE_ITEM = 12 |
Jack Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 61 | OD_OK_ITEM = 13 |
| 62 | OD_CANCEL_ITEM = 14 |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 63 | OD_HELP_ITEM = 22 |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 64 | |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 65 | def optinteract(options): |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 66 | """Let the user interact with the options dialog""" |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 67 | d = GetNewDialog(OPT_DIALOG_ID, -1) |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 68 | htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM) |
| 69 | SetDialogItemText(htext, options['creator']) |
| 70 | htext = d.GetDialogItemAsControl(OD_TYPE_ITEM) |
| 71 | SetDialogItemText(htext, options['type']) |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 72 | d.SetDialogDefaultItem(OD_OK_ITEM) |
| 73 | d.SetDialogCancelItem(OD_CANCEL_ITEM) |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 74 | |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 75 | while 1: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 76 | for name in opt_dialog_dict.keys(): |
| 77 | num = opt_dialog_dict[name] |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 78 | ctl = d.GetDialogItemAsControl(num) |
| 79 | ctl.SetControlValue(options[name]) |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 80 | n = ModalDialog(None) |
| 81 | if n == OD_OK_ITEM: |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 82 | htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM) |
| 83 | ncreator = GetDialogItemText(htext) |
| 84 | htext = d.GetDialogItemAsControl(OD_TYPE_ITEM) |
| 85 | ntype = GetDialogItemText(htext) |
Jack Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 86 | if len(ncreator) == 4 and len(ntype) == 4: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 87 | options['creator'] = ncreator |
| 88 | options['type'] = ntype |
| 89 | return options |
Jack Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 90 | else: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 91 | MacOS.SysBeep() |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 92 | elif n == OD_CANCEL_ITEM: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 93 | return |
Jack Jansen | 6c4e987 | 1996-09-06 22:25:34 +0000 | [diff] [blame] | 94 | elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM): |
| 95 | pass |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 96 | elif n == OD_HELP_ITEM: |
| 97 | onoff = Help.HMGetBalloons() |
| 98 | Help.HMSetBalloons(not onoff) |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 99 | elif 1 <= n <= len(opt_dialog_map): |
| 100 | options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]]) |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 101 | |
| 102 | |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 103 | def interact(options, title): |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 104 | """Let the user interact with the dialog""" |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 105 | try: |
| 106 | # Try to go to the "correct" dir for GetDirectory |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 107 | os.chdir(options['dir'].as_pathname()) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 108 | except os.error: |
| 109 | pass |
| 110 | d = GetNewDialog(DIALOG_ID, -1) |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 111 | htext = d.GetDialogItemAsControl(TITLE_ITEM) |
| 112 | SetDialogItemText(htext, title) |
Jack Jansen | 8242c9e | 2000-01-13 16:22:12 +0000 | [diff] [blame^] | 113 | path_ctl = d.GetDialogItemAsControl(TEXT_ITEM) |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 114 | data = string.joinfields(options['path'], '\r') |
Jack Jansen | 8242c9e | 2000-01-13 16:22:12 +0000 | [diff] [blame^] | 115 | path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data) |
Jack Jansen | 6a6db07 | 1999-12-23 14:34:07 +0000 | [diff] [blame] | 116 | |
Jack Jansen | a918b8c | 1996-11-20 14:55:26 +0000 | [diff] [blame] | 117 | d.SelectDialogItemText(TEXT_ITEM, 0, 32767) |
| 118 | d.SelectDialogItemText(TEXT_ITEM, 0, 0) |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 119 | ## d.SetDialogDefaultItem(OK_ITEM) |
| 120 | d.SetDialogCancelItem(CANCEL_ITEM) |
Jack Jansen | a918b8c | 1996-11-20 14:55:26 +0000 | [diff] [blame] | 121 | d.GetDialogWindow().ShowWindow() |
| 122 | d.DrawDialog() |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 123 | while 1: |
| 124 | n = ModalDialog(None) |
| 125 | if n == OK_ITEM: |
| 126 | break |
| 127 | if n == CANCEL_ITEM: |
| 128 | return None |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 129 | ## if n == REVERT_ITEM: |
| 130 | ## return [], pythondir |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 131 | if n == DIR_ITEM: |
Jack Jansen | 9062fa2 | 1995-08-14 12:21:12 +0000 | [diff] [blame] | 132 | fss, ok = macfs.GetDirectory('Select python home folder:') |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 133 | if ok: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 134 | options['dir'] = fss |
Jack Jansen | b95901e | 1997-09-09 13:58:19 +0000 | [diff] [blame] | 135 | elif n == HELP_ITEM: |
| 136 | onoff = Help.HMGetBalloons() |
| 137 | Help.HMSetBalloons(not onoff) |
Jack Jansen | 822a30b | 1996-04-10 14:52:18 +0000 | [diff] [blame] | 138 | if n == OPTIONS_ITEM: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 139 | noptions = options |
| 140 | for k in options.keys(): |
| 141 | noptions[k] = options[k] |
| 142 | noptions = optinteract(noptions) |
| 143 | if noptions: |
| 144 | options = noptions |
Jack Jansen | 8242c9e | 2000-01-13 16:22:12 +0000 | [diff] [blame^] | 145 | data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag) |
| 146 | tmp = string.splitfields(data, '\r') |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 147 | newpath = [] |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 148 | for i in tmp: |
| 149 | if i: |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 150 | newpath.append(i) |
| 151 | options['path'] = newpath |
| 152 | return options |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 153 | |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 154 | |
| 155 | def edit_preferences(): |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 156 | handler = pythonprefs.PythonOptions() |
Jack Jansen | 43fd1f7 | 1999-12-02 22:52:12 +0000 | [diff] [blame] | 157 | options = handler.load() |
| 158 | if options['noargs']: |
| 159 | EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.') |
| 160 | result = interact(options, 'System-wide preferences') |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 161 | if result: |
| 162 | handler.save(result) |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 163 | |
| 164 | def edit_applet(name): |
Jack Jansen | 3b3a287 | 1997-09-08 13:19:42 +0000 | [diff] [blame] | 165 | handler = pythonprefs.AppletOptions(name) |
| 166 | result = interact(handler.load(), os.path.split(name)[1]) |
| 167 | if result: |
| 168 | handler.save(result) |
Jack Jansen | db0bace | 1996-04-04 15:38:44 +0000 | [diff] [blame] | 169 | |
| 170 | def main(): |
| 171 | try: |
| 172 | h = OpenResFile('EditPythonPrefs.rsrc') |
| 173 | except Res.Error: |
| 174 | pass # Assume we already have acces to our own resource |
| 175 | |
| 176 | if len(sys.argv) <= 1: |
| 177 | edit_preferences() |
| 178 | else: |
| 179 | for appl in sys.argv[1:]: |
| 180 | edit_applet(appl) |
| 181 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 182 | |
| 183 | if __name__ == '__main__': |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 184 | main() |