11
11
import imp
12
12
import io
13
13
class preprocessor :
14
- def __init__ (self , inFile = sys .argv [0 ], outFile = '' ,
15
- defines = [], removeMeta = False , escapeChar = None , mode = None , escape = '#' , run = True , resume = False , save = True ):
14
+ def __init__ (self , inFile = sys .argv [0 ], outFile = '' , defines = [], \
15
+ removeMeta = False , escapeChar = None , mode = None , escape = '#' , \
16
+ run = True , resume = False , save = True ):
16
17
# public variables
17
18
self .defines = defines
18
19
self .input = inFile
@@ -40,28 +41,28 @@ def deprecation(message):
40
41
warnings .simplefilter ('always' , DeprecationWarning )
41
42
warnings .warn (message , DeprecationWarning )
42
43
warnings .simplefilter ('default' , DeprecationWarning )
43
- if ( self .escapeChar != None ) :
44
+ if self .escapeChar != None :
44
45
deprecation ("'pypreprocessor.escapeChar' is deprecated. Use 'escape' instead." )
45
- if ( self .escape == '#' ) :
46
+ if self .escape == '#' :
46
47
self .escape = self .escapeChar
47
- if ( self .mode != None ) :
48
+ if self .mode != None :
48
49
msg = "'pypreprocessor.mode' is deprecated. Use 'run/resume/save' options instead."
49
- if ( self .run != True or self .resume != False or self .save != True ) :
50
+ if self .run != True or self .resume != False or self .save != True :
50
51
msg += " Ignoring 'pypreprocessor.mode'."
51
52
else :
52
- if ( self .mode .lower () == 'run' ) :
53
+ if self .mode .lower () == 'run' :
53
54
self .run = True
54
55
self .resume = False
55
56
self .save = False
56
- elif ( self .mode .lower () == 'pp' ) :
57
+ elif self .mode .lower () == 'pp' :
57
58
self .run = False
58
59
self .resume = False
59
60
self .save = True
60
- elif ( self .mode .lower () == 'ppcont' ) :
61
+ elif self .mode .lower () == 'ppcont' :
61
62
self .run = False
62
63
self .resume = True
63
64
self .save = True
64
- elif ( self .mode is not None ) :
65
+ elif self .mode is not None :
65
66
print ('Unknown mode : ' + str (self .mode ))
66
67
deprecation (msg )
67
68
@@ -77,7 +78,7 @@ def __reset_internal(self):
77
78
# the #define directive
78
79
def define (self , define ):
79
80
self .defines .append (define )
80
-
81
+
81
82
# the #undef directive
82
83
def undefine (self , define ):
83
84
# re-map the defines list excluding the define specified in the args
@@ -89,12 +90,12 @@ def search_defines(self, define):
89
90
return True
90
91
else :
91
92
return False
92
-
93
- #returning: validness of #ifdef #else block
93
+
94
+ #returning: validness of #ifdef #else block
94
95
def __if (self ):
95
96
value = bool (self .__ifblocks )
96
97
for ib in self .__ifblocks :
97
- value *= ib #* represents and: value = value and ib
98
+ value *= ib #* represents and: value = value and ib
98
99
return not value #not: because True means removing
99
100
100
101
# evaluate
@@ -133,55 +134,57 @@ def lexer(self, line):
133
134
self .exit_error (self .escape + 'endexclude' )
134
135
else :
135
136
self .__excludeblock = False
136
- return False , True
137
+ return False , True
137
138
# handle #ifnotdef directives (is the same as: #ifdef X #else)
138
139
elif line [:len (self .escape ) + 8 ] == self .escape + 'ifdefnot' :
139
140
if len (line .split ()) != 2 :
140
141
self .exit_error (self .escape + 'ifdefnot' )
141
142
else :
142
- self .__ifblocks .append (not ( self .search_defines (line .split ()[1 ]) ))
143
- self .__ifconditions .append (line .split ()[1 ])
143
+ self .__ifblocks .append (not self .search_defines (line .split ()[1 ]))
144
+ self .__ifconditions .append (line .split ()[1 ])
144
145
return False , True
145
146
# handle #ifdef directives
146
147
elif line [:len (self .escape ) + 5 ] == self .escape + 'ifdef' :
147
148
if len (line .split ()) != 2 :
148
149
self .exit_error (self .escape + 'ifdef' )
149
150
else :
150
151
self .__ifblocks .append (self .search_defines (line .split ()[1 ]))
151
- self .__ifconditions .append (line .split ()[1 ])
152
+ self .__ifconditions .append (line .split ()[1 ])
152
153
return False , True
153
154
# handle #else...
154
155
# handle #elseif directives
155
156
elif line [:len (self .escape ) + 6 ] == self .escape + 'elseif' :
156
157
if len (line .split ()) != 2 :
157
158
self .exit_error (self .escape + 'elseif' )
158
159
else :
159
- self .__ifblocks [- 1 ]= not (self .__ifblocks [- 1 ])#self.search_defines(self.__ifconditions[-1]))
160
+ self .__ifblocks [- 1 ] = not self .__ifblocks [- 1 ]
161
+ #self.search_defines(self.__ifconditions[-1]))
160
162
self .__ifblocks .append (self .search_defines (line .split ()[1 ]))
161
- self .__ifconditions .append (line .split ()[1 ])
162
- return False , True
163
+ self .__ifconditions .append (line .split ()[1 ])
164
+ return False , True
163
165
# handle #else directives
164
166
elif line [:len (self .escape ) + 4 ] == self .escape + 'else' :
165
167
if len (line .split ()) != 1 :
166
168
self .exit_error (self .escape + 'else' )
167
169
else :
168
- self .__ifblocks [- 1 ]= not (self .__ifblocks [- 1 ])#self.search_defines(self.__ifconditions[-1]))
169
- return False , True
170
- # handle #endif..
170
+ self .__ifblocks [- 1 ] = not self .__ifblocks [- 1 ]
171
+ #self.search_defines(self.__ifconditions[-1]))
172
+ return False , True
173
+ # handle #endif..
171
174
# handle #endififdef
172
175
elif line [:len (self .escape ) + 10 ] == self .escape + 'endififdef' :
173
176
if len (line .split ()) != 2 :
174
177
self .exit_error (self .escape + 'endififdef' )
175
178
else :
176
- if len (self .__ifconditions )>= 1 :
179
+ if len (self .__ifconditions ) >= 1 :
177
180
self .__ifblocks .pop (- 1 )
178
- self .__ifcondition = self .__ifconditions .pop (- 1 )
179
- else :
181
+ self .__ifcondition = self .__ifconditions .pop (- 1 )
182
+ else :
180
183
self .__ifblocks = []
181
184
self .__ifconditions = []
182
185
self .__ifblocks .append (self .search_defines (line .split ()[1 ]))
183
- self .__ifconditions .append (line .split ()[1 ])
184
- return False , True
186
+ self .__ifconditions .append (line .split ()[1 ])
187
+ return False , True
185
188
# handle #endifall directives
186
189
elif line [:len (self .escape ) + 8 ] == self .escape + 'endifall' :
187
190
if len (line .split ()) != 1 :
@@ -190,26 +193,27 @@ def lexer(self, line):
190
193
self .__ifblocks = []
191
194
self .__ifconditions = []
192
195
return False , True
193
- # handle #endif and #endif numb directives
196
+ # handle #endif and #endif numb directives
194
197
elif line [:len (self .escape ) + 5 ] == self .escape + 'endif' :
195
198
if len (line .split ()) != 1 :
196
199
self .exit_error (self .escape + 'endif number' )
197
200
else :
198
201
try :
199
- number = int (line [6 :])
202
+ number = int (line [6 :])
200
203
except ValueError as VE :
201
204
#print('ValueError',VE)
202
205
#self.exit_error(self.escape + 'endif number')
203
- number = 1
204
- if len (self .__ifconditions )> number :
205
- for i in range (0 ,number ):
206
+ number = 1
207
+ if len (self .__ifconditions ) > number :
208
+ for i in range (0 , number ):
206
209
self .__ifblocks .pop (- 1 )
207
- self .__ifcondition = self .__ifconditions .pop (- 1 )
210
+ self .__ifcondition = self .__ifconditions .pop (- 1 )
208
211
elif len (self .__ifconditions ) == number :
209
212
self .__ifblocks = []
210
213
self .__ifconditions = []
211
214
else :
212
- print ('Warning try to remove more blocks than present' , self .input , self .__linenum )
215
+ print ('Warning try to remove more blocks than present' , \
216
+ self .input , self .__linenum )
213
217
self .__ifblocks = []
214
218
self .__ifconditions = []
215
219
return False , True
@@ -218,11 +222,11 @@ def lexer(self, line):
218
222
if self .__excludeblock is True :
219
223
return True , False
220
224
# process the ifblock
221
- elif self .__ifblocks : # is True:
225
+ elif self .__ifblocks : # is True:
222
226
return self .__if (), False
223
227
#here can add other stuff for deleting comnments eg
224
228
else :
225
- return False , False
229
+ return False , False
226
230
227
231
# error handling
228
232
def exit_error (self , directive ):
@@ -244,7 +248,7 @@ def parse(self):
244
248
self .__reset_internal ()
245
249
self .check_deprecation ()
246
250
# open the input file
247
- input_file = io .open (os .path .join (self .input ),'r' , encoding = self .readEncoding )
251
+ input_file = io .open (os .path .join (self .input ), 'r' , encoding = self .readEncoding )
248
252
try :
249
253
# process the input file
250
254
for line in input_file :
@@ -256,7 +260,7 @@ def parse(self):
256
260
if metaData is True or squelch is True :
257
261
continue
258
262
if squelch is True :
259
- if ( metaData ) :
263
+ if metaData :
260
264
self .__outputBuffer += self .escape + line
261
265
else :
262
266
self .__outputBuffer += self .escape [0 ] + line
@@ -268,21 +272,21 @@ def parse(self):
268
272
input_file .close ()
269
273
#Warnings for unclosed #ifdef blocks
270
274
if self .__ifblocks :
271
- print ('Warning: Number of unclosed Ifdefblocks: ' ,len (self .__ifblocks ))
275
+ print ('Warning: Number of unclosed Ifdefblocks: ' , len (self .__ifblocks ))
272
276
print ('Can cause unwished behaviour in the preprocessed code, preprocessor is safe' )
273
277
try :
274
278
select = input ('Do you want more Information? ' )
275
279
except SyntaxError :
276
280
select = 'no'
277
281
select = select .lower ()
278
- if select in ('yes' ,'true' ,'y' ,'1' ):
279
- print ('Name of input and output file: ' ,self .input ,' ' ,self .output )
282
+ if select in ('yes' , 'true' , 'y' , '1' ):
283
+ print ('Name of input and output file: ' , self .input , ' ' , self .output )
280
284
for i , item in enumerate (self .__ifconditions ):
281
285
if (item in self .defines ) != self .__ifblocks [i ]:
282
286
cond = ' else '
283
287
else :
284
288
cond = ' if '
285
- print ('Block:' ,item , ' is in condition: ' ,cond )
289
+ print ('Block:' , item , ' is in condition: ' , cond )
286
290
self .post_process ()
287
291
288
292
# post-processor
@@ -298,17 +302,17 @@ def post_process(self):
298
302
finally :
299
303
output_file .close ()
300
304
301
- if ( self .run ) :
305
+ if self .run :
302
306
# if this module is loaded as a library override the import
303
307
if imp .lock_held () is True :
304
308
self .override_import ()
305
309
else :
306
310
self .on_the_fly ()
307
- if ( not self .save ) :
311
+ if not self .save :
308
312
# remove tmp file
309
- if ( os .path .exists (self .output ) ):
313
+ if os .path .exists (self .output ):
310
314
os .remove (self .output )
311
- if ( not self .resume ) :
315
+ if not self .resume :
312
316
# break execution so python doesn't
313
317
# run the rest of the pre-processed code
314
318
sys .exit (0 )
@@ -331,10 +335,10 @@ def override_import(self):
331
335
# postprocessor - on-the-fly execution
332
336
def on_the_fly (self ):
333
337
try :
334
- f = io .open (self .output ,"r" , encoding = self .readEncoding )
338
+ f = io .open (self .output , "r" , encoding = self .readEncoding )
335
339
exec (f .read ())
336
340
f .close ()
337
341
except :
338
342
self .rewrite_traceback ()
339
343
340
- pypreprocessor = preprocessor ()
344
+ pypreprocessor = preprocessor ()
0 commit comments