Skip to content

Commit 5e8400a

Browse files
committed
lint pypreprocessor
1 parent 7936bcb commit 5e8400a

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

pypreprocessor/__init__.py

+54-50
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
import imp
1212
import io
1313
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):
1617
# public variables
1718
self.defines = defines
1819
self.input = inFile
@@ -40,28 +41,28 @@ def deprecation(message):
4041
warnings.simplefilter('always', DeprecationWarning)
4142
warnings.warn(message, DeprecationWarning)
4243
warnings.simplefilter('default', DeprecationWarning)
43-
if(self.escapeChar != None):
44+
if self.escapeChar != None:
4445
deprecation("'pypreprocessor.escapeChar' is deprecated. Use 'escape' instead.")
45-
if(self.escape == '#'):
46+
if self.escape == '#':
4647
self.escape = self.escapeChar
47-
if(self.mode != None):
48+
if self.mode != None:
4849
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:
5051
msg += " Ignoring 'pypreprocessor.mode'."
5152
else:
52-
if(self.mode.lower() == 'run'):
53+
if self.mode.lower() == 'run':
5354
self.run = True
5455
self.resume = False
5556
self.save = False
56-
elif(self.mode.lower() == 'pp'):
57+
elif self.mode.lower() == 'pp':
5758
self.run = False
5859
self.resume = False
5960
self.save = True
60-
elif(self.mode.lower() == 'ppcont'):
61+
elif self.mode.lower() == 'ppcont':
6162
self.run = False
6263
self.resume = True
6364
self.save = True
64-
elif(self.mode is not None):
65+
elif self.mode is not None:
6566
print('Unknown mode : ' + str(self.mode))
6667
deprecation(msg)
6768

@@ -77,7 +78,7 @@ def __reset_internal(self):
7778
# the #define directive
7879
def define(self, define):
7980
self.defines.append(define)
80-
81+
8182
# the #undef directive
8283
def undefine(self, define):
8384
# re-map the defines list excluding the define specified in the args
@@ -89,12 +90,12 @@ def search_defines(self, define):
8990
return True
9091
else:
9192
return False
92-
93-
#returning: validness of #ifdef #else block
93+
94+
#returning: validness of #ifdef #else block
9495
def __if(self):
9596
value = bool(self.__ifblocks)
9697
for ib in self.__ifblocks:
97-
value*=ib #* represents and: value = value and ib
98+
value *= ib #* represents and: value = value and ib
9899
return not value #not: because True means removing
99100

100101
# evaluate
@@ -133,55 +134,57 @@ def lexer(self, line):
133134
self.exit_error(self.escape + 'endexclude')
134135
else:
135136
self.__excludeblock = False
136-
return False, True
137+
return False, True
137138
# handle #ifnotdef directives (is the same as: #ifdef X #else)
138139
elif line[:len(self.escape) + 8] == self.escape + 'ifdefnot':
139140
if len(line.split()) != 2:
140141
self.exit_error(self.escape + 'ifdefnot')
141142
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])
144145
return False, True
145146
# handle #ifdef directives
146147
elif line[:len(self.escape) + 5] == self.escape + 'ifdef':
147148
if len(line.split()) != 2:
148149
self.exit_error(self.escape + 'ifdef')
149150
else:
150151
self.__ifblocks.append(self.search_defines(line.split()[1]))
151-
self.__ifconditions.append(line.split()[1])
152+
self.__ifconditions.append(line.split()[1])
152153
return False, True
153154
# handle #else...
154155
# handle #elseif directives
155156
elif line[:len(self.escape) + 6] == self.escape + 'elseif':
156157
if len(line.split()) != 2:
157158
self.exit_error(self.escape + 'elseif')
158159
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]))
160162
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
163165
# handle #else directives
164166
elif line[:len(self.escape) + 4] == self.escape + 'else':
165167
if len(line.split()) != 1:
166168
self.exit_error(self.escape + 'else')
167169
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..
171174
# handle #endififdef
172175
elif line[:len(self.escape) + 10] == self.escape + 'endififdef':
173176
if len(line.split()) != 2:
174177
self.exit_error(self.escape + 'endififdef')
175178
else:
176-
if len(self.__ifconditions)>=1:
179+
if len(self.__ifconditions) >= 1:
177180
self.__ifblocks.pop(-1)
178-
self.__ifcondition=self.__ifconditions.pop(-1)
179-
else:
181+
self.__ifcondition = self.__ifconditions.pop(-1)
182+
else:
180183
self.__ifblocks = []
181184
self.__ifconditions = []
182185
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
185188
# handle #endifall directives
186189
elif line[:len(self.escape) + 8] == self.escape + 'endifall':
187190
if len(line.split()) != 1:
@@ -190,26 +193,27 @@ def lexer(self, line):
190193
self.__ifblocks = []
191194
self.__ifconditions = []
192195
return False, True
193-
# handle #endif and #endif numb directives
196+
# handle #endif and #endif numb directives
194197
elif line[:len(self.escape) + 5] == self.escape + 'endif':
195198
if len(line.split()) != 1:
196199
self.exit_error(self.escape + 'endif number')
197200
else:
198201
try:
199-
number=int(line[6:])
202+
number = int(line[6:])
200203
except ValueError as VE:
201204
#print('ValueError',VE)
202205
#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):
206209
self.__ifblocks.pop(-1)
207-
self.__ifcondition=self.__ifconditions.pop(-1)
210+
self.__ifcondition = self.__ifconditions.pop(-1)
208211
elif len(self.__ifconditions) == number:
209212
self.__ifblocks = []
210213
self.__ifconditions = []
211214
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)
213217
self.__ifblocks = []
214218
self.__ifconditions = []
215219
return False, True
@@ -218,11 +222,11 @@ def lexer(self, line):
218222
if self.__excludeblock is True:
219223
return True, False
220224
# process the ifblock
221-
elif self.__ifblocks: # is True:
225+
elif self.__ifblocks: # is True:
222226
return self.__if(), False
223227
#here can add other stuff for deleting comnments eg
224228
else:
225-
return False, False
229+
return False, False
226230

