4
from ctypesgencore.descriptions import *
5
from ctypesgencore.ctypedescs import *
6
from ctypesgencore.messages import *
8
import ctypesgencore.libraryloader # So we can get the path to it
9
import test # So we can find the path to local files in the printer package
11
def path_to_local_file(name,known_local_module = test):
12
basedir=os.path.dirname(known_local_module.__file__)
13
return os.path.join(basedir,name)
16
def __init__(self,outpath,options,data):
17
status_message("Writing to %s." % outpath)
19
self.file=file(outpath,"w")
22
if self.options.strip_build_path and \
23
self.options.strip_build_path[-1] != os.path.sep:
24
self.options.strip_build_path += os.path.sep
35
self.print_group(self.options.libraries,"libraries",self.print_library)
36
self.print_group(self.options.modules,"modules",self.print_module)
39
'function': self.print_function,
40
'macro': self.print_macro,
41
'struct': self.print_struct,
42
'struct-body': self.print_struct_members,
43
'typedef': self.print_typedef,
44
'variable': self.print_variable,
45
'enum': self.print_enum,
46
'constant': self.print_constant
49
for kind,desc in data.output_order:
51
method_table[kind](desc)
54
self.print_group(self.options.inserted_files,"inserted files",
57
def print_group(self,list,name,function):
59
print >>self.file,"# Begin %s" % name
64
print >>self.file,"# %d %s" % (len(list),name)
65
print >>self.file,"# End %s" % name
67
print >>self.file,"# No %s" % name
70
def srcinfo(self,src):
75
if filename in ("<built-in>","<command line>"):
76
print >>self.file, "# %s" % filename
78
if self.options.strip_build_path and \
79
filename.startswith(self.options.strip_build_path):
80
filename = filename[len(self.options.strip_build_path):]
81
print >>self.file, "# %s: %s" % (filename, lineno)
83
def template_subs(self):
86
'argv': ' '.join([x for x in sys.argv if not x.startswith("--strip-build-path")]),
87
'name': os.path.basename(self.options.headers[0])
90
for opt,value in self.options.__dict__.iteritems():
92
template_subs[opt]=value
93
elif isinstance(value,(list,tuple)):
94
template_subs[opt]=(os.path.sep).join(value)
96
template_subs[opt]=repr(value)
100
def print_header(self):
103
if self.options.header_template:
104
path = self.options.header_template
106
template_file = file(path,"r")
108
error_message("Cannot load header template from file \"%s\" " \
109
" - using default template." % path, cls = 'missing-file')
111
if not template_file:
112
path = path_to_local_file("defaultheader.py")
113
template_file = file(path,"r")
115
template_subs=self.template_subs()
116
self.file.write(template_file.read() % template_subs)
118
template_file.close()
120
def print_preamble(self):
121
path = path_to_local_file("preamble.py")
123
print >>self.file, "# Begin preamble"
125
preamble_file=file(path,"r")
126
self.file.write(preamble_file.read())
127
preamble_file.close()
129
print >>self.file, "# End preamble"
131
def print_loader(self):
132
print >>self.file, "_libs = {}"
133
print >>self.file, "_libdirs = %s" % self.options.compile_libdirs
135
print >>self.file, "# Begin loader"
137
path = path_to_local_file("libraryloader.py",
138
ctypesgencore.libraryloader)
139
loader_file=file(path,"r")
140
self.file.write(loader_file.read())
143
print >>self.file, "# End loader"
145
print >>self.file, "add_library_search_dirs([%s])" % \
146
", ".join([repr(d) for d in self.options.runtime_libdirs])
148
def print_library(self,library):
149
print >>self.file, '_libs["%s"] = load_library("%s")'%(library,library)
151
def print_module(self,module):
152
print >>self.file, 'from %s import *' % name
154
def print_constant(self,constant):
155
print >>self.file, '%s = %s' % \
156
(constant.name,constant.value.py_string(False)),
157
self.srcinfo(constant.src)
159
def print_typedef(self,typedef):
160
print >>self.file, '%s = %s' % \
161
(typedef.name,typedef.ctype.py_string()),
162
self.srcinfo(typedef.src)
164
def print_struct(self, struct):
165
self.srcinfo(struct.src)
166
base = {'union': 'Union', 'struct': 'Structure'}[struct.variety]
167
print >>self.file, 'class %s_%s(%s):' % \
168
(struct.variety, struct.tag, base)
169
print >>self.file, ' pass'
171
def print_struct_members(self, struct):
172
if struct.opaque: return
173
print >>self.file, '%s_%s.__slots__ = [' % (struct.variety, struct.tag)
174
for name,ctype in struct.members:
175
print >>self.file, " '%s'," % name
176
print >>self.file, ']'
177
print >>self.file, '%s_%s._fields_ = [' % (struct.variety, struct.tag)
178
for name,ctype in struct.members:
179
if isinstance(ctype,CtypesBitfield):
180
print >>self.file, " ('%s', %s, %s)," % \
181
(name, ctype.py_string(), ctype.bitfield.py_string(False))
183
print >>self.file, " ('%s', %s)," % (name, ctype.py_string())
184
print >>self.file, ']'
186
def print_enum(self,enum):
187
print >>self.file, 'enum_%s = c_int' % enum.tag,
188
self.srcinfo(enum.src)
189
# Values of enumerator are output as constants.
191
def print_function(self, function):
192
if function.variadic:
193
self.print_variadic_function(function)
195
self.print_fixed_function(function)
197
def print_fixed_function(self, function):
198
self.srcinfo(function.src)
199
if function.source_library:
200
print >>self.file, "if hasattr(_libs[%r], %r):" % \
201
(function.source_library,function.c_name())
202
print >>self.file, " %s = _libs[%r].%s" % \
203
(function.py_name(),function.source_library,function.c_name())
204
print >>self.file, " %s.restype = %s" % \
205
(function.py_name(),function.restype.py_string())
206
print >>self.file, " %s.argtypes = [%s]" % (function.py_name(),
207
', '.join([a.py_string() for a in function.argtypes]))
209
print >>self.file, "for _lib in _libs.values():"
210
print >>self.file, " if hasattr(_lib, %r):" % function.c_name()
211
print >>self.file, " %s = _lib.%s" % (function.py_name(),function.c_name())
212
print >>self.file, " %s.restype = %s" % (function.py_name(),function.restype.py_string())
213
print >>self.file, " %s.argtypes = [%s]" % (function.py_name(),
214
', '.join([a.py_string() for a in function.argtypes]))
215
print >>self.file, " break"
217
def print_variadic_function(self,function):
218
self.srcinfo(function.src)
219
if function.source_library:
220
print >>self.file, "if hasattr(_libs[%r], %r):" % \
221
(function.source_library,function.c_name())
222
print >>self.file, " _func = _libs[%r].%s" % \
223
(function.source_library,function.c_name())
224
print >>self.file, " _restype = %s" % function.restype.py_string()
225
print >>self.file, " _argtypes = [%s]" % \
226
', '.join([a.py_string() for a in function.argtypes])
227
print >>self.file, " %s = _variadic_function(_func,_restype,_argtypes)" % \
230
print >>self.file, "for _lib in _libs.values():"
231
print >>self.file, " if hasattr(_lib, %r):" % function.c_name()
232
print >>self.file, " _func = _lib.%s" % \
234
print >>self.file, " _restype = %s" % function.restype.py_string()
235
print >>self.file, " _argtypes = [%s]" % \
236
', '.join([a.py_string() for a in function.argtypes])
237
print >>self.file, " %s = _variadic_function(_func,_restype,_argtypes)" % \
241
def print_variable(self, variable):
242
self.srcinfo(variable.src)
243
if variable.source_library:
244
print >>self.file, 'try:'
245
print >>self.file, ' %s = (%s).in_dll(_libs[%r], %r)' % \
247
variable.ctype.py_string(),
248
variable.source_library,
250
print >>self.file, 'except:'
251
print >>self.file, ' pass'
253
print >>self.file, "for _lib in _libs.values():"
254
print >>self.file, ' try:'
255
print >>self.file, ' %s = (%s).in_dll(_lib, %r)' % \
257
variable.ctype.py_string(),
259
print >>self.file, " break"
260
print >>self.file, ' except:'
261
print >>self.file, ' pass'
263
def print_macro(self, macro):
265
self.print_func_macro(macro)
267
self.print_simple_macro(macro)
269
def print_simple_macro(self, macro):
270
# The macro translator makes heroic efforts but it occasionally fails.
271
# We want to contain the failures as much as possible.
272
# Hence the try statement.
273
self.srcinfo(macro.src)
274
print >>self.file, "try:"
275
print >>self.file, " %s = %s" % (macro.name,macro.expr.py_string(True))
276
print >>self.file, "except:"
277
print >>self.file, " pass"
279
def print_func_macro(self, macro):
280
self.srcinfo(macro.src)
281
print >>self.file, "def %s(%s):" % \
282
(macro.name,", ".join(macro.params))
283
print >>self.file, " return %s" % macro.expr.py_string(True)
285
def insert_file(self,filename):
287
inserted_file = file(filename,"r")
289
error_message("Cannot open file \"%s\". Skipped it." % filename,
290
cls = 'missing-file')
292
print >>self.file,"# Begin \"%s\"" % filename
294
self.file.write(inserted_file.read())
296
print >>self.file,"# End \"%s\"" % filename
298
inserted_file.close()