-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnormalize_include_guards.py
47 lines (32 loc) · 1 KB
/
normalize_include_guards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright © 2016 Martin Ueding <[email protected]>
import argparse
import re
pattern = re.compile(r'#ifndef (.*)\n#define \1\n')
def main():
options = _parse_args()
for filename in options.files:
normalize_file(filename)
def normalize_file(filename):
with open(filename) as f:
contents = f.read()
m = pattern.search(contents)
if m:
new_guard_var = re.sub(r'\W', '_', filename.upper())
print(m.group(1), new_guard_var)
new_contents = pattern.sub('#ifndef {0}\n#define {0}\n'.format(new_guard_var), contents)
with open(filename, 'w') as f:
f.write(new_contents)
def _parse_args():
'''
Parses the command line arguments.
:return: Namespace with arguments.
:rtype: Namespace
'''
parser = argparse.ArgumentParser(description='')
parser.add_argument('files', nargs='+')
options = parser.parse_args()
return options
if __name__ == '__main__':
main()