| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- Mode: Python; py-indent-offset: 4 -*- |
|---|
| 3 | |
|---|
| 4 | import sys, os, getopt |
|---|
| 5 | |
|---|
| 6 | module_init_template = \ |
|---|
| 7 | '/* -*- Mode: C; c-basic-offset: 4 -*- */\n' + \ |
|---|
| 8 | '#ifdef HAVE_CONFIG_H\n' + \ |
|---|
| 9 | '# include "config.h"\n' + \ |
|---|
| 10 | '#endif\n' + \ |
|---|
| 11 | '#include <Python.h>\n' + \ |
|---|
| 12 | '#include <pygtk.h>\n' + \ |
|---|
| 13 | '\n' + \ |
|---|
| 14 | '/* include any extra headers needed here */\n' + \ |
|---|
| 15 | '\n' + \ |
|---|
| 16 | 'void %(prefix)s_register_classes(PyObject *d);\n' + \ |
|---|
| 17 | 'extern PyMethodDef %(prefix)s_functions[];\n' + \ |
|---|
| 18 | '\n' + \ |
|---|
| 19 | 'DL_EXPORT(void)\n' + \ |
|---|
| 20 | 'init%(module)s(void)\n' + \ |
|---|
| 21 | '{\n' + \ |
|---|
| 22 | ' PyObject *m, *d;\n' + \ |
|---|
| 23 | '\n' + \ |
|---|
| 24 | ' /* perform any initialisation required by the library here */\n' + \ |
|---|
| 25 | '\n' + \ |
|---|
| 26 | ' m = Py_InitModule("%(module)s", %(prefix)s_functions);\n' + \ |
|---|
| 27 | ' d = PyModule_GetDict(m);\n' + \ |
|---|
| 28 | '\n' + \ |
|---|
| 29 | ' init_pygtk();\n' + \ |
|---|
| 30 | '\n' + \ |
|---|
| 31 | ' %(prefix)s_register_classes(d);\n' + \ |
|---|
| 32 | '\n' + \ |
|---|
| 33 | ' /* add anything else to the module dictionary (such as constants) */\n' +\ |
|---|
| 34 | '\n' + \ |
|---|
| 35 | ' if (PyErr_Occurred())\n' + \ |
|---|
| 36 | ' Py_FatalError("could not initialise module %(module)s");\n' + \ |
|---|
| 37 | '}\n' |
|---|
| 38 | |
|---|
| 39 | override_template = \ |
|---|
| 40 | '/* -*- Mode: C; c-basic-offset: 4 -*- */\n' + \ |
|---|
| 41 | '%%%%\n' + \ |
|---|
| 42 | 'headers\n' + \ |
|---|
| 43 | '/* include any required headers here */\n' + \ |
|---|
| 44 | '%%%%\n' + \ |
|---|
| 45 | 'init\n' + \ |
|---|
| 46 | ' /* include any code here that needs to be executed before the\n' + \ |
|---|
| 47 | ' * extension classes get initialised */\n' + \ |
|---|
| 48 | '%%%%\n' + \ |
|---|
| 49 | '\n' + \ |
|---|
| 50 | '/* you should add appropriate ignore, ignore-glob and\n' + \ |
|---|
| 51 | ' * override sections here */\n' |
|---|
| 52 | |
|---|
| 53 | def open_with_backup(file): |
|---|
| 54 | if os.path.exists(file): |
|---|
| 55 | try: |
|---|
| 56 | os.rename(file, file+'~') |
|---|
| 57 | except OSError: |
|---|
| 58 | # fail silently if we can't make a backup |
|---|
| 59 | pass |
|---|
| 60 | return open(file, 'w') |
|---|
| 61 | |
|---|
| 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 }) |
|---|
| 65 | fp.close() |
|---|
| 66 | fp = open_with_backup(fileprefix+'.override') |
|---|
| 67 | fp.write(override_template % { 'prefix': prefix, 'module': module }) |
|---|
| 68 | fp.close() |
|---|
| 69 | |
|---|
| 70 | if __name__ == '__main__': |
|---|
| 71 | opts, args = getopt.getopt(sys.argv[1:], 'f:p:m:h', |
|---|
| 72 | ['file-prefix=', 'prefix=', 'module=', 'help']) |
|---|
| 73 | fileprefix = None |
|---|
| 74 | prefix = None |
|---|
| 75 | module = None |
|---|
| 76 | for opt, arg in opts: |
|---|
| 77 | if opt in ('-f', '--file-prefix'): |
|---|
| 78 | fileprefix = arg |
|---|
| 79 | elif opt in ('-p', '--prefix'): |
|---|
| 80 | prefix = arg |
|---|
| 81 | elif opt in ('-m', '--module'): |
|---|
| 82 | module = arg |
|---|
| 83 | elif opt in ('-h', '--help'): |
|---|
| 84 | print 'usage: mkskel.py -f fileprefix -p prefix -m module' |
|---|
| 85 | sys.exit(0) |
|---|
| 86 | if not fileprefix or not prefix or not module: |
|---|
| 87 | print 'usage: mkskel.py -f fileprefix -p prefix -m module' |
|---|
| 88 | sys.exit(1) |
|---|
| 89 | write_skels(fileprefix, prefix, module) |
|---|