summaryrefslogtreecommitdiffstats
path: root/Mac/Lib/macfsn.py
blob: c78459d3767a794cb592bf72c958425edf24acce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""StandardFile compatability module: implement macfs StandardFile
API calls with Navigation Services"""
import macfs
import struct
import Res
try:
	import Nav
except ImportError:
	Nav = None

_curfolder = None
_movablemodal = 1

def _mktypelist(typelist):
	if not typelist:
		return None
	data = 'Pyth' + struct.pack("hh", 0, len(typelist))
	for type in typelist:
		data = data+type
	return Res.Resource(data)
	
def _StandardGetFile(*typelist):
	return apply(_PromptGetFile, (None,)+typelist)
	
def _PromptGetFile(prompt, *typelist):
	args = {}
	flags = 0x56
	typehandle = _mktypelist(typelist)
	if typehandle:
		args['typeList'] = typehandle
	else:
		flags = flags | 0x01
	if prompt:
		args['message'] = prompt
	args['preferenceKey'] = 'PyMC'
	if _movablemodal:
		args['eventProc'] = None
	args['dialogOptionFlags'] = flags
	_handleSetFolder(args)
	try:
		rr = Nav.NavChooseFile(args)
		good = 1
	except Nav.error, arg:
		if arg[0] != -128: # userCancelledErr
			raise Nav.error, arg
		good = 0
		fss = macfs.FSSpec(':cancelled')
	else:
		fss = rr.selection[0]
##	if typehandle:
##		typehandle.DisposeHandle()
	return fss, good

def _StandardPutFile(prompt, default=None):
	args = {}
	flags = 0x07
	if prompt:
		args['message'] = prompt
	args['preferenceKey'] = 'PyMC'
	if _movablemodal:
		args['eventProc'] = None
	if default:
		args['savedFileName'] = default
	args['dialogOptionFlags'] = flags
	_handleSetFolder(args)
	try:
		rr = Nav.NavPutFile(args)
		good = 1
	except Nav.error, arg:
		if arg[0] != -128: # userCancelledErr
			raise Nav.error, arg
		good = 0
		fss = macfs.FSSpec(':cancelled')
	else:
		fss = rr.selection[0]
	return fss, good
	
def _SetFolder(folder):
	global _curfolder
	if _curfolder:
		rv = _curfolder
	else:
		rv = None
	_curfolder = macfs.FSSpec(folder)
	return rv
	
def _handleSetFolder(args):
	global _curfolder
	if not _curfolder:
		return
	import aepack
	fss = macfs.FSSpec(_curfolder)
	aedesc = aepack.pack(fss)
	args['defaultLocation'] = aedesc
	_curfolder = None
	
def _GetDirectory(prompt=None):
	args = {}
	flags = 0x17
	if prompt:
		args['message'] = prompt
	args['preferenceKey'] = 'PyMC'
	if _movablemodal:
		args['eventProc'] = None
	args['dialogOptionFlags'] = flags
	_handleSetFolder(args)
	try:
		rr = Nav.NavChooseFolder(args)
		good = 1
	except Nav.error, arg:
		if arg[0] != -128: # userCancelledErr
			raise Nav.error, arg
		good = 0
		fss = macfs.FSSpec(':cancelled')
	else:
		fss = rr.selection[0]
	return fss, good
	
def _install():
	macfs.StandardGetFile = StandardGetFile
	macfs.PromptGetFile = PromptGetFile
	macfs.StandardPutFile = StandardPutFile
	macfs.SetFolder = SetFolder
	macfs.GetDirectory = GetDirectory
	
if Nav and Nav.NavServicesAvailable():
	StandardGetFile = _StandardGetFile
	PromptGetFile = _PromptGetFile
	StandardPutFile = _StandardPutFile
	SetFolder = _SetFolder
	GetDirectory = _GetDirectory
else:
	from macfs import StandardGetFile, PromptGetFile, StandardPutFile, SetFolder, GetDirectory
	

if __name__ == '__main__':
	print 'Testing StandardGetFile'
	fss, ok = StandardGetFile()
	print '->', fss, ok
	print 'Testing StandardGetFile("TEXT")'
	fss, ok = StandardGetFile("TEXT")
	print '->', fss, ok
	print 'Testing PromptGetFile'
	fss, ok = PromptGetFile("prompt")
	print '->', fss, ok
	print 'Testing StandardPutFile("the prompt", "default")'
	fss, ok = StandardPutFile("the prompt", "default")
	print '->', fss, ok
	print 'Testing GetDirectory("another prompt")'
	fss, ok = GetDirectory("Another prompt")
	print '->', fss, ok
	import sys
	sys.exit(1)