TIOVX User Guide
code_generate.py
1 #
2 # Copyright (c) 2017 Texas Instruments Incorporated
3 #
4 # All rights reserved not granted herein.
5 #
6 # Limited License.
7 #
8 # Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
9 # license under copyrights and patents it now or hereafter owns or controls to make,
10 # have made, use, import, offer to sell and sell ("Utilize") this software subject to the
11 # terms herein. With respect to the foregoing patent license, such license is granted
12 # solely to the extent that any such patent is necessary to Utilize the software alone.
13 # The patent license shall not apply to any combinations which include this software,
14 # other than combinations with devices manufactured by or for TI ("TI Devices").
15 # No hardware patent is licensed hereunder.
16 #
17 # Redistributions must preserve existing copyright notices and reproduce this license
18 # (including the above copyright notice and the disclaimer and (if applicable) source
19 # code license limitations below) in the documentation and/or other materials provided
20 # with the distribution
21 #
22 # Redistribution and use in binary form, without modification, are permitted provided
23 # that the following conditions are met:
24 #
25 # No reverse engineering, decompilation, or disassembly of this software is
26 # permitted with respect to any software provided in binary form.
27 #
28 # any redistribution and use are licensed by TI for use only with TI Devices.
29 #
30 # Nothing shall obligate TI to provide you with source code for the software
31 # licensed and provided to you in object code.
32 #
33 # If software source code is provided to you, modification and redistribution of the
34 # source code are permitted provided that the following conditions are met:
35 #
36 # any redistribution and use of the source code, including any resulting derivative
37 # works, are licensed by TI for use only with TI Devices.
38 #
39 # any redistribution and use of any object code compiled from the source code
40 # and any resulting derivative works, are licensed by TI for use only with TI Devices.
41 #
42 # Neither the name of Texas Instruments Incorporated nor the names of its suppliers
43 #
44 # may be used to endorse or promote products derived from this software without
45 # specific prior written permission.
46 #
47 # DISCLAIMER.
48 #
49 # THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
50 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 # IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
56 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
57 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
58 # OF THE POSSIBILITY OF SUCH DAMAGE.
59 #
60 #
61 
62 from . import *
63 
64 import datetime
65 
66 class CodeGenerate :
67  def __init__(self, filename, header=True, additional_filename="") :
68  self.indent_level = 0
69  self.indent_level_additional = 0
70  self.indent = " "
71  self.filename = filename
72  self.additional_filename_exists = False
73  if additional_filename :
74  self.additional_filename_exists = True
75  self.additional_filename = additional_filename
76  self.header = header
77  self.open()
78 
79  def open(self) :
80  self.file = open(self.filename,'w')
81  if self.additional_filename_exists :
82  self.additional_file = open(self.additional_filename,'w')
83  if self.header :
84  self.write_header()
85 
86  def close(self, new_line=True) :
87  if new_line :
88  self.write_newline()
89  self.file.close()
90  if self.additional_filename_exists :
91  self.additional_file.close()
92 
93  def write_header(self) :
94  self.now = datetime.datetime.now()
95  self.write_line('/*')
96  self.write_line(' *')
97  self.write_line(' * Copyright (c) ' + str(self.now.year) + ' Texas Instruments Incorporated')
98  self.write_line(' *')
99  self.write_line(' * All rights reserved not granted herein.')
100  self.write_line(' *')
101  self.write_line(' * Limited License.')
102  self.write_line(' *')
103  self.write_line(' * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive')
104  self.write_line(' * license under copyrights and patents it now or hereafter owns or controls to make,')
105  self.write_line(' * have made, use, import, offer to sell and sell ("Utilize") this software subject to the')
106  self.write_line(' * terms herein. With respect to the foregoing patent license, such license is granted')
107  self.write_line(' * solely to the extent that any such patent is necessary to Utilize the software alone.')
108  self.write_line(' * The patent license shall not apply to any combinations which include this software,')
109  self.write_line(' * other than combinations with devices manufactured by or for TI ("TI Devices").')
110  self.write_line(' * No hardware patent is licensed hereunder.')
111  self.write_line(' *')
112  self.write_line(' * Redistributions must preserve existing copyright notices and reproduce this license')
113  self.write_line(' * (including the above copyright notice and the disclaimer and (if applicable) source')
114  self.write_line(' * code license limitations below) in the documentation and/or other materials provided')
115  self.write_line(' * with the distribution')
116  self.write_line(' *')
117  self.write_line(' * Redistribution and use in binary form, without modification, are permitted provided')
118  self.write_line(' * that the following conditions are met:')
119  self.write_line(' *')
120  self.write_line(' * * No reverse engineering, decompilation, or disassembly of this software is')
121  self.write_line(' * permitted with respect to any software provided in binary form.')
122  self.write_line(' *')
123  self.write_line(' * * any redistribution and use are licensed by TI for use only with TI Devices.')
124  self.write_line(' *')
125  self.write_line(' * * Nothing shall obligate TI to provide you with source code for the software')
126  self.write_line(' * licensed and provided to you in object code.')
127  self.write_line(' *')
128  self.write_line(' * If software source code is provided to you, modification and redistribution of the')
129  self.write_line(' * source code are permitted provided that the following conditions are met:')
130  self.write_line(' *')
131  self.write_line(' * * any redistribution and use of the source code, including any resulting derivative')
132  self.write_line(' * works, are licensed by TI for use only with TI Devices.')
133  self.write_line(' *')
134  self.write_line(' * * any redistribution and use of any object code compiled from the source code')
135  self.write_line(' * and any resulting derivative works, are licensed by TI for use only with TI Devices.')
136  self.write_line(' *')
137  self.write_line(' * Neither the name of Texas Instruments Incorporated nor the names of its suppliers')
138  self.write_line(' *')
139  self.write_line(' * may be used to endorse or promote products derived from this software without')
140  self.write_line(' * specific prior written permission.')
141  self.write_line(' *')
142  self.write_line(' * DISCLAIMER.')
143  self.write_line(' *')
144  self.write_line(' * THIS SOFTWARE IS PROVIDED BY TI AND TI\'S LICENSORS "AS IS" AND ANY EXPRESS')
145  self.write_line(' * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES')
146  self.write_line(' * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.')
147  self.write_line(' * IN NO EVENT SHALL TI AND TI\'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,')
148  self.write_line(' * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,')
149  self.write_line(' * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,')
150  self.write_line(' * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY')
151  self.write_line(' * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE')
152  self.write_line(' * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED')
153  self.write_line(' * OF THE POSSIBILITY OF SUCH DAMAGE.')
154  self.write_line(' *')
155  self.write_line(' */')
156  self.write_newline()
157 
158  def write_comment_line(self, text_line, files=2) :
159  self.write_line('/* %s */' % text_line, True, True, files)
160 
161  # Note: files variable defines whether or not to output to additional files or not
162  # If files = 2, both files get written to; if files = 0, only the original file gets written to
163  # If files = 1, only the additional file gets written to
164  def write_line(self, text_line, new_line=True, indent=True, files=2) :
165  if indent :
166  for i in range(0, self.indent_level) :
167  if files == 2 or files == 0 :
168  self.file.write(self.indent)
169  if self.additional_filename_exists :
170  for i in range(0, self.indent_level_additional) :
171  if files == 2 or files == 1 :
172  self.additional_file.write(self.indent)
173  if files == 2 or files == 0 :
174  self.file.write(text_line)
175  if files == 2 or files == 1 :
176  if self.additional_filename_exists :
177  self.additional_file.write(text_line)
178  if new_line:
179  if files == 2 or files == 0 :
180  self.file.write('\n')
181  if files == 2 or files == 1 :
182  if self.additional_filename_exists :
183  self.additional_file.write('\n')
184 
185  def write_block(self, text_block, new_line=True, files=2) :
186  self.file.write(text_block)
187  if self.additional_filename_exists :
188  self.additional_file.write(text_block)
189  if new_line:
190  self.file.write('\n')
191  if self.additional_filename_exists :
192  self.additional_file.write('\n')
193 
194  def write_open_brace(self, files=2) :
195  self.write_line('{', True, True, files)
196  if files == 2 or files == 0 :
197  self.indent_level = self.indent_level+1
198  if files == 2 or files == 1 :
199  self.indent_level_additional = self.indent_level_additional+1
200 
201  def write_close_brace(self, text="", files=2) :
202  if files == 2 or files == 0 :
203  self.indent_level = self.indent_level-1
204  if files == 2 or files == 1 :
205  self.indent_level_additional = self.indent_level_additional-1
206  self.write_line('}%s' % text, True, True, files)
207 
208  def write_include(self, include_file_name, files=2) :
209  self.write_line('#include "%s"' % include_file_name, True, True, files)
210 
211  def write_ifndef_define(self, text, files=2) :
212  self.write_line('#ifndef %s' % text, True, True, files)
213  self.write_line('#define %s' % text, True, True, files)
214  self.write_newline(files)
215 
216  def write_endif(self, text, files=2) :
217  self.write_line('#endif /* %s */' % text, True, True, files)
218  self.write_newline(files)
219 
220  def write_extern_c_top(self, files=2) :
221  self.write_line("#ifdef __cplusplus", True, True, files)
222  self.write_line("extern \"C\" {", True, True, files)
223  self.write_line("#endif", True, True, files)
224  self.write_newline(files)
225 
226  def write_extern_c_bottom(self, files=2) :
227  self.write_line("#ifdef __cplusplus", True, True, files)
228  self.write_line("}", True, True, files)
229  self.write_line("#endif", True, True, files)
230  self.write_newline(files)
231 
232  def write_newline(self, files=2) :
233  if files == 2 or files == 0 :
234  self.file.write('\n')
235  if files == 2 or files == 1 :
236  if self.additional_filename_exists :
237  self.additional_file.write('\n')
238 
239  def write_define_status(self, files=2) :
240  self.write_line("vx_status status = (vx_status)VX_SUCCESS;", True, True, files)
241 
242  def write_if_status(self, files=2) :
243  self.write_line("if (status == (vx_status)VX_SUCCESS)", True, True, files)
244 
245