2 # -*- Mode: Python; py-indent-offset: 4 -*-
6 module_init_template = \
7 '/* -*- Mode: C; c-basic-offset: 4 -*- */\n' + \
8 '#ifdef HAVE_CONFIG_H\n' + \
9 '# include "config.h"\n' + \
11 '#include <Python.h>\n' + \
12 '#include <pygtk.h>\n' + \
14 '/* include any extra headers needed here */\n' + \
16 'void %(prefix)s_register_classes(PyObject *d);\n' + \
17 'extern PyMethodDef %(prefix)s_functions[];\n' + \
19 'DL_EXPORT(void)\n' + \
20 'init%(module)s(void)\n' + \
22 ' PyObject *m, *d;\n' + \
24 ' /* perform any initialisation required by the library here */\n' + \
26 ' m = Py_InitModule("%(module)s", %(prefix)s_functions);\n' + \
27 ' d = PyModule_GetDict(m);\n' + \
29 ' init_pygtk();\n' + \
31 ' %(prefix)s_register_classes(d);\n' + \
33 ' /* add anything else to the module dictionary (such as constants) */\n' +\
35 ' if (PyErr_Occurred())\n' + \
36 ' Py_FatalError("could not initialise module %(module)s");\n' + \
40 '/* -*- Mode: C; c-basic-offset: 4 -*- */\n' + \
43 '/* include any required headers here */\n' + \
46 ' /* include any code here that needs to be executed before the\n' + \
47 ' * extension classes get initialised */\n' + \
50 '/* you should add appropriate ignore, ignore-glob and\n' + \
51 ' * override sections here */\n'
53 def open_with_backup(file):
54 if os.path.exists(file):
56 os.rename(file, file+'~')
58 # fail silently if we can't make a backup
60 return open(file, 'w')
62 def write_skels(fileprefix, prefix, module):
63 fp = open_with_backup(fileprefix+'module.c')
64 fp.write(module_init_template % { 'prefix': prefix, 'module': module })
66 fp = open_with_backup(fileprefix+'.override')
67 fp.write(override_template % { 'prefix': prefix, 'module': module })
70 if __name__ == '__main__':
71 opts, args = getopt.getopt(sys.argv[1:], 'f:p:m:h',
72 ['file-prefix=', 'prefix=', 'module=', 'help'])
77 if opt in ('-f', '--file-prefix'):
79 elif opt in ('-p', '--prefix'):
81 elif opt in ('-m', '--module'):
83 elif opt in ('-h', '--help'):
84 print 'usage: mkskel.py -f fileprefix -p prefix -m module'
86 if not fileprefix or not prefix or not module:
87 print 'usage: mkskel.py -f fileprefix -p prefix -m module'
89 write_skels(fileprefix, prefix, module)