diff options
author | Jack Jansen <jack.jansen@cwi.nl> | 1996-04-04 15:38:44 (GMT) |
---|---|---|
committer | Jack Jansen <jack.jansen@cwi.nl> | 1996-04-04 15:38:44 (GMT) |
commit | db0baced2309b358d706f5f027a2ecd2e581e609 (patch) | |
tree | a45a145ca223fa1b0206205189ed325c24d28204 /Mac/scripts/EditPythonPrefs.py | |
parent | bbb4e10e91f231bcb1cb012198b2262333f3e0ed (diff) | |
download | cpython-db0baced2309b358d706f5f027a2ecd2e581e609.zip cpython-db0baced2309b358d706f5f027a2ecd2e581e609.tar.gz cpython-db0baced2309b358d706f5f027a2ecd2e581e609.tar.bz2 |
Added support for editing override preferences in applets
Diffstat (limited to 'Mac/scripts/EditPythonPrefs.py')
-rw-r--r-- | Mac/scripts/EditPythonPrefs.py | 157 |
1 files changed, 132 insertions, 25 deletions
diff --git a/Mac/scripts/EditPythonPrefs.py b/Mac/scripts/EditPythonPrefs.py index f070d82..0fd6bed 100644 --- a/Mac/scripts/EditPythonPrefs.py +++ b/Mac/scripts/EditPythonPrefs.py @@ -18,13 +18,20 @@ DIALOG_ID = 512 TEXT_ITEM = 1 OK_ITEM = 2 CANCEL_ITEM = 3 -REVERT_ITEM = 4 -DIR_ITEM = 5 +DIR_ITEM = 4 +TITLE_ITEM = 5 # Resource IDs in the preferences file PATH_STRINGS_ID = 128 DIRECTORY_ID = 128 +OPTIONS_ID = 128 +# Override IDs (in the applet) +OVERRIDE_PATH_STRINGS_ID = 129 +OVERRIDE_DIRECTORY_ID = 129 +OVERRIDE_OPTIONS_ID = 129 + +READ = 1 WRITE = 2 smAllScripts = -3 kOnSystemDisk = 0x8000 @@ -61,7 +68,7 @@ def message(str = "Hello, world!", id = MESSAGE_ID): n = ModalDialog(None) if n == 1: break -def interact(list, pythondir): +def interact(list, pythondir, title): """Let the user interact with the dialog""" opythondir = pythondir try: @@ -70,16 +77,20 @@ def interact(list, pythondir): except os.error: pass d = GetNewDialog(DIALOG_ID, -1) - tp, h, rect = d.GetDialogItem(1) + tp, h, rect = d.GetDialogItem(TITLE_ITEM) + SetDialogItemText(h, title) + tp, h, rect = d.GetDialogItem(TEXT_ITEM) SetDialogItemText(h, string.joinfields(list, '\r')) +## d.SetDialogDefaultItem(OK_ITEM) + d.SetDialogCancelItem(CANCEL_ITEM) while 1: n = ModalDialog(None) if n == OK_ITEM: break if n == CANCEL_ITEM: return None - if n == REVERT_ITEM: - return [], pythondir +## if n == REVERT_ITEM: +## return [], pythondir if n == DIR_ITEM: fss, ok = macfs.GetDirectory('Select python home folder:') if ok: @@ -91,42 +102,62 @@ def interact(list, pythondir): rv.append(i) return rv, pythondir -def main(): +def getprefpath(id): + # Load the path and directory resources try: - h = OpenResFile('EditPythonPrefs.rsrc') - except Res.Error: - pass # Assume we already have acces to our own resource + sr = GetResource('STR#', id) + except (MacOS.Error, Res.Error): + return None, None + d = sr.data + l = restolist(d) + return l, sr + +def getprefdir(id): + try: + dr = GetResource('alis', id) + fss, fss_changed = macfs.RawAlias(dr.data).Resolve() + except (MacOS.Error, Res.Error): + return None, None, 1 + return fss, dr, fss_changed +def openpreffile(rw): # Find the preferences folder and our prefs file, create if needed. vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0) preff_fss = macfs.FSSpec((vrefnum, dirid, 'Python Preferences')) try: - preff_handle = FSpOpenResFile(preff_fss, WRITE) + preff_handle = FSpOpenResFile(preff_fss, rw) except Res.Error: # Create it message('No preferences file, creating one...') FSpCreateResFile(preff_fss, 'Pyth', 'pref', smAllScripts) - preff_handle = FSpOpenResFile(preff_fss, WRITE) - - # Load the path and directory resources + preff_handle = FSpOpenResFile(preff_fss, rw) + return preff_handle + +def openapplet(name): + fss = macfs.FSSpec(name) try: - sr = GetResource('STR#', PATH_STRINGS_ID) - except (MacOS.Error, Res.Error): - message('Cannot find any sys.path resource! (Old python?)') + app_handle = FSpOpenResFile(fss, WRITE) + except Res.Error: + message('File does not have a resource fork.') sys.exit(0) - d = sr.data - l = restolist(d) + return app_handle + - try: - dr = GetResource('alis', DIRECTORY_ID) - fss, fss_changed = macfs.RawAlias(dr.data).Resolve() - except (MacOS.Error, Res.Error): - dr = None +def edit_preferences(): + preff_handle = openpreffile(WRITE) + + l, sr = getprefpath(PATH_STRINGS_ID) + if l == None: + message('Cannot find any sys.path resource! (Old python?)') + sys.exit(0) + + fss, dr, fss_changed = getprefdir(DIRECTORY_ID) + if fss == None: fss = macfs.FSSpec(os.getcwd()) fss_changed = 1 # Let the user play away - result = interact(l, fss) + result = interact(l, fss, 'System-wide preferences') # See what we have to update, and how if result == None: @@ -158,6 +189,82 @@ def main(): sr.AddResource('STR#', PATH_STRINGS_ID, '') CloseResFile(preff_handle) + +def edit_applet(name): + pref_handle = openpreffile(READ) + app_handle = openapplet(name) + + notfound = '' + l, sr = getprefpath(OVERRIDE_PATH_STRINGS_ID) + if l == None: + notfound = 'path' + + l, dummy = getprefpath(PATH_STRINGS_ID) + if l == None: + message('Cannot find any sys.path resource! (Old python?)') + sys.exit(0) + + fss, dr, fss_changed = getprefdir(OVERRIDE_DIRECTORY_ID) + if fss == None: + if notfound: + notfound = notfound + ' and ' + 'directory' + else: + notfound = 'directory' + fss, dummy, dummy2 = getprefdir(DIRECTORY_ID) + if fss == None: + fss = macfs.FSSpec(os.getcwd()) + fss_changed = 1 + + dummy = dummy2 = None # Discard them. + + if notfound: + message('Warning: initial %s taken from system-wide defaults'%notfound) + # Let the user play away + result = interact(l, fss, name) + + # See what we have to update, and how + if result == None: + sys.exit(0) + + pathlist, nfss = result + if nfss != fss: + fss_changed = 1 + + if fss_changed or pathlist != l: + if fss_changed: + alias = nfss.NewAlias() + if dr: + dr.data = alias.data + dr.ChangedResource() + else: + dr = Resource(alias.data) + dr.AddResource('alis', OVERRIDE_DIRECTORY_ID, '') + + if pathlist != l: + if pathlist == []: + if sr.HomeResFile() == app_handle: + sr.RemoveResource() + elif sr and sr.HomeResFile() == app_handle: + sr.data = listtores(pathlist) + sr.ChangedResource() + else: + sr = Resource(listtores(pathlist)) + sr.AddResource('STR#', OVERRIDE_PATH_STRINGS_ID, '') + + CloseResFile(app_handle) + +def main(): + try: + h = OpenResFile('EditPythonPrefs.rsrc') + except Res.Error: + pass # Assume we already have acces to our own resource + + if len(sys.argv) <= 1: + edit_preferences() + else: + for appl in sys.argv[1:]: + edit_applet(appl) + if __name__ == '__main__': print # Stupid, to init toolboxes... |