/Mac/Modules/launch/launchsupport.py

http://unladen-swallow.googlecode.com/ · Python · 101 lines · 70 code · 12 blank · 19 comment · 1 complexity · ee60568d25db756e864db7abaefe458c 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. MODNAME = '_Launch' # The name of the module
  8. OBJECTNAME = 'UNUSED' # The basic name of the objects used here
  9. KIND = 'Record' # Usually 'Ptr' or 'Handle'
  10. # The following is *usually* unchanged but may still require tuning
  11. MODPREFIX = 'Launch' # The prefix for module-wide routines
  12. OBJECTTYPE = OBJECTNAME + KIND # The C type used to represent them
  13. OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
  14. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  15. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  16. from macsupport import *
  17. # Create the type objects
  18. LSAcceptanceFlags = Type("LSAcceptanceFlags", "l")
  19. LSInitializeFlags = Type("LSInitializeFlags", "l")
  20. LSRequestedInfo = Type("LSRequestedInfo", "l")
  21. LSRolesMask = Type("LSRolesMask", "l")
  22. UniCharCount = Type("UniCharCount", "l")
  23. OptCFStringRef = OpaqueByValueType("CFStringRef", "OptCFStringRefObj")
  24. LSItemInfoRecord = OpaqueType("LSItemInfoRecord", "LSItemInfoRecord")
  25. includestuff = includestuff + """
  26. #if PY_VERSION_HEX < 0x02040000
  27. PyObject *PyMac_GetOSErrException(void);
  28. #endif
  29. #include <ApplicationServices/ApplicationServices.h>
  30. /*
  31. ** Optional CFStringRef. None will pass NULL
  32. */
  33. static int
  34. OptCFStringRefObj_Convert(PyObject *v, CFStringRef *spec)
  35. {
  36. if (v == Py_None) {
  37. *spec = NULL;
  38. return 1;
  39. }
  40. return CFStringRefObj_Convert(v, spec);
  41. }
  42. PyObject *
  43. OptCFStringRefObj_New(CFStringRef it)
  44. {
  45. if (it == NULL) {
  46. Py_INCREF(Py_None);
  47. return Py_None;
  48. }
  49. return CFStringRefObj_New(it);
  50. }
  51. /*
  52. ** Convert LSItemInfoRecord to Python.
  53. */
  54. PyObject *
  55. LSItemInfoRecord_New(LSItemInfoRecord *it)
  56. {
  57. return Py_BuildValue("{s:is:O&s:O&s:O&s:O&s:i}",
  58. "flags", it->flags,
  59. "filetype", PyMac_BuildOSType, it->filetype,
  60. "creator", PyMac_BuildOSType, it->creator,
  61. "extension", OptCFStringRefObj_New, it->extension,
  62. "iconFileName", OptCFStringRefObj_New, it->iconFileName,
  63. "kindID", it->kindID);
  64. }
  65. """
  66. # From here on it's basically all boiler plate...
  67. execfile(string.lower(MODPREFIX) + 'typetest.py')
  68. # Create the generator groups and link them
  69. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  70. ##object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
  71. ##module.addobject(object)
  72. # Create the generator classes used to populate the lists
  73. Function = OSErrFunctionGenerator
  74. ##Method = OSErrMethodGenerator
  75. # Create and populate the lists
  76. functions = []
  77. ##methods = []
  78. execfile(INPUTFILE)
  79. # add the populated lists to the generator groups
  80. # (in a different wordl the scan program would generate this)
  81. for f in functions: module.add(f)
  82. ##for f in methods: object.add(f)
  83. # generate output (open the output file as late as possible)
  84. SetOutputFileName(OUTPUTFILE)
  85. module.generate()