Skip to content

Commit 40d0192

Browse files
author
Laurent Pinson
committed
Added quiet mode
1 parent f49ffa1 commit 40d0192

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,15 @@ pypreprocessor.run = True / False
182182
pypreprocessor.resume = True / False
183183
pypreprocessor.save = True / False
184184
pypreprocessor.overload = True / False
185+
pypreprocessor.quiet = True / False
185186
```
186187
set the options of the preprocessor:
187188

188189
* run: Run the preprocessed code if true. Default is true
189190
* resume: Return after a file is preprocessed and can preprocess a next file if true. Default is false
190191
* save: Save preprocessed code if true. Default is true
191192
* overload: Any defines added to the preprocessor will overload existing defines. Default is false
193+
* quiet: no warning about ununderstood directives or missing #indef
192194

193195
```python
194196
pypreprocessor.input = 'inputFile.py'

pypreprocessor/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def append(self, var):
2222
class preprocessor:
2323
def __init__(self, inFile=sys.argv[0], outFile='', defines={}, removeMeta=False,
2424
escapeChar=None, mode=None, escape='#', run=True, resume=False,
25-
save=True, overload=True):
25+
save=True, overload=True, quiet=False):
2626
# public variables
2727
self.defines = customDict()
2828
#support for <=0.7.7
@@ -42,6 +42,7 @@ def __init__(self, inFile=sys.argv[0], outFile='', defines={}, removeMeta=False,
4242
self.resume = resume
4343
self.save = save
4444
self.overload = overload
45+
self.quiet = quiet
4546
self.readEncoding = sys.stdin.encoding
4647
self.writeEncoding = sys.stdout.encoding
4748

@@ -303,8 +304,9 @@ def lexer(self, line):
303304
self.__ifblocks.pop(-1)
304305
self.__ifconditions.pop(-1)
305306
else:
306-
print('Warning trying to remove more blocks than present', \
307-
self.input, self.__linenum)
307+
if not self.quiet:
308+
print('Warning trying to remove more blocks than present',
309+
self.input, self.__linenum)
308310
self.__ifblocks = []
309311
self.__ifconditions = []
310312

@@ -313,8 +315,8 @@ def lexer(self, line):
313315
# escapechar + space ==> comment
314316
# starts with #!/ ==> shebang
315317
# else print warning
316-
if len(line.split()[0]) > 1 and not line.startswith('#!/'):
317-
print('Warning unknown directive or comment starting with ', \
318+
if len(line.split()[0]) > 1 and not line.startswith('#!/') and not self.quiet:
319+
print('Warning unknown directive or comment starting with ',
318320
line.split()[0], self.input, self.__linenum + 1)
319321

320322
return False, True
@@ -374,7 +376,7 @@ def parse(self):
374376
continue
375377
finally:
376378
#Warnings for unclosed #ifdef blocks
377-
if self.__ifblocks:
379+
if self.__ifblocks and not self.quiet:
378380
print('Warning: Number of unclosed Ifdefblocks: ', len(self.__ifblocks))
379381
print('Can cause unwished behaviour in the preprocessed code, preprocessor is safe')
380382
try:

pypreprocessor/__main__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818
parser.add_argument("-d", "--define", help="list of variable to define", nargs='*')
1919
parser.add_argument("-o", "--overload", help="overload variable definition in the file by those \
2020
provided by --define", action='store_true', default=False)
21+
parser.add_argument("-q", "--quiet", help="No warning on ununderstable directives and missign ending",
22+
action='store_true', default=False)
2123
parser.add_argument("input", help="input file.")
2224
parser.add_argument("output", nargs='?', help="output file. Default is <input_basename>_out.<input_extension>")
2325
args = parser.parse_args()
2426

2527
p=preprocessor(inFile=args.input, mode=None, removeMeta=args.removeMeta, escapeChar=None,
26-
run=args.run, resume=False, save=True, overload=args.overload)
28+
run=args.run, resume=False, save=True, overload=args.overload, quiet=args.quiet)
2729
if args.output:
2830
p.define = args.output
2931
if args.define:
3032
for elem in args.define:
3133
p.define(*elem.split(':'))
34+
self.__reset_internal() # not very pythonic
3235
if args.escape:
3336
p.escape = args.escape
3437

0 commit comments

Comments
 (0)