blob: 8f3203ca900296129032222eec454307aa999d12 [file] [log] [blame]
Jack Jansen54b9ce11999-12-18 16:57:33 +00001"""StandardFile compatability module: implement macfs StandardFile
2API calls with Navigation Services"""
3import macfs
4import struct
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00005from Carbon import Res
Jack Jansen54b9ce11999-12-18 16:57:33 +00006try:
7 import Nav
8except ImportError:
9 Nav = None
10
11_curfolder = None
12_movablemodal = 1
13
14def _mktypelist(typelist):
Jack Jansen32f782c2002-03-29 21:19:37 +000015 # Workaround for OSX typeless files:
16 if 'TEXT' in typelist and not '\0\0\0\0' in typelist:
17 typelist = typelist + ('\0\0\0\0',)
Jack Jansen54b9ce11999-12-18 16:57:33 +000018 if not typelist:
19 return None
20 data = 'Pyth' + struct.pack("hh", 0, len(typelist))
21 for type in typelist:
22 data = data+type
Jack Jansene31d3d62000-04-07 09:08:37 +000023 return Res.Handle(data)
Jack Jansen54b9ce11999-12-18 16:57:33 +000024
25def _StandardGetFile(*typelist):
26 return apply(_PromptGetFile, (None,)+typelist)
27
28def _PromptGetFile(prompt, *typelist):
29 args = {}
30 flags = 0x56
31 typehandle = _mktypelist(typelist)
32 if typehandle:
33 args['typeList'] = typehandle
34 else:
35 flags = flags | 0x01
36 if prompt:
37 args['message'] = prompt
38 args['preferenceKey'] = 'PyMC'
39 if _movablemodal:
40 args['eventProc'] = None
Jack Jansen5c6634c2000-01-13 16:26:35 +000041 args['dialogOptionFlags'] = flags
42 _handleSetFolder(args)
Jack Jansen54b9ce11999-12-18 16:57:33 +000043 try:
44 rr = Nav.NavChooseFile(args)
45 good = 1
46 except Nav.error, arg:
Jack Jansen5c6634c2000-01-13 16:26:35 +000047 if arg[0] != -128: # userCancelledErr
48 raise Nav.error, arg
Jack Jansen54b9ce11999-12-18 16:57:33 +000049 good = 0
Jack Jansen32f782c2002-03-29 21:19:37 +000050 fss = None
Jack Jansen54b9ce11999-12-18 16:57:33 +000051 else:
Just van Rossum67050d22001-10-31 22:58:23 +000052 if rr.selection:
53 fss = rr.selection[0]
54 else:
55 fss = None
56 good = 0
Jack Jansen54b9ce11999-12-18 16:57:33 +000057## if typehandle:
58## typehandle.DisposeHandle()
59 return fss, good
60
61def _StandardPutFile(prompt, default=None):
62 args = {}
Jack Jansen5c6634c2000-01-13 16:26:35 +000063 flags = 0x07
Jack Jansen54b9ce11999-12-18 16:57:33 +000064 if prompt:
65 args['message'] = prompt
66 args['preferenceKey'] = 'PyMC'
67 if _movablemodal:
68 args['eventProc'] = None
Jack Jansen5c6634c2000-01-13 16:26:35 +000069 if default:
70 args['savedFileName'] = default
71 args['dialogOptionFlags'] = flags
72 _handleSetFolder(args)
Jack Jansen54b9ce11999-12-18 16:57:33 +000073 try:
74 rr = Nav.NavPutFile(args)
75 good = 1
76 except Nav.error, arg:
Jack Jansen5c6634c2000-01-13 16:26:35 +000077 if arg[0] != -128: # userCancelledErr
78 raise Nav.error, arg
Jack Jansen54b9ce11999-12-18 16:57:33 +000079 good = 0
Jack Jansen32f782c2002-03-29 21:19:37 +000080 fss = None
Jack Jansen54b9ce11999-12-18 16:57:33 +000081 else:
82 fss = rr.selection[0]
83 return fss, good
84
85def _SetFolder(folder):
86 global _curfolder
87 if _curfolder:
88 rv = _curfolder
89 else:
Jack Jansen5c6634c2000-01-13 16:26:35 +000090 rv = None
Jack Jansen54b9ce11999-12-18 16:57:33 +000091 _curfolder = macfs.FSSpec(folder)
92 return rv
93
Jack Jansen5c6634c2000-01-13 16:26:35 +000094def _handleSetFolder(args):
95 global _curfolder
96 if not _curfolder:
97 return
98 import aepack
99 fss = macfs.FSSpec(_curfolder)
100 aedesc = aepack.pack(fss)
101 args['defaultLocation'] = aedesc
102 _curfolder = None
103
Jack Jansen54b9ce11999-12-18 16:57:33 +0000104def _GetDirectory(prompt=None):
105 args = {}
Jack Jansen5c6634c2000-01-13 16:26:35 +0000106 flags = 0x17
Jack Jansen54b9ce11999-12-18 16:57:33 +0000107 if prompt:
108 args['message'] = prompt
109 args['preferenceKey'] = 'PyMC'
110 if _movablemodal:
111 args['eventProc'] = None
Jack Jansen5c6634c2000-01-13 16:26:35 +0000112 args['dialogOptionFlags'] = flags
113 _handleSetFolder(args)
Jack Jansen54b9ce11999-12-18 16:57:33 +0000114 try:
115 rr = Nav.NavChooseFolder(args)
116 good = 1
117 except Nav.error, arg:
Jack Jansen5c6634c2000-01-13 16:26:35 +0000118 if arg[0] != -128: # userCancelledErr
119 raise Nav.error, arg
Jack Jansen54b9ce11999-12-18 16:57:33 +0000120 good = 0
Jack Jansen32f782c2002-03-29 21:19:37 +0000121 fss = None
Jack Jansen54b9ce11999-12-18 16:57:33 +0000122 else:
123 fss = rr.selection[0]
124 return fss, good
125
126def _install():
127 macfs.StandardGetFile = StandardGetFile
128 macfs.PromptGetFile = PromptGetFile
129 macfs.StandardPutFile = StandardPutFile
130 macfs.SetFolder = SetFolder
131 macfs.GetDirectory = GetDirectory
132
133if Nav and Nav.NavServicesAvailable():
134 StandardGetFile = _StandardGetFile
135 PromptGetFile = _PromptGetFile
136 StandardPutFile = _StandardPutFile
137 SetFolder = _SetFolder
138 GetDirectory = _GetDirectory
Jack Jansen1fdadcd2000-05-05 23:10:58 +0000139 _install()
Jack Jansen54b9ce11999-12-18 16:57:33 +0000140else:
141 from macfs import StandardGetFile, PromptGetFile, StandardPutFile, SetFolder, GetDirectory
142
143
144if __name__ == '__main__':
145 print 'Testing StandardGetFile'
146 fss, ok = StandardGetFile()
147 print '->', fss, ok
148 print 'Testing StandardGetFile("TEXT")'
149 fss, ok = StandardGetFile("TEXT")
150 print '->', fss, ok
151 print 'Testing PromptGetFile'
152 fss, ok = PromptGetFile("prompt")
153 print '->', fss, ok
154 print 'Testing StandardPutFile("the prompt", "default")'
155 fss, ok = StandardPutFile("the prompt", "default")
156 print '->', fss, ok
157 print 'Testing GetDirectory("another prompt")'
158 fss, ok = GetDirectory("Another prompt")
159 print '->', fss, ok
160 import sys
161 sys.exit(1)
162