1 | # Copyright Bruno da Silva de Oliveira 2003. Use, modification and |
---|
2 | # distribution is subject to the Boost Software License, Version 1.0. |
---|
3 | # (See accompanying file LICENSE_1_0.txt or copy at |
---|
4 | # http:#www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | from settings import namespaces |
---|
7 | import settings |
---|
8 | from utils import remove_duplicated_lines, left_equals |
---|
9 | from SmartFile import SmartFile |
---|
10 | |
---|
11 | |
---|
12 | #============================================================================== |
---|
13 | # SingleCodeUnit |
---|
14 | #============================================================================== |
---|
15 | class SingleCodeUnit: |
---|
16 | ''' |
---|
17 | Represents a cpp file, where other objects can write in one of the |
---|
18 | predefined sections. |
---|
19 | The avaiable sections are: |
---|
20 | pchinclude - The pre-compiled header area |
---|
21 | include - The include area of the cpp file |
---|
22 | declaration - The part before the module definition |
---|
23 | module - Inside the BOOST_PYTHON_MODULE macro |
---|
24 | ''' |
---|
25 | |
---|
26 | def __init__(self, modulename, filename): |
---|
27 | self.modulename = modulename |
---|
28 | self.filename = filename |
---|
29 | # define the avaiable sections |
---|
30 | self.code = {} |
---|
31 | # include section |
---|
32 | self.code['pchinclude'] = '' |
---|
33 | # include section |
---|
34 | self.code['include'] = '' |
---|
35 | # declaration section (inside namespace) |
---|
36 | self.code['declaration'] = '' |
---|
37 | # declaration (outside namespace) |
---|
38 | self.code['declaration-outside'] = '' |
---|
39 | # inside BOOST_PYTHON_MACRO |
---|
40 | self.code['module'] = '' |
---|
41 | # create the default module definition |
---|
42 | self.module_definition = 'BOOST_PYTHON_MODULE(%s)' % modulename |
---|
43 | |
---|
44 | |
---|
45 | def Write(self, section, code): |
---|
46 | 'write the given code in the section of the code unit' |
---|
47 | if section not in self.code: |
---|
48 | raise RuntimeError, 'Invalid CodeUnit section: %s' % section |
---|
49 | self.code[section] += code |
---|
50 | |
---|
51 | |
---|
52 | def Merge(self, other): |
---|
53 | for section in ('include', 'declaration', 'declaration-outside', 'module'): |
---|
54 | self.code[section] = self.code[section] + other.code[section] |
---|
55 | |
---|
56 | |
---|
57 | def Section(self, section): |
---|
58 | return self.code[section] |
---|
59 | |
---|
60 | |
---|
61 | def SetCurrent(self, *args): |
---|
62 | pass |
---|
63 | |
---|
64 | |
---|
65 | def Current(self): |
---|
66 | pass |
---|
67 | |
---|
68 | |
---|
69 | def Save(self, append=False): |
---|
70 | 'Writes this code unit to the filename' |
---|
71 | space = '\n\n' |
---|
72 | if not append: |
---|
73 | flag = 'w' |
---|
74 | else: |
---|
75 | flag = 'a' |
---|
76 | fout = SmartFile(self.filename, flag) |
---|
77 | fout.write('\n') |
---|
78 | # includes |
---|
79 | # boost.python header |
---|
80 | if self.code['pchinclude']: |
---|
81 | fout.write(left_equals('PCH')) |
---|
82 | fout.write(self.code['pchinclude']+'\n') |
---|
83 | fout.write('#ifdef _MSC_VER\n') |
---|
84 | fout.write('#pragma hdrstop\n') |
---|
85 | fout.write('#endif\n') |
---|
86 | else: |
---|
87 | fout.write(left_equals('Boost Includes')) |
---|
88 | fout.write('#include <boost/python.hpp>\n') |
---|
89 | # include numerical boost for int64 definitions |
---|
90 | fout.write('#include <boost/cstdint.hpp>\n') |
---|
91 | fout.write('\n') |
---|
92 | # other includes |
---|
93 | if self.code['include']: |
---|
94 | fout.write(left_equals('Includes')) |
---|
95 | includes = remove_duplicated_lines(self.code['include']) |
---|
96 | fout.write(includes) |
---|
97 | fout.write(space) |
---|
98 | # using |
---|
99 | if settings.USING_BOOST_NS and not append: |
---|
100 | fout.write(left_equals('Using')) |
---|
101 | fout.write('using namespace boost::python;\n\n') |
---|
102 | # declarations |
---|
103 | declaration = self.code['declaration'] |
---|
104 | declaration_outside = self.code['declaration-outside'] |
---|
105 | if declaration_outside or declaration: |
---|
106 | fout.write(left_equals('Declarations')) |
---|
107 | if declaration_outside: |
---|
108 | fout.write(declaration_outside + '\n\n') |
---|
109 | if declaration: |
---|
110 | pyste_namespace = namespaces.pyste[:-2] |
---|
111 | fout.write('namespace %s {\n\n' % pyste_namespace) |
---|
112 | fout.write(declaration) |
---|
113 | fout.write('\n}// namespace %s\n' % pyste_namespace) |
---|
114 | fout.write(space) |
---|
115 | # module |
---|
116 | fout.write(left_equals('Module')) |
---|
117 | fout.write(self.module_definition + '\n') |
---|
118 | fout.write('{\n') |
---|
119 | fout.write(self.code['module']) |
---|
120 | fout.write('}\n\n') |
---|
121 | fout.close() |
---|