Mercurial > cpython-lru_cache-weakref
comparison Mac/scripts/EditPythonPrefs.py @ 2873:315760ba0041 legacy-trunk
A new home for scripts/applets
EditPythonPrefs - Edit the preferences file (sys.path, python home)
PackLibDir - Convert a sys.path directory to a resource file
RunLibScript - import a module and run as __main__
mkapplet - Create a python applet
The resource files belonging to these should also be stored here, somehow..
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Sat, 29 Jul 1995 13:48:41 +0000 |
| parents | |
| children | 7eea9b426802 |
comparison
equal
deleted
inserted
replaced
| 2872:d3b07ba3e12d | 2873:315760ba0041 |
|---|---|
| 1 """Edit the Python Preferences file.""" | |
| 2 import addpack | |
| 3 addpack.addpack('Tools') | |
| 4 addpack.addpack('bgen') | |
| 5 addpack.addpack('evt') | |
| 6 | |
| 7 from Dlg import * | |
| 8 from Events import * | |
| 9 from Res import * | |
| 10 import string | |
| 11 import struct | |
| 12 import macfs | |
| 13 import MacOS | |
| 14 import os | |
| 15 import sys | |
| 16 import Res # For Res.Error | |
| 17 | |
| 18 # resource IDs in our own resources (dialogs, etc) | |
| 19 MESSAGE_ID = 256 | |
| 20 | |
| 21 DIALOG_ID = 131 | |
| 22 TEXT_ITEM = 1 | |
| 23 OK_ITEM = 2 | |
| 24 CANCEL_ITEM = 3 | |
| 25 REVERT_ITEM = 4 | |
| 26 DIR_ITEM = 5 | |
| 27 | |
| 28 # Resource IDs in the preferences file | |
| 29 PATH_STRINGS_ID = 128 | |
| 30 DIRECTORY_ID = 128 | |
| 31 | |
| 32 WRITE = 2 | |
| 33 smAllScripts = -3 | |
| 34 kOnSystemDisk = 0x8000 | |
| 35 | |
| 36 def restolist(data): | |
| 37 """Convert STR# resource data to a list of strings""" | |
| 38 if not data: | |
| 39 return [] | |
| 40 num, = struct.unpack('h', data[:2]) | |
| 41 data = data[2:] | |
| 42 rv = [] | |
| 43 for i in range(num): | |
| 44 strlen = ord(data[0]) | |
| 45 if strlen < 0: strlen = strlen + 256 | |
| 46 str = data[1:strlen+1] | |
| 47 data = data[strlen+1:] | |
| 48 rv.append(str) | |
| 49 return rv | |
| 50 | |
| 51 def listtores(list): | |
| 52 """Convert a list of strings to STR# resource data""" | |
| 53 rv = struct.pack('h', len(list)) | |
| 54 for str in list: | |
| 55 rv = rv + chr(len(str)) + str | |
| 56 return rv | |
| 57 | |
| 58 def message(str = "Hello, world!", id = MESSAGE_ID): | |
| 59 """Show a simple alert with a text message""" | |
| 60 d = GetNewDialog(id, -1) | |
| 61 print 'd=', d | |
| 62 tp, h, rect = d.GetDialogItem(2) | |
| 63 SetDialogItemText(h, str) | |
| 64 while 1: | |
| 65 n = ModalDialog(None) | |
| 66 if n == 1: break | |
| 67 | |
| 68 def interact(list, pythondir): | |
| 69 """Let the user interact with the dialog""" | |
| 70 opythondir = pythondir | |
| 71 try: | |
| 72 # Try to go to the "correct" dir for GetDirectory | |
| 73 os.chdir(pythondir.as_pathname()) | |
| 74 except os.error: | |
| 75 pass | |
| 76 d = GetNewDialog(DIALOG_ID, -1) | |
| 77 tp, h, rect = d.GetDialogItem(1) | |
| 78 SetDialogItemText(h, string.joinfields(list, '\r')) | |
| 79 while 1: | |
| 80 n = ModalDialog(None) | |
| 81 if n == OK_ITEM: | |
| 82 break | |
| 83 if n == CANCEL_ITEM: | |
| 84 return None | |
| 85 if n == REVERT_ITEM: | |
| 86 return [], pythondir | |
| 87 if n == DIR_ITEM: | |
| 88 fss, ok = macfs.GetDirectory() | |
| 89 if ok: | |
| 90 pythondir = fss | |
| 91 tmp = string.splitfields(GetDialogItemText(h), '\r') | |
| 92 rv = [] | |
| 93 for i in tmp: | |
| 94 if i: | |
| 95 rv.append(i) | |
| 96 return rv, pythondir | |
| 97 | |
| 98 def main(): | |
| 99 try: | |
| 100 h = OpenResFile('EditPythonPrefs.rsrc') | |
| 101 except Res.Error: | |
| 102 pass # Assume we already have acces to our own resource | |
| 103 | |
| 104 # Find the preferences folder and our prefs file, create if needed. | |
| 105 vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0) | |
| 106 preff_fss = macfs.FSSpec((vrefnum, dirid, 'Python Preferences')) | |
| 107 try: | |
| 108 preff_handle = FSpOpenResFile(preff_fss, WRITE) | |
| 109 except Res.Error: | |
| 110 # Create it | |
| 111 message('No preferences file, creating one...') | |
| 112 FSpCreateResFile(preff_fss, 'PYTH', 'pref', smAllScripts) | |
| 113 preff_handle = FSpOpenResFile(preff_fss, WRITE) | |
| 114 | |
| 115 # Load the path and directory resources | |
| 116 try: | |
| 117 sr = GetResource('STR#', PATH_STRINGS_ID) | |
| 118 except (MacOS.Error, Res.Error): | |
| 119 message('Cannot find any sys.path resource! (Old python?)') | |
| 120 sys.exit(0) | |
| 121 d = sr.data | |
| 122 l = restolist(d) | |
| 123 | |
| 124 try: | |
| 125 dr = GetResource('alis', DIRECTORY_ID) | |
| 126 fss, fss_changed = macfs.RawAlias(dr.data).Resolve() | |
| 127 except (MacOS.Error, Res.Error): | |
| 128 dr = None | |
| 129 fss = macfs.FSSpec(os.getcwd()) | |
| 130 fss_changed = 1 | |
| 131 | |
| 132 # Let the user play away | |
| 133 result = interact(l, fss) | |
| 134 | |
| 135 # See what we have to update, and how | |
| 136 if result == None: | |
| 137 sys.exit(0) | |
| 138 | |
| 139 pathlist, nfss = result | |
| 140 if nfss != fss: | |
| 141 fss_changed = 1 | |
| 142 | |
| 143 if fss_changed or pathlist != l: | |
| 144 if fss_changed: | |
| 145 alias = nfss.NewAlias() | |
| 146 if dr: | |
| 147 dr.data = alias.data | |
| 148 dr.ChangedResource() | |
| 149 else: | |
| 150 dr = Resource(alias.data) | |
| 151 dr.AddResource('alis', DIRECTORY_ID, '') | |
| 152 | |
| 153 if pathlist != l: | |
| 154 if pathlist == []: | |
| 155 if sr.HomeResFile() == preff_handle: | |
| 156 sr.RemoveResource() | |
| 157 elif sr.HomeResFile() == preff_handle: | |
| 158 sr.data = listtores(pathlist) | |
| 159 sr.ChangedResource() | |
| 160 else: | |
| 161 sr = Resource(listtores(pathlist)) | |
| 162 sr.AddResource('STR#', PATH_STRINGS_ID, '') | |
| 163 | |
| 164 CloseResFile(preff_handle) | |
| 165 | |
| 166 if __name__ == '__main__': | |
| 167 print # Stupid, to init toolboxes... | |
| 168 main() |
