/Mac/Modules/icn/icnsupport.py

http://unladen-swallow.googlecode.com/ · Python · 90 lines · 46 code · 16 blank · 28 comment · 1 complexity · a4919559a615d4f5dc540b425bcf6224 MD5 · raw file

  1. # This script generates a Python interface for an Apple Macintosh Manager.
  2. # It uses the "bgen" package to generate C code.
  3. # The function specifications are generated by scanning the mamager's header file,
  4. # using the "scantools" package (customized for this particular manager).
  5. import string
  6. # Declarations that change for each manager
  7. MACHEADERFILE = 'Icons.h' # The Apple header file
  8. MODNAME = '_Icn' # The name of the module
  9. OBJECTNAME = 'Icon' # The basic name of the objects used here
  10. KIND = 'Handle' # Usually 'Ptr' or 'Handle'
  11. # The following is *usually* unchanged but may still require tuning
  12. MODPREFIX = 'Icn' # The prefix for module-wide routines
  13. OBJECTTYPE = OBJECTNAME + KIND # The C type used to represent them
  14. OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
  15. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  16. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  17. from macsupport import *
  18. # Create the type objects
  19. CIconHandle = OpaqueByValueType("CIconHandle", "ResObj")
  20. IconSuiteRef = OpaqueByValueType("IconSuiteRef", "ResObj")
  21. IconCacheRef = OpaqueByValueType("IconCacheRef", "ResObj")
  22. IconRef = OpaqueByValueType("IconRef", "ResObj")
  23. IconFamilyHandle = OpaqueByValueType("IconFamilyHandle", "ResObj")
  24. RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
  25. IconAlignmentType = Type("IconAlignmentType", "h")
  26. IconTransformType = Type("IconTransformType", "h")
  27. IconSelectorValue = Type("IconSelectorValue", "l")
  28. IconServicesUsageFlags = Type("IconServicesUsageFlags", "l")
  29. RGBColor = OpaqueType("RGBColor", "QdRGB")
  30. CGContextRef = OpaqueByValueType("CGContextRef", "CGContextRefObj")
  31. #WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
  32. # RgnHandle = FakeType("(RgnHandle)0")
  33. # XXXX Should be next, but this will break a lot of code...
  34. # RgnHandle = OpaqueByValueType("RgnHandle", "OptResObj")
  35. # KeyMap = ArrayOutputBufferType("KeyMap")
  36. #MacOSEventKind = Type("MacOSEventKind", "h") # Old-style
  37. #MacOSEventMask = Type("MacOSEventMask", "h") # Old-style
  38. #EventMask = Type("EventMask", "H")
  39. #EventKind = Type("EventKind", "H")
  40. includestuff = includestuff + """
  41. #include <Carbon/Carbon.h>
  42. """
  43. class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  44. def outputCheckNewArg(self):
  45. Output("if (itself == NULL) return PyMac_Error(resNotFound);")
  46. def outputCheckConvertArg(self):
  47. OutLbrace("if (DlgObj_Check(v))")
  48. Output("*p_itself = ((WindowObject *)v)->ob_itself;")
  49. Output("return 1;")
  50. OutRbrace()
  51. Out("""
  52. if (v == Py_None) { *p_itself = NULL; return 1; }
  53. if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
  54. """)
  55. # From here on it's basically all boiler plate...
  56. # Create the generator groups and link them
  57. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  58. ##object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
  59. ##module.addobject(object)
  60. # Create the generator classes used to populate the lists
  61. Function = OSErrWeakLinkFunctionGenerator
  62. ##Method = OSErrMethodGenerator
  63. # Create and populate the lists
  64. functions = []
  65. ##methods = []
  66. execfile(INPUTFILE)
  67. # add the populated lists to the generator groups
  68. # (in a different wordl the scan program would generate this)
  69. for f in functions: module.add(f)
  70. ##for f in methods: object.add(f)
  71. # generate output (open the output file as late as possible)
  72. SetOutputFileName(OUTPUTFILE)
  73. module.generate()