summaryrefslogtreecommitdiffstats
path: root/Mac/Modules/file/filesupport.py
blob: c431d42b1c136529bdd2c9f46b0db9de03b1ef67 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# This script generates a Python interface for an Apple Macintosh Manager.
# It uses the "bgen" package to generate C code.
# The function specifications are generated by scanning the mamager's header file,
# using the "scantools" package (customized for this particular manager).

import string

# Declarations that change for each manager
#MACHEADERFILE = 'Files.h'		# The Apple header file
MODNAME = '_File'				# The name of the module

# The following is *usually* unchanged but may still require tuning
MODPREFIX = 'File'			# The prefix for module-wide routines
INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
OUTPUTFILE = MODNAME + "module.c"	# The file generated by this program

from macsupport import *

# Various integers:
SInt64 = Type("SInt64", "L")
UInt64 = Type("UInt64", "L")
FNMessage = Type("FNMessage", "l")
FSAllocationFlags = Type("FSAllocationFlags", "H")
FSCatalogInfoBitmap = Type("FSCatalogInfoBitmap", "l")
FSIteratorFlags = Type("FSIteratorFlags", "l")
FSVolumeRefNum = Type("FSVolumeRefNum", "h")
AliasInfoType = Type("AliasInfoType", "h")

# Various types of strings:
#class UniCharCountBuffer(InputOnlyType):
#	pass
class VarReverseInputBufferType(ReverseInputBufferMixin, VarInputBufferType):
	pass
FullPathName = VarReverseInputBufferType()
ConstStr31Param = OpaqueArrayType("Str31", "PyMac_BuildStr255", "PyMac_GetStr255")
ConstStr32Param = OpaqueArrayType("Str32", "PyMac_BuildStr255", "PyMac_GetStr255")
ConstStr63Param = OpaqueArrayType("Str63", "PyMac_BuildStr255", "PyMac_GetStr255")
Str63 = OpaqueArrayType("Str63", "PyMac_BuildStr255", "PyMac_GetStr255")

HFSUniStr255 = OpaqueType("HFSUniStr255", "PyMac_BuildHFSUniStr255", "PyMac_GetHFSUniStr255")
UInt8_ptr = InputOnlyType("UInt8 *", "s")

# Other types:
FInfo = OpaqueByValueStructType("FInfo", "PyMac_BuildFInfo", "PyMac_GetFInfo")
FInfo_ptr = OpaqueType("FInfo", "PyMac_BuildFInfo", "PyMac_GetFInfo")
AliasHandle = OpaqueByValueType("AliasHandle", "Alias")
FSSpec = OpaqueType("FSSpec", "FSSpec")
FSSpec_ptr = OpaqueType("FSSpec", "FSSpec")
FSRef = OpaqueType("FSRef", "FSRef")
FSRef_ptr = OpaqueType("FSRef", "FSRef")

# To be done:
#CatPositionRec
#FSCatalogInfo
#FSForkInfo
#FSIterator
#FSVolumeInfo
#FSSpecArrayPtr

includestuff = includestuff + """
#ifdef WITHOUT_FRAMEWORKS
#include <Files.h>
#else
#include <Carbon/Carbon.h>
#endif

/* Forward declarations */
extern PyObject *FSRef_New(FSRef *itself);
extern PyObject *FSSpec_New(FSSpec *itself);
extern PyObject *Alias_New(AliasHandle itself);
extern int FSRef_Convert(PyObject *v, FSRef *p_itself);
extern int FSSpec_Convert(PyObject *v, FSSpec *p_itself);
extern int Alias_Convert(PyObject *v, AliasHandle *p_itself);
static int myPyMac_GetFSSpec(PyObject *v, FSSpec *spec);
static int myPyMac_GetFSRef(PyObject *v, FSRef *fsr);

/*
** Parse/generate objsect
*/
static PyObject *
PyMac_BuildHFSUniStr255(HFSUniStr255 *itself)
{

	return Py_BuildValue("u#", itself->unicode, itself->length);
}

/*
** Parse/generate objsect
*/
static PyObject *
PyMac_BuildFInfo(FInfo *itself)
{

	return Py_BuildValue("O&O&HO&h",
		PyMac_BuildOSType, itself->fdType,
		PyMac_BuildOSType, itself->fdCreator,
		itself->fdFlags,
		PyMac_BuildPoint, &itself->fdLocation,
		itself->fdFldr);
}

static int
PyMac_GetFInfo(PyObject *v, FInfo *itself)
{
	return PyArg_ParseTuple(v, "O&O&HO&h",
		PyMac_GetOSType, &itself->fdType,
		PyMac_GetOSType, &itself->fdCreator,
		&itself->fdFlags,
		PyMac_GetPoint, &itself->fdLocation,
		&itself->fdFldr);
}
"""

