Commit a8ef4035 authored by Johan Dahlin's avatar Johan Dahlin

codegen/*: Copy PyGTK code generator here, to avoid depending on latest version.

Original commit message from CVS:
* codegen/*: Copy PyGTK code generator here, to avoid depending on
latest version.

* configure.ac: Add versioning, ala pygtk, and use the internal
code generator instead of the installed one
parent c93b5c13
2004-03-05 Johan Dahlin <johan@gnome.org>
* codegen/*: Copy PyGTK code generator here, to avoid depending on
latest version.
* configure.ac: Add versioning, ala pygtk, and use the internal
code generator instead of the installed one
2004-03-04 Thomas Vander Stichele <thomas at apestaart dot org>
* configure.ac:
......
codegen_PYTHON = \
__init__.py \
argtypes.py \
codegen.py \
definitions.py \
defsparser.py \
docextract.py \
docgen.py \
h2def.py \
mergedefs.py \
mkskel.py \
override.py \
scmexpr.py
# -*- Mode: Python; py-indent-offset: 4 -*-
__all__ = [
'argtypes',
'codegen',
'definitions',
'defsparser',
'docextract',
'docgen',
'h2def',
'mergedefs',
'mkskel',
'override',
'scmexpr'
]
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
import sys
import string, re
# ------------------ Create typecodes from typenames ---------
_upperstr_pat1 = re.compile(r'([^A-Z])([A-Z])')
_upperstr_pat2 = re.compile(r'([A-Z][A-Z])([A-Z][0-9a-z])')
_upperstr_pat3 = re.compile(r'^([A-Z])([A-Z])')
def to_upper_str(name):
"""Converts a typename to the equivalent upercase and underscores
name. This is used to form the type conversion macros and enum/flag
name variables"""
name = _upperstr_pat1.sub(r'\1_\2', name)
name = _upperstr_pat2.sub(r'\1_\2', name)
name = _upperstr_pat3.sub(r'\1_\2', name, count=1)
return string.upper(name)
def typecode(typename):
"""create a typecode (eg. GTK_TYPE_WIDGET) from a typename"""
return string.replace(to_upper_str(typename), '_', '_TYPE_', 1)
STATE_START = 0
STATE_OBJECT = 1
STATE_INTERFACE = 2
STATE_BOXED = 3
STATE_ENUM = 4
STATE_FLAGS = 5
STATE_METHOD = 6
STATE_FUNCTION = 7
def convert(infp=sys.stdin, outfp=sys.stdout):
state = STATE_START
seen_params = 0
line = infp.readline()
while line:
if line[:8] == '(object ':
state = STATE_OBJECT
seen_params = 0
outfp.write('(define-object ' + line[8:])
elif line[:11] == '(interface ':
state = STATE_INTERFACE
seen_params = 0
outfp.write('(define-interface ' + line[11:])
elif line[:7] == '(boxed ':
state = STATE_BOXED
seen_params = 0
outfp.write('(define-boxed ' + line[7:])
elif line[:6] == '(enum ':
state = STATE_ENUM
seen_params = 0
outfp.write('(define-enum ' + line[6:])
elif line[:7] == '(flags ':
state = STATE_FLAGS
seen_params = 0
outfp.write('(define-flags ' + line[7:])
elif line[:8] == '(method ':
state = STATE_METHOD
seen_params = 0
outfp.write('(define-method ' + line[8:])
elif line[:10] == '(function ':
state = STATE_FUNCTION
seen_params = 0
outfp.write('(define-function ' + line[10:])
elif line[:13] == ' (in-module ':
outfp.write(re.sub(r'^(\s+\(in-module\s+)(\w+)(.*)$',
r'\1"\2"\3', line))
elif line[:10] == ' (parent ':
outfp.write(re.sub(r'^(\s+\(parent\s+)(\w+)(\s+\((\w+)\))?(.*)$',
r'\1"\4\2"\5', line))
elif line[:14] == ' (implements ':
outfp.write(re.sub(r'^(\s+\(implements\s+)([^\s]+)(\s*\))$',
r'\1"\2"\3', line))
elif line[:13] == ' (of-object ':
outfp.write(re.sub(r'^(\s+\(of-object\s+)(\w+)(\s+\((\w+)\))?(.*)$',
r'\1"\4\2"\5', line))
elif line[:10] == ' (c-name ':
outfp.write(re.sub(r'^(\s+\(c-name\s+)([^\s]+)(\s*\))$',
r'\1"\2"\3', line))
if state in (STATE_OBJECT, STATE_INTERFACE, STATE_BOXED,
STATE_ENUM, STATE_FLAGS):
c_name = re.match(r'^\s+\(c-name\s+([^\s]+)\s*\)$',
line).group(1)
outfp.write(' (gtype-id "%s")\n' % typecode(c_name))
elif line[:15] == ' (return-type ':
outfp.write(re.sub(r'^(\s+\(return-type\s+)([^\s]+)(\s*\))$',
r'\1"\2"\3', line))
elif line[:13] == ' (copy-func ':
outfp.write(re.sub(r'^(\s+\(copy-func\s+)(\w+)(.*)$',
r'\1"\2"\3', line))
elif line[:16] == ' (release-func ':
outfp.write(re.sub(r'^(\s+\(release-func\s+)(\w+)(.*)$',
r'\1"\2"\3', line))
elif line[:9] == ' (field ':
if not seen_params:
outfp.write(' (fields\n')
seen_params = 1
outfp.write(re.sub(r'^\s+\(field\s+\(type-and-name\s+([^\s]+)\s+([^\s]+)\s*\)\s*\)$',
' \'("\\1" "\\2")', line))
elif line[:9] == ' (value ':
if not seen_params:
outfp.write(' (values\n')
seen_params = 1
outfp.write(re.sub(r'^\s+\(value\s+\(name\s+([^\s]+)\)\s+\(c-name\s+([^\s]+)\s*\)\s*\)$',
' \'("\\1" "\\2")', line))
elif line[:13] == ' (parameter ':
if not seen_params:
outfp.write(' (parameters\n')
seen_params = 1
outfp.write(re.sub(r'^\s+\(parameter\s+\(type-and-name\s+([^\s]+)\s+([^\s]+)\s*\)(\s*.*)\)$',
' \'("\\1" "\\2"\\3)', line))
elif line[:11] == ' (varargs ':
if seen_params:
outfp.write(' )\n')
seen_params = 0
outfp.write(' (varargs #t)\n')
elif line[0] == ')':
if seen_params:
outfp.write(' )\n')
seen_params = 0
state = STATE_START
outfp.write(line)
else:
outfp.write(line)
line = infp.readline()
if __name__ == '__main__':
convert()
# -*- Mode: Python; py-indent-offset: 4 -*-
import os, sys
import scmexpr
from definitions import *
class IncludeParser(scmexpr.Parser):
"""A simple parser that follows include statements automatically"""
def include(self, filename):
if not os.path.isabs(filename):
filename = os.path.join(os.path.dirname(self.filename), filename)
# set self.filename to the include name, to handle recursive includes
oldfile = self.filename
self.filename = filename
self.startParsing()
self.filename = oldfile
class DefsParser(IncludeParser):
def __init__(self, arg, defines={}):
IncludeParser.__init__(self, arg)
self.objects = []
self.interfaces = []
self.enums = [] # enums and flags
self.boxes = [] # boxed types
self.pointers = [] # pointer types
self.functions = [] # functions and methods
self.c_name = {} # hash of c names of functions
self.methods = {} # hash of methods of particular objects
self.defines = defines # -Dfoo=bar options, as dictionary
def define_object(self, *args):
odef = apply(ObjectDef, args)
self.objects.append(odef)
self.c_name[odef.c_name] = odef
def define_interface(self, *args):
idef = apply(InterfaceDef, args)
self.interfaces.append(idef)
self.c_name[idef.c_name] = idef
def define_enum(self, *args):
edef = apply(EnumDef, args)
self.enums.append(edef)
self.c_name[edef.c_name] = edef
def define_flags(self, *args):
fdef = apply(FlagsDef, args)
self.enums.append(fdef)
self.c_name[fdef.c_name] = fdef
def define_boxed(self, *args):
bdef = apply(BoxedDef, args)
self.boxes.append(bdef)
self.c_name[bdef.c_name] = bdef
def define_pointer(self, *args):
pdef = apply(PointerDef, args)
self.pointers.append(pdef)
self.c_name[pdef.c_name] = pdef
def define_function(self, *args):
fdef = apply(FunctionDef, args)
self.functions.append(fdef)
self.c_name[fdef.c_name] = fdef
def define_method(self, *args):
mdef = apply(MethodDef, args)
self.functions.append(mdef)
self.c_name[mdef.c_name] = mdef
def merge(self, old):
for obj in self.objects:
if old.c_name.has_key(obj.c_name):
obj.merge(old.c_name[obj.c_name])
for f in self.functions:
if old.c_name.has_key(f.c_name):
f.merge(old.c_name[f.c_name])
def printMissing(self, old):
for obj in self.objects:
if not old.c_name.has_key(obj.c_name):
obj.write_defs()
for f in self.functions:
if not old.c_name.has_key(f.c_name):
f.write_defs()
def write_defs(self, fp=sys.stdout):
for obj in self.objects:
obj.write_defs(fp)
for enum in self.enums:
enum.write_defs(fp)
for boxed in self.boxes:
boxed.write_defs(fp)
for pointer in self.pointers:
pointer.write_defs(fp)
for func in self.functions:
func.write_defs(fp)
def find_object(self, c_name):
for obj in self.objects:
if obj.c_name == c_name:
return obj
else:
raise ValueError, 'object not found'
def find_constructor(self, obj, overrides):
for func in self.functions:
if isinstance(func, FunctionDef) and \
func.is_constructor_of == obj.c_name and \
not overrides.is_ignored(func.c_name):
return func
def find_methods(self, obj):
objname = obj.c_name
return filter(lambda func, on=objname: isinstance(func, MethodDef) and
func.of_object == on, self.functions)
def find_functions(self):
return filter(lambda func: isinstance(func, FunctionDef) and
not func.is_constructor_of, self.functions)
def ifdef(self, *args):
if args[1] in self.defines:
for arg in args[2:]:
self.handle(arg)
# -*- Mode: Python; py-indent-offset: 4 -*-
'''Simple module for extracting GNOME style doc comments from C
sources, so I can use them for other purposes.'''
import sys, os, string, re
__all__ = ['extract']
class FunctionDoc:
def __init__(self):
self.name = None
self.params = []
self.description = ''
self.ret = ''
def set_name(self, name):
self.name = name
def add_param(self, name, description):
if name == '...':
name = 'Varargs'
self.params.append((name, description))
def append_to_last_param(self, extra):
self.params[-1] = (self.params[-1][0], self.params[-1][1] + extra)
def append_to_named_param(self, name, extra):
for i in range(len(self.params)):
if self.params[i][0] == name:
self.params[i] = (name, self.params[i][1] + extra)
return
# fall through to adding extra parameter ...
self.add_param(name, extra)
def append_description(self, extra):
self.description = self.description + extra
def append_return(self, extra):
self.ret = self.ret + extra
def get_param_description(self, name):
for param, description in self.params:
if param == name:
return description
else:
return ''
comment_start_pat = re.compile(r'^\s*/\*\*\s')
comment_end_pat = re.compile(r'^\s*\*+/')
comment_line_lead = re.compile(r'^\s*\*\s*')
funcname_pat = re.compile(r'^(\w+)\s*:?')
return_pat = re.compile(r'^(returns:|return\s+value:|returns\s*)(.*\n?)$',
re.IGNORECASE)
param_pat = re.compile(r'^@(\S+)\s*:(.*\n?)$')
def parse_file(fp, doc_dict):
line = fp.readline()
in_comment_block = 0
while line:
if not in_comment_block:
if comment_start_pat.match(line):
in_comment_block = 1
cur_doc = FunctionDoc()
in_description = 0
in_return = 0
line = fp.readline()
continue
# we are inside a comment block ...
if comment_end_pat.match(line):
if not cur_doc.name:
sys.stderr.write("no function name found in doc comment\n")
else:
doc_dict[cur_doc.name] = cur_doc
in_comment_block = 0
line = fp.readline()
continue
# inside a comment block, and not the end of the block ...
line = comment_line_lead.sub('', line)
if not line: line = '\n'
if not cur_doc.name:
match = funcname_pat.match(line)
if match:
cur_doc.set_name(match.group(1))
elif in_return:
match = return_pat.match(line)
if match:
# assume the last return statement was really part of the
# description
cur_doc.description = cur_doc.description + return_start + \
cur_doc.ret
return_start = match.group(1)
cur_doc.ret = match.group(2)
else:
cur_doc.append_return(line)
elif in_description:
if line[:12] == 'Description:':
line = line[12:]
match = return_pat.match(line)
if match:
in_return = 1
return_start = match.group(1)
cur_doc.append_return(match.group(2))
else:
cur_doc.append_description(line)
elif line == '\n':
# end of parameters
in_description = 1
else:
match = param_pat.match(line)
if match:
param = match.group(1)
desc = match.group(2)
cur_doc.add_param(param, desc)
else:
# must be continuation
try:
cur_doc.append_to_last_param(line)
except:
sys.stderr.write('something weird while reading param\n')
line = fp.readline()
def parse_dir(dir, doc_dict):
for file in os.listdir(dir):
if file in ('.', '..'): continue
path = os.path.join(dir, file)
if os.path.isdir(path):
parse_dir(path, doc_dict)
if len(file) > 2 and file[-2:] == '.c':
parse_file(open(path, 'r'), doc_dict)
def extract(dirs, doc_dict=None):
if not doc_dict: doc_dict = {}
for dir in dirs:
parse_dir(dir, doc_dict)
return doc_dict
tmpl_section_pat = re.compile(r'^<!-- ##### (\w+) (\w+) ##### -->$')
def parse_tmpl(fp, doc_dict):
cur_doc = None
line = fp.readline()
while line:
match = tmpl_section_pat.match(line)
if match:
cur_doc = None # new input shouldn't affect the old doc dict
sect_type = match.group(1)
sect_name = match.group(2)
if sect_type == 'FUNCTION':
cur_doc = doc_dict.get(sect_name)
if not cur_doc:
cur_doc = FunctionDoc()
cur_doc.set_name(sect_name)
doc_dict[sect_name] = cur_doc
elif line == '<!-- # Unused Parameters # -->\n':
cur_doc = None # don't worry about unused params.
elif cur_doc:
if line[:10] == '@Returns: ':
if string.strip(line[10:]):
cur_doc.append_return(line[10:])
elif line[0] == '@':
pos = string.find(line, ':')
if pos >= 0:
cur_doc.append_to_named_param(line[1:pos], line[pos+1:])
else:
cur_doc.append_description(line)
else:
cur_doc.append_description(line)
line = fp.readline()
def extract_tmpl(dirs, doc_dict=None):
if not doc_dict: doc_dict = {}
for dir in dirs:
for file in os.listdir(dir):
if file in ('.', '..'): continue
path = os.path.join(dir, file)
if os.path.isdir(path):
continue
if len(file) > 2 and file[-2:] == '.sgml':
parse_tmpl(open(path, 'r'), doc_dict)
return doc_dict
#!/usr/bin/env python
# -*- Mode: Python; py-indent-offset: 4 -*-
#
# This litte script outputs the C doc comments to an XML format.
# So far it's only used by gtkmm (The C++ bindings). Murray Cumming.
# Usage example:
# # ./docextract_to_xml.py -s /gnome/head/cvs/gtk+/gtk/ -s /gnome/head/cvs/gtk+/docs/reference/gtk/tmpl/ > gtk_docs.xml
import sys, os, string, re, getopt
import docextract
import string
def escape_text(unescaped_text):
escaped_text = unescaped_text
escaped_text = string.replace(escaped_text, '<', '&lt;')
escaped_text = string.replace(escaped_text, '>', '&gt;')
escaped_text = string.replace(escaped_text, '&', '&amp;')
escaped_text = string.replace(escaped_text, '\'', '&apos;')
escaped_text = string.replace(escaped_text, '\"', '&quot;')
#Apparently this is an undefined symbol:
escaped_text = string.replace(escaped_text, '&mdash;', ' mdash ')
return escaped_text
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "d:s:o:",
["source-dir="])
except getopt.error, e:
sys.stderr.write('docgen.py: %s\n' % e)
sys.stderr.write(
'usage: docgen.py [-s /src/dir]\n')
sys.exit(1)
source_dirs = []
for opt, arg in opts:
if opt in ('-s', '--source-dir'):
source_dirs.append(arg)
if len(args) != 0:
sys.stderr.write(
'usage: docgen.py [-s /src/dir]\n')
sys.exit(1)
docs = docextract.extract(source_dirs);
docextract.extract_tmpl(source_dirs, docs); #Try the tmpl sgml files too.
# print d.docs
if docs:
print "<root>"
for name, value in docs.items():
print "<function name=\"" + escape_text(name) + "\">"
print "<description>"
#The value is a docextract.FunctionDoc
print escape_text(value.description)
print "</description>"
# Loop through the parameters:
print "<parameters>"
for name, description in value.params:
print "<parameter name=\"" + escape_text(name) + "\">"
print "<parameter_description>" + escape_text(description) + "</parameter_description>"
print "</parameter>"
print "</parameters>"
# Show the return-type:
print "<return>" + escape_text(value.ret) + "</return>"
print "</function>\n"
print "</root>"
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env python
# -*- Mode: Python; py-indent-offset: 4 -*-
import sys
import defsparser
if len(sys.argv) < 3:
sys.stderr.write("Usage: mergedefs.py generated-defs old-defs\n")
sys.exit(1)
newp = defsparser.DefsParser(sys.argv[1])
oldp = defsparser.DefsParser(sys.argv[2])
newp.startParsing()
oldp.startParsing()
newp.merge(oldp)
newp.write_defs()
#!/usr/bin/env python
# -*- Mode: Python; py-indent-offset: 4 -*-
import sys
import defsparser
if len(sys.argv) < 3:
sys.stderr.write("Usage: missingdefs.py generated-defs old-defs\n")
sys.exit(1)
newp = defsparser.DefsParser(sys.argv[1])
oldp = defsparser.DefsParser(sys.argv[2])
newp.startParsing()
oldp.startParsing()
newp.printMissing(oldp)
#!/usr/bin/env python
# -*- Mode: Python; py-indent-offset: 4 -*-
import sys, os, getopt
module_init_template = \
'/* -*- Mode: C; c-basic-offset: 4 -*- */\n' + \
'#ifdef HAVE_CONFIG_H\n' + \
'# include "config.h"\n' + \
'#endif\n' + \
'#include <Python.h>\n' + \
'#include <pygtk.h>\n' + \
'\n' + \
'/* include any extra headers needed here */\n' + \
'\n' + \
'void %(prefix)s_register_classes(PyObject *d);\n' + \
'extern PyMethodDef %(prefix)s_functions[];\n' + \
'\n' + \
'DL_EXPORT(void)\n' + \
'init%(module)s(void)\n' + \
'{\n' + \
' PyObject *m, *d;\n' + \
'\n' + \
' /* perform any initialisation required by the library here */\n' + \
'\n' + \
' m = Py_InitModule("%(module)s", %(prefix)s_functions);\n' + \
' d = PyModule_GetDict(m);\n' + \
'\n' + \
' init_pygtk();\n' + \
'\n' + \
' %(prefix)s_register_classes(d);\n' + \
'\n' + \
' /* add anything else to the module dictionary (such as constants) */\n' +\
'\n' + \
' if (PyErr_Occurred())\n' + \
' Py_FatalError("could not initialise module %(module)s");\n' + \
'}\n'
override_template = \
'/* -*- Mode: C; c-basic-offset: 4 -*- */\n' + \
'%%%%\n' + \
'headers\n' + \
'/* include any required headers here */\n' + \
'%%%%\n' + \
'init\n' + \
' /* include any code here that needs to be executed before the\n' + \
' * extension classes get initialised */\n' + \
'%%%%\n' + \
'\n' + \
'/* you should add appropriate ignore, ignore-glob and\n' + \
' * override sections here */\n'
def open_with_backup(file):
if os.path.exists(file):
try:
os.rename(file, file+'~')
except OSError:
# fail silently if we can't make a backup
pass
return open(file, 'w')
def write_skels(fileprefix, prefix, module):
fp = open_with_backup(fileprefix+'module.c')
fp.write(module_init_template % { 'prefix': prefix, 'module': module })
fp.close()
fp = open_with_backup(fileprefix+'.override')
fp.write(override_template % { 'prefix': prefix, 'module': module })
fp.close()
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], 'f:p:m:h',
['file-prefix=', 'prefix=', 'module=', 'help'])
fileprefix = None
prefix = None
module = None
for opt, arg in opts:
if opt in ('-f', '--file-prefix'):
fileprefix = arg
elif opt in ('-p', '--prefix'):
prefix = arg
elif opt in ('-m', '--module'):
module = arg
elif opt in ('-h', '--help'):
print 'usage: mkskel.py -f fileprefix -p prefix -m module'
sys.exit(0)
if not fileprefix or not prefix or not module:
print 'usage: mkskel.py -f fileprefix -p prefix -m module'
sys.exit(1)
write_skels(fileprefix, prefix, module)
# -*- Mode: Python; py-indent-offset: 4 -*-
# this file contains code for loading up an override file. The override file
# provides implementations of functions where the code generator could not
# do its job correctly.
import fnmatch
import os
import re
import string
import sys
def class2cname(klass, method):
c_name = ''
for c in klass:
if c.isupper():
c_name += '_' + c.lower()
else:
c_name += c
return c_name[1:] + '_' + method
import_pat = re.compile(r'\s*import\s+(\S+)\.([^\s.]+)\s+as\s+(\S+)')
class Overrides:
def __init__(self, filename=None):
self.modulename = None
self.ignores = {}
self.glob_ignores = []
self.overrides = {}
self.overridden = {}
self.kwargs = {}
self.noargs = {}
self.startlines = {}