227231
# error handling
228232
def exit_error(self, directive):
@@ -244,7 +248,7 @@ def parse(self):
244248
self.__reset_internal()
245249
self.check_deprecation()
246250
# 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)
248252
try:
249253
# process the input file
250254
for line in input_file:
@@ -256,7 +260,7 @@ def parse(self):
256260
if metaData is True or squelch is True:
257261
continue
258262
if squelch is True:
259-
if(metaData):
263+
if metaData:
260264
self.__outputBuffer += self.escape + line
261265
else:
262266
self.__outputBuffer += self.escape[0] + line
@@ -268,21 +272,21 @@ def parse(self):
268272
input_file.close()
269273
#Warnings for unclosed #ifdef blocks
270274
if self.__ifblocks:
271-
print('Warning: Number of unclosed Ifdefblocks: ',len(self.__ifblocks))
275+
print('Warning: Number of unclosed Ifdefblocks: ', len(self.__ifblocks))
272276
print('Can cause unwished behaviour in the preprocessed code, preprocessor is safe')
273277
try:
274278
select = input('Do you want more Information? ')
275279
except SyntaxError:
276280
select = 'no'
277281
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)
280284
for i, item in enumerate(self.__ifconditions):
281285
if (item in self.defines) != self.__ifblocks[i]:
282286
cond = ' else '
283287
else:
284288
cond = ' if '
285-
print('Block:',item, ' is in condition: ',cond )
289+
print('Block:', item, ' is in condition: ', cond)
286290
self.post_process()
287291

288292
# post-processor
@@ -298,17 +302,17 @@ def post_process(self):
298302
finally:
299303
output_file.close()
300304

301-
if(self.run):
305+
if self.run:
302306
# if this module is loaded as a library override the import
303307
if imp.lock_held() is True:
304308
self.override_import()
305309
else:
306310
self.on_the_fly()
307-
if(not self.save):
311+
if not self.save:
308312
# remove tmp file
309-
if(os.path.exists(self.output)):
313+
if os.path.exists(self.output):
310314
os.remove(self.output)
311-
if(not self.resume):
315+
if not self.resume:
312316
# break execution so python doesn't
313317
# run the rest of the pre-processed code
314318
sys.exit(0)
@@ -331,10 +335,10 @@ def override_import(self):
331335
# postprocessor - on-the-fly execution
332336
def on_the_fly(self):
333337
try:
334-
f = io.open(self.output,"r", encoding=self.readEncoding)
338+
f = io.open(self.output, "r", encoding=self.readEncoding)
335339
exec(f.read())
336340
f.close()
337341
except:
338342
self.rewrite_traceback()
339343

340-
pypreprocessor = preprocessor()
344+
pypreprocessor = preprocessor()

0 commit comments

Comments
 (0)