finalstuff = finalstuff + """
static int
myPyMac_GetFSSpec(PyObject *v, FSSpec *spec)
{
	Str255 path;
	short refnum;
	long parid;
	OSErr err;
	FSRef fsr;

	if (FSSpec_Check(v)) {
		*spec = ((FSSpecObject *)v)->ob_itself;
		return 1;
	}

	if (PyArg_Parse(v, "(hlO&)",
						&refnum, &parid, PyMac_GetStr255, &path)) {
		err = FSMakeFSSpec(refnum, parid, path, spec);
		if ( err && err != fnfErr ) {
			PyMac_Error(err);
			return 0;
		}
		return 1;
	}
	PyErr_Clear();
#if !TARGET_API_MAC_OSX
	/* On OS9 we now try a pathname */
	if ( PyString_Check(v) ) {
		/* It's a pathname */
		if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
			return 0;
		refnum = 0; /* XXXX Should get CurWD here?? */
		parid = 0;
		err = FSMakeFSSpec(refnum, parid, path, spec);
		if ( err && err != fnfErr ) {
			PyMac_Error(err);
			return 0;
		}
		return 1;
	}
	PyErr_Clear();
#endif
	/* Otherwise we try to go via an FSRef. On OSX we go all the way,
	** on OS9 we accept only a real FSRef object
	*/
#if TARGET_API_MAX_OSX
	if ( myPyMac_GetFSRef(v, &fsr) >= 0 ) {
#else
	if ( PyArg_Parse(v, "O&", FSRef_Convert, &fsr) ) {
#endif	
		err = FSGetCatalogInfo(&fsr, kFSCatInfoNone, NULL, NULL, spec, NULL);
		if (err != noErr) {
			PyMac_Error(err);
			return 0;
		}
		return 1;
	}
	PyErr_SetString(PyExc_TypeError, "FSSpec, FSRef, pathname or (refnum, parid, path) required");
	return 0;
}

static int
myPyMac_GetFSRef(PyObject *v, FSRef *fsr)
{
	if (FSRef_Check(v)) {
		*fsr = ((FSRefObject *)v)->ob_itself;
		return 1;
	}

#if !TARGET_API_MAC_OSX
	/* On OSX we now try a pathname */
	if ( PyString_Check(args) ) {
		OSStatus err;
		if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) {
			PyErr_Mac(ErrorObject, err);
			return 0;
		}
		return 1;
	}
	/* XXXX Should try unicode here too */
#endif
	/* Otherwise we try to go via an FSSpec */
	if (FSSpec_Check(v)) {
		if (FSpMakeFSRef(&((FSSpecObject *)v)->ob_itself, fsr))
			return 1;
		return 0;
	}
	PyErr_SetString(PyExc_TypeError, "FSRef, FSSpec or pathname required");
	return 0;
}
"""

execfile(string.lower(MODPREFIX) + 'typetest.py')

# Our object types:
class FSSpecDefinition(PEP253Mixin, GlobalObjectDefinition):
	def __init__(self, name, prefix, itselftype):
		GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
		self.argref = "*"	# Store FSSpecs, but pass them by address

	def outputCheckNewArg(self):
		Output("if (itself == NULL) return PyMac_Error(resNotFound);")

	def output_tp_newBody(self):
		Output("PyObject *self;");
		Output()
		Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
		Output("memset(&((%s *)self)->ob_itself, 0, sizeof(%s));", 
			self.objecttype, self.objecttype)
		Output("return self;")

	def output_tp_initBody(self):
		Output("PyObject *v;")
		Output("char *kw[] = {\"itself\", 0};")
		Output()
		Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"O\", kw, &v))")
		Output("return -1;")
		Output("if (myPyMac_GetFSSpec(v, &((%s *)self)->ob_itself)) return 0;", self.objecttype)
		Output("return -1;")
	
