scrapsupport.py 2.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# 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).

# NOTE: the scrap include file is so bad that the bgen output has to be
# massaged by hand.

import string

# Declarations that change for each manager
MACHEADERFILE = 'Scrap.h'		# The Apple header file
13
MODNAME = '_Scrap'				# The name of the module
14
OBJECTNAME = 'Scrap'			# The basic name of the objects used here
15 16

# The following is *usually* unchanged but may still require tuning
17
MODPREFIX = 'Scrap'			# The prefix for module-wide routines
18 19
OBJECTTYPE = OBJECTNAME + 'Ref'	# The C type used to represent them
OBJECTPREFIX = MODPREFIX + 'Obj'	# The prefix for object methods
20 21 22 23 24 25
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 *

# Create the type objects
26
ScrapRef = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
27 28

includestuff = includestuff + """
29
#include <Carbon/Carbon.h>
30 31 32 33

/*
** Generate ScrapInfo records
*/
34 35
static PyObject *
SCRRec_New(itself)
36 37 38 39 40
	ScrapStuff *itself;
{

	return Py_BuildValue("lO&hhO&", itself->scrapSize,
		ResObj_New, itself->scrapHandle, itself->scrapCount, itself->scrapState,
41
		PyMac_BuildStr255, itself->scrapName);
42 43 44
}
"""

45 46
ScrapStuffPtr = OpaqueByValueType('ScrapStuffPtr', 'SCRRec')
ScrapFlavorType = OSTypeType('ScrapFlavorType')
47 48
ScrapFlavorFlags = Type('ScrapFlavorFlags', 'l')
#ScrapFlavorInfo = OpaqueType('ScrapFlavorInfo', 'ScrapFlavorInfo')
49 50
putscrapbuffer = FixedInputBufferType('void *')

51
class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
52 53
	pass

54 55
# Create the generator groups and link them
module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
56 57
object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
module.addobject(object)
58 59 60

# Create the generator classes used to populate the lists
Function = OSErrFunctionGenerator
61
Method = OSErrMethodGenerator
62 63 64

# Create and populate the lists
functions = []
65
methods = []
66 67 68 69 70
execfile(INPUTFILE)

# 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)
71
for f in methods: object.add(f)
72 73 74 75 76

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