~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to lib/python/ctypes/ctypesgencore/printer/printer.py

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import os, sys, time
 
4
from ctypesgencore.descriptions import *
 
5
from ctypesgencore.ctypedescs import *
 
6
from ctypesgencore.messages import *
 
7
 
 
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
 
10
 
 
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)
 
14
 
 
15
class WrapperPrinter:
 
16
    def __init__(self,outpath,options,data):
 
17
        status_message("Writing to %s." % outpath)
 
18
        
 
19
        self.file=file(outpath,"w")
 
20
        self.options=options
 
21
 
 
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
 
25
        
 
26
        self.print_header()
 
27
        print >>self.file
 
28
        
 
29
        self.print_preamble()
 
30
        print >>self.file
 
31
        
 
32
        self.print_loader()
 
33
        print >>self.file
 
34
                
 
35
        self.print_group(self.options.libraries,"libraries",self.print_library)
 
36
        self.print_group(self.options.modules,"modules",self.print_module)
 
37
        
 
38
        method_table = {
 
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
 
47
        }
 
48
        
 
49
        for kind,desc in data.output_order:
 
50
            if desc.included:
 
51
                method_table[kind](desc)
 
52
                print >>self.file
 
53
        
 
54
        self.print_group(self.options.inserted_files,"inserted files",
 
55
                         self.insert_file)
 
56
    
 
57
    def print_group(self,list,name,function):
 
58
        if list:
 
59
            print >>self.file,"# Begin %s" % name
 
60
            print >>self.file
 
61
            for obj in list:
 
62
                function(obj)
 
63
            print >>self.file
 
64
            print >>self.file,"# %d %s" % (len(list),name)
 
65
            print >>self.file,"# End %s" % name
 
66
        else:
 
67
            print >>self.file,"# No %s" % name
 
68
        print >>self.file
 
69
    
 
70
    def srcinfo(self,src):
 
71
        if src==None:
 
72
            print >>self.file
 
73
        else:
 
74
            filename,lineno = src
 
75
            if filename in ("<built-in>","<command line>"):
 
76
                print >>self.file, "# %s" % filename
 
77
            else:
 
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)
 
82
    
 
83
    def template_subs(self):
 
84
        template_subs={
 
85
            'date': time.ctime(),
 
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])
 
88
        }
 
89
        
 
90
        for opt,value in self.options.__dict__.iteritems():
 
91
            if type(value)==str:
 
92
                template_subs[opt]=value
 
93
            elif isinstance(value,(list,tuple)):
 
94
                template_subs[opt]=(os.path.sep).join(value)
 
95
            else:
 
96
                template_subs[opt]=repr(value)
 
97
        
 
98
        return template_subs
 
99
    
 
100
    def print_header(self):
 
101
        template_file = None
 
102
        
 
103
        if self.options.header_template:
 
104
            path = self.options.header_template
 
105
            try:
 
106
                template_file = file(path,"r")
 
107
            except IOError:
 
108
                error_message("Cannot load header template from file \"%s\" " \
 
109
                    " - using default template." % path, cls = 'missing-file')
 
110
        
 
111
        if not template_file:
 
112
            path = path_to_local_file("defaultheader.py")
 
113
            template_file = file(path,"r")
 
114
        
 
115
        template_subs=self.template_subs()
 
116
        self.file.write(template_file.read() % template_subs)
 
117
        
 
118
        template_file.close()
 
119
    
 
120
    def print_preamble(self):
 
121
        path = path_to_local_file("preamble.py")
 
122
        
 
123
        print >>self.file, "# Begin preamble"
 
124
        print >>self.file
 
125
        preamble_file=file(path,"r")
 
126
        self.file.write(preamble_file.read())
 
127
        preamble_file.close()
 
128
        print >>self.file
 
129
        print >>self.file, "# End preamble"
 
130
    
 
131
    def print_loader(self):
 
132
        print >>self.file, "_libs = {}"
 
133
        print >>self.file, "_libdirs = %s" % self.options.compile_libdirs
 
134
        print >>self.file
 
135
        print >>self.file, "# Begin loader"
 
136
        print >>self.file
 
137
        path = path_to_local_file("libraryloader.py",
 
138
                                      ctypesgencore.libraryloader)
 
139
        loader_file=file(path,"r")
 
140
        self.file.write(loader_file.read())
 
141
        loader_file.close()
 
142
        print >>self.file
 
143
        print >>self.file, "# End loader"
 
144
        print >>self.file
 
145
        print >>self.file, "add_library_search_dirs([%s])" % \
 
146
                ", ".join([repr(d) for d in self.options.runtime_libdirs])
 
147
    
 
148
    def print_library(self,library):
 
149
        print >>self.file, '_libs["%s"] = load_library("%s")'%(library,library)
 
150
    
 
151
    def print_module(self,module):
 
152
        print >>self.file, 'from %s import *' % name
 
153
    
 
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)
 
158
    
 
159
    def print_typedef(self,typedef):
 
160
        print >>self.file, '%s = %s' % \
 
161
            (typedef.name,typedef.ctype.py_string()),
 
162
        self.srcinfo(typedef.src)
 
163
    
 
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'
 
170
    
 
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))
 
182
            else:
 
183
                print >>self.file, "    ('%s', %s)," % (name, ctype.py_string())
 
184
        print >>self.file, ']'
 
185
    
 
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.
 
190
    
 
191
    def print_function(self, function):
 
192
        if function.variadic:
 
193
            self.print_variadic_function(function)
 
194
        else:
 
195
            self.print_fixed_function(function)
 
196
    
 
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]))
 
208
        else:
 
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"
 
216
    
 
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)" % \
 
228
                function.py_name()
 
229
        else:
 
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" % \
 
233
                (function.c_name())
 
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)" % \
 
238
                function.py_name()
 
239
 
 
240
    
 
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)' % \
 
246
                (variable.py_name(),
 
247
                 variable.ctype.py_string(),
 
248
                 variable.source_library,
 
249
                 variable.c_name())
 
250
            print >>self.file, 'except:'
 
251
            print >>self.file, '    pass'
 
252
        else:
 
253
            print >>self.file, "for _lib in _libs.values():"
 
254
            print >>self.file, '    try:'
 
255
            print >>self.file, '        %s = (%s).in_dll(_lib, %r)' % \
 
256
                (variable.py_name(),
 
257
                 variable.ctype.py_string(),
 
258
                 variable.c_name())
 
259
            print >>self.file, "        break"
 
260
            print >>self.file, '    except:'
 
261
            print >>self.file, '        pass'
 
262
    
 
263
    def print_macro(self, macro):
 
264
        if macro.params:
 
265
            self.print_func_macro(macro)
 
266
        else:
 
267
            self.print_simple_macro(macro)
 
268
    
 
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"
 
278
    
 
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)
 
284
    
 
285
    def insert_file(self,filename):
 
286
        try:
 
287
            inserted_file = file(filename,"r")
 
288
        except IOError:
 
289
            error_message("Cannot open file \"%s\". Skipped it." % filename,
 
290
                          cls = 'missing-file')
 
291
        
 
292
        print >>self.file,"# Begin \"%s\"" % filename
 
293
        print >>self.file
 
294
        self.file.write(inserted_file.read())
 
295
        print >>self.file
 
296
        print >>self.file,"# End \"%s\"" % filename
 
297
              
 
298
        inserted_file.close()