class FSRefDefinition(PEP253Mixin, GlobalObjectDefinition):
	def __init__(self, name, prefix, itselftype):
		GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
		self.argref = "*"	# Store FSRefs, but pass them by address

	def outputCheckNewArg(self):
		Output("if (itself == NULL) return PyMac_Error(resNotFound);")

	def output_tp_newBody(self):
		Output("PyObject *self;");
		Output()
		Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
		Output("memset(&((%s *)self)->ob_itself, 0, sizeof(%s));", 
			self.objecttype, self.objecttype)
		Output("return self;")
	
	def output_tp_initBody(self):
		Output("PyObject *v;")
		Output("char *kw[] = {\"itself\", 0};")
		Output()
		Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"O\", kw, &v))")
		Output("return -1;")
		Output("if (myPyMac_GetFSRef(v, &((%s *)self)->ob_itself)) return 0;", self.objecttype)
		Output("return -1;")
	
class AliasDefinition(PEP253Mixin, GlobalObjectDefinition):
	# XXXX Should inherit from resource?

	def outputCheckNewArg(self):
		Output("if (itself == NULL) return PyMac_Error(resNotFound);")
		
	def outputStructMembers(self):
		GlobalObjectDefinition.outputStructMembers(self)
		Output("void (*ob_freeit)(%s ptr);", self.itselftype)
		
	def outputInitStructMembers(self):
		GlobalObjectDefinition.outputInitStructMembers(self)
		Output("it->ob_freeit = NULL;")
		
	def outputCleanupStructMembers(self):
		Output("if (self->ob_freeit && self->ob_itself)")
		OutLbrace()
		Output("self->ob_freeit(self->ob_itself);")
		OutRbrace()
		Output("self->ob_itself = NULL;")

	def output_tp_newBody(self):
		Output("PyObject *self;");
		Output()
		Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
		Output("((%s *)self)->ob_itself = NULL;", self.objecttype)
		Output("return self;")
	
	def output_tp_initBody(self):
		Output("%s itself;", self.itselftype);
		Output("char *kw[] = {\"itself\", 0};")
		Output()
		Output("if (PyArg_ParseTupleAndKeywords(args, kwds, \"O&\", kw, %s_Convert, &itself))",
			self.prefix)
		OutLbrace()
		Output("((%s *)self)->ob_itself = itself;", self.objecttype)
		Output("return 0;")
		OutRbrace()
		Output("return -1;")
	
# Alias methods come in two flavors: those with the alias as arg1 and
# those with the alias as arg 2.
class Arg2MethodGenerator(MethodGenerator):
	"""Similar to MethodGenerator, but has self as second argument"""

	def parseArgumentList(self, args):
		args0, arg1, argsrest = args[:1], args[1], args[2:]
		t0, n0, m0 = arg1
		args = args0 + argsrest
		if m0 != InMode:
			raise ValueError, "method's 'self' must be 'InMode'"
		self.itself = Variable(t0, "_self->ob_itself", SelfMode)
		FunctionGenerator.parseArgumentList(self, args)
		self.argumentList.insert(2, self.itself)

# From here on it's basically all boiler plate...

# Create the generator groups and link them
module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)

aliasobject = AliasDefinition('Alias', 'Alias', 'AliasHandle')
fsspecobject = FSSpecDefinition('FSSpec', 'FSSpec', 'FSSpec')
fsrefobject = FSRefDefinition('FSRef', 'FSRef', 'FSRef')

module.addobject(aliasobject)
module.addobject(fsspecobject)
module.addobject(fsrefobject)

# Create the generator classes used to populate the lists
Function = OSErrFunctionGenerator
Method = OSErrMethodGenerator

# Create and populate the lists
functions = []
alias_methods = []
fsref_methods = []
fsspec_methods = []
execfile(INPUTFILE)

# Manual generators:
FSRefMakePath_body = """
OSStatus _err;
#define MAXPATHNAME 1024
UInt8 path[MAXPATHNAME];
UInt32 maxPathSize = MAXPATHNAME;

_err = FSRefMakePath(&_self->ob_itself,
					 path,
					 maxPathSize);
if (_err != noErr) return PyMac_Error(_err);
_res = Py_BuildValue("s", path);
return _res;
"""
f = ManualGenerator("FSRefMakePath", FSRefMakePath_body)
f.docstring = lambda: "() -> string"
fsref_methods.append(f)


# add the populated lists to the generator groups
# (in a different wordl the scan program would generate this)
for f in functions: module.add(f)
for f in alias_methods: aliasobject.add(f)
for f in fsspec_methods: fsspecobject.add(f)
for f in fsref_methods: fsrefobject.add(f)

# generate output (open the output file as late as possible)
SetOutputFileName(OUTPUTFILE)
module.generate()