1 """Edit the Python Preferences file."""
3 addpack
.addpack('Tools')
4 addpack
.addpack('bgen')
16 import Res
# For Res.Error
18 # resource IDs in our own resources (dialogs, etc)
28 # Resource IDs in the preferences file
34 kOnSystemDisk
= 0x8000
37 """Convert STR# resource data to a list of strings"""
40 num
, = struct
.unpack('h', data
[:2])
45 if strlen
< 0: strlen
= strlen
+ 256
46 str = data
[1:strlen
+1]
47 data
= data
[strlen
+1:]
52 """Convert a list of strings to STR# resource data"""
53 rv
= struct
.pack('h', len(list))
55 rv
= rv
+ chr(len(str)) + str
58 def message(str = "Hello, world!", id = MESSAGE_ID
):
59 """Show a simple alert with a text message"""
60 d
= GetNewDialog(id, -1)
62 tp
, h
, rect
= d
.GetDialogItem(2)
63 SetDialogItemText(h
, str)
68 def interact(list, pythondir
):
69 """Let the user interact with the dialog"""
70 opythondir
= pythondir
72 # Try to go to the "correct" dir for GetDirectory
73 os
.chdir(pythondir
.as_pathname())
76 d
= GetNewDialog(DIALOG_ID
, -1)
77 tp
, h
, rect
= d
.GetDialogItem(1)
78 SetDialogItemText(h
, string
.joinfields(list, '\r'))
88 fss
, ok
= macfs
.GetDirectory('Select python home folder:')
91 tmp
= string
.splitfields(GetDialogItemText(h
), '\r')
100 h
= OpenResFile('EditPythonPrefs.rsrc')
102 pass # Assume we already have acces to our own resource
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'))
108 preff_handle
= FSpOpenResFile(preff_fss
, WRITE
)
111 message('No preferences file, creating one...')
112 FSpCreateResFile(preff_fss
, 'PYTH', 'pref', smAllScripts
)
113 preff_handle
= FSpOpenResFile(preff_fss
, WRITE
)
115 # Load the path and directory resources
117 sr
= GetResource('STR#', PATH_STRINGS_ID
)
118 except (MacOS
.Error
, Res
.Error
):
119 message('Cannot find any sys.path resource! (Old python?)')
125 dr
= GetResource('alis', DIRECTORY_ID
)
126 fss
, fss_changed
= macfs
.RawAlias(dr
.data
).Resolve()
127 except (MacOS
.Error
, Res
.Error
):
129 fss
= macfs
.FSSpec(os
.getcwd())
132 # Let the user play away
133 result
= interact(l
, fss
)
135 # See what we have to update, and how
139 pathlist
, nfss
= result
143 if fss_changed
or pathlist
!= l
:
145 alias
= nfss
.NewAlias()
150 dr
= Resource(alias
.data
)
151 dr
.AddResource('alis', DIRECTORY_ID
, '')
155 if sr
.HomeResFile() == preff_handle
:
157 elif sr
.HomeResFile() == preff_handle
:
158 sr
.data
= listtores(pathlist
)
161 sr
= Resource(listtores(pathlist
))
162 sr
.AddResource('STR#', PATH_STRINGS_ID
, '')
164 CloseResFile(preff_handle
)
166 if __name__
== '__main__':
167 print # Stupid, to init toolboxes...