blob: 3c126465edefadee2556c1dd3d06cfbe7953c6fa [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
5import Res
6try:
7 import Nav
8except ImportError:
9 Nav = None
10
11_curfolder = None
12_movablemodal = 1
13
14def _mktypelist(typelist):
15 if not typelist:
16 return None
17 data = 'Pyth' + struct.pack("hh", 0, len(typelist))
18 for type in typelist:
19 data = data+type
Jack Jansene31d3d62000-04-07 09:08:37 +000020 return Res.Handle(data)
Jack Jansen54b9ce11999-12-18 16:57:33 +000021
22def _StandardGetFile(*typelist):
23 return apply(_PromptGetFile, (None,)+typelist)
24
25def _PromptGetFile(prompt, *typelist):
26 args = {}
27 flags = 0x56
28 typehandle = _mktypelist(typelist)
29 if typehandle:
30 args['typeList'] = typehandle
31 else:
32 flags = flags | 0x01
33 if prompt:
34 args['message'] = prompt
35 args['preferenceKey'] = 'PyMC'
36 if _movablemodal:
37 args['eventProc'] = None
Jack Jansen5c6634c2000-01-13 16:26:35 +000038 args['dialogOptionFlags'] = flags
39 _handleSetFolder(args)
Jack Jansen54b9ce11999-12-18 16:57:33 +000040 try:
41 rr = Nav.NavChooseFile(args)
42 good = 1
43 except Nav.error, arg:
Jack Jansen5c6634c2000-01-13 16:26:35 +000044 if arg[0] != -128: # userCancelledErr
45 raise Nav.error, arg
Jack Jansen54b9ce11999-12-18 16:57:33 +000046 good = 0
47 fss = macfs.FSSpec(':cancelled')
48 else:
49 fss = rr.selection[0]
50## if typehandle:
51## typehandle.DisposeHandle()
52 return fss, good
53
54def _StandardPutFile(prompt, default=None):
55 args = {}
Jack Jansen5c6634c2000-01-13 16:26:35 +000056 flags = 0x07
Jack Jansen54b9ce11999-12-18 16:57:33 +000057 if prompt:
58 args['message'] = prompt
59 args['preferenceKey'] = 'PyMC'
60 if _movablemodal:
61 args['eventProc'] = None
Jack Jansen5c6634c2000-01-13 16:26:35 +000062 if default:
63 args['savedFileName'] = default
64 args['dialogOptionFlags'] = flags
65 _handleSetFolder(args)
Jack Jansen54b9ce11999-12-18 16:57:33 +000066 try:
67 rr = Nav.NavPutFile(args)
68 good = 1
69 except Nav.error, arg:
Jack Jansen5c6634c2000-01-13 16:26:35 +000070 if arg[0] != -128: # userCancelledErr
71 raise Nav.error, arg
Jack Jansen54b9ce11999-12-18 16:57:33 +000072 good = 0
73 fss = macfs.FSSpec(':cancelled')
74 else:
75 fss = rr.selection[0]
76 return fss, good
77
78def _SetFolder(folder):
79 global _curfolder
80 if _curfolder:
81 rv = _curfolder
82 else:
Jack Jansen5c6634c2000-01-13 16:26:35 +000083 rv = None
Jack Jansen54b9ce11999-12-18 16:57:33 +000084 _curfolder = macfs.FSSpec(folder)
85 return rv
86
Jack Jansen5c6634c2000-01-13 16:26:35 +000087def _handleSetFolder(args):
88 global _curfolder
89 if not _curfolder:
90 return
91 import aepack
92 fss = macfs.FSSpec(_curfolder)
93 aedesc = aepack.pack(fss)
94 args['defaultLocation'] = aedesc
95 _curfolder = None
96
Jack Jansen54b9ce11999-12-18 16:57:33 +000097def _GetDirectory(prompt=None):
98 args = {}
Jack Jansen5c6634c2000-01-13 16:26:35 +000099 flags = 0x17
Jack Jansen54b9ce11999-12-18 16:57:33 +0000100 if prompt:
101 args['message'] = prompt
102 args['preferenceKey'] = 'PyMC'
103 if _movablemodal:
104 args['eventProc'] = None
Jack Jansen5c6634c2000-01-13 16:26:35 +0000105 args['dialogOptionFlags'] = flags
106 _handleSetFolder(args)
Jack Jansen54b9ce11999-12-18 16:57:33 +0000107 try:
108 rr = Nav.NavChooseFolder(args)
109 good = 1
110 except Nav.error, arg:
Jack Jansen5c6634c2000-01-13 16:26:35 +0000111 if arg[0] != -128: # userCancelledErr
112 raise Nav.error, arg
Jack Jansen54b9ce11999-12-18 16:57:33 +0000113 good = 0
114 fss = macfs.FSSpec(':cancelled')
115 else:
116 fss = rr.selection[0]
117 return fss, good
118
119def _install():
120 macfs.StandardGetFile = StandardGetFile
121 macfs.PromptGetFile = PromptGetFile
122 macfs.StandardPutFile = StandardPutFile
123 macfs.SetFolder = SetFolder
124 macfs.GetDirectory = GetDirectory
125
126if Nav and Nav.NavServicesAvailable():
127 StandardGetFile = _StandardGetFile
128 PromptGetFile = _PromptGetFile
129 StandardPutFile = _StandardPutFile
130 SetFolder = _SetFolder
131 GetDirectory = _GetDirectory
Jack Jansen1fdadcd2000-05-05 23:10:58 +0000132 _install()
Jack Jansen54b9ce11999-12-18 16:57:33 +0000133else:
134 from macfs import StandardGetFile, PromptGetFile, StandardPutFile, SetFolder, GetDirectory
135
136
137if __name__ == '__main__':
138 print 'Testing StandardGetFile'
139 fss, ok = StandardGetFile()
140 print '->', fss, ok
141 print 'Testing StandardGetFile("TEXT")'
142 fss, ok = StandardGetFile("TEXT")
143 print '->', fss, ok
144 print 'Testing PromptGetFile'
145 fss, ok = PromptGetFile("prompt")
146 print '->', fss, ok
147 print 'Testing StandardPutFile("the prompt", "default")'
148 fss, ok = StandardPutFile("the prompt", "default")
149 print '->', fss, ok
150 print 'Testing GetDirectory("another prompt")'
151 fss, ok = GetDirectory("Another prompt")
152 print '->', fss, ok
153 import sys
154 sys.exit(1)
155