blob: e7eeacd565ccc7fe26b5e3917282a47cfea26f41 [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):
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:
Just van Rossum67050d22001-10-31 22:58:23 +000049 if rr.selection:
50 fss = rr.selection[0]
51 else:
52 fss = None
53 good = 0
Jack Jansen54b9ce11999-12-18 16:57:33 +000054## if typehandle:
55## typehandle.DisposeHandle()
56 return fss, good
57
58def _StandardPutFile(prompt, default=None):
59 args = {}
Jack Jansen5c6634c2000-01-13 16:26:35 +000060 flags = 0x07
Jack Jansen54b9ce11999-12-18 16:57:33 +000061 if prompt:
62 args['message'] = prompt
63 args['preferenceKey'] = 'PyMC'
64 if _movablemodal:
65 args['eventProc'] = None
Jack Jansen5c6634c2000-01-13 16:26:35 +000066 if default:
67 args['savedFileName'] = default
68 args['dialogOptionFlags'] = flags
69 _handleSetFolder(args)
Jack Jansen54b9ce11999-12-18 16:57:33 +000070 try:
71 rr = Nav.NavPutFile(args)
72 good = 1
73 except Nav.error, arg:
Jack Jansen5c6634c2000-01-13 16:26:35 +000074 if arg[0] != -128: # userCancelledErr
75 raise Nav.error, arg
Jack Jansen54b9ce11999-12-18 16:57:33 +000076 good = 0
77 fss = macfs.FSSpec(':cancelled')
78 else:
79 fss = rr.selection[0]
80 return fss, good
81
82def _SetFolder(folder):
83 global _curfolder
84 if _curfolder:
85 rv = _curfolder
86 else:
Jack Jansen5c6634c2000-01-13 16:26:35 +000087 rv = None
Jack Jansen54b9ce11999-12-18 16:57:33 +000088 _curfolder = macfs.FSSpec(folder)
89 return rv
90
Jack Jansen5c6634c2000-01-13 16:26:35 +000091def _handleSetFolder(args):
92 global _curfolder
93 if not _curfolder:
94 return
95 import aepack
96 fss = macfs.FSSpec(_curfolder)
97 aedesc = aepack.pack(fss)
98 args['defaultLocation'] = aedesc
99 _curfolder = None
100
Jack Jansen54b9ce11999-12-18 16:57:33 +0000101def _GetDirectory(prompt=None):
102 args = {}
Jack Jansen5c6634c2000-01-13 16:26:35 +0000103 flags = 0x17
Jack Jansen54b9ce11999-12-18 16:57:33 +0000104 if prompt:
105 args['message'] = prompt
106 args['preferenceKey'] = 'PyMC'
107 if _movablemodal:
108 args['eventProc'] = None
Jack Jansen5c6634c2000-01-13 16:26:35 +0000109 args['dialogOptionFlags'] = flags
110 _handleSetFolder(args)
Jack Jansen54b9ce11999-12-18 16:57:33 +0000111 try:
112 rr = Nav.NavChooseFolder(args)
113 good = 1
114 except Nav.error, arg:
Jack Jansen5c6634c2000-01-13 16:26:35 +0000115 if arg[0] != -128: # userCancelledErr
116 raise Nav.error, arg
Jack Jansen54b9ce11999-12-18 16:57:33 +0000117 good = 0
118 fss = macfs.FSSpec(':cancelled')
119 else:
120 fss = rr.selection[0]
121 return fss, good
122
123def _install():
124 macfs.StandardGetFile = StandardGetFile
125 macfs.PromptGetFile = PromptGetFile
126 macfs.StandardPutFile = StandardPutFile
127 macfs.SetFolder = SetFolder
128 macfs.GetDirectory = GetDirectory
129
130if Nav and Nav.NavServicesAvailable():
131 StandardGetFile = _StandardGetFile
132 PromptGetFile = _PromptGetFile
133 StandardPutFile = _StandardPutFile
134 SetFolder = _SetFolder
135 GetDirectory = _GetDirectory
Jack Jansen1fdadcd2000-05-05 23:10:58 +0000136 _install()
Jack Jansen54b9ce11999-12-18 16:57:33 +0000137else:
138 from macfs import StandardGetFile, PromptGetFile, StandardPutFile, SetFolder, GetDirectory
139
140
141if __name__ == '__main__':
142 print 'Testing StandardGetFile'
143 fss, ok = StandardGetFile()
144 print '->', fss, ok
145 print 'Testing StandardGetFile("TEXT")'
146 fss, ok = StandardGetFile("TEXT")
147 print '->', fss, ok
148 print 'Testing PromptGetFile'
149 fss, ok = PromptGetFile("prompt")
150 print '->', fss, ok
151 print 'Testing StandardPutFile("the prompt", "default")'
152 fss, ok = StandardPutFile("the prompt", "default")
153 print '->', fss, ok
154 print 'Testing GetDirectory("another prompt")'
155 fss, ok = GetDirectory("Another prompt")
156 print '->', fss, ok
157 import sys
158 sys.exit(1)
159