tests: replace pycodestyle with black
[vpp.git] / docs / _scripts / filter_h.py
1 #!/usr/bin/env python3
2 # Copyright (c) 2016 Comcast Cable Communications Management, LLC.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Filter for .c files to make various preprocessor tricks Doxygenish
17
18 import os
19 import re
20 import sys
21
22 if len(sys.argv) < 2:
23     sys.stderr.write("Usage: %s <filename>\n" % (sys.argv[0]))
24     sys.exit(1)
25
26 replace_patterns = [
27     # Search for CLIB_PAD_FROM_TO(...); and replace with padding
28     # #define CLIB_PAD_FROM_TO(from,to) u8 pad_##from[(to) - (from)]
29     (
30         re.compile(
31             r"(?P<m>CLIB_PAD_FROM_TO)\s*[(](?P<from>[^,]+)," r"\s*(?P<to>[^)]+)[)]"
32         ),
33         r"/** Padding. */ u8 pad_\g<from>[(\g<to>) - (\g<from>)]",
34     ),
35 ]
36
37
38 filename = sys.argv[1]
39 cwd = os.getcwd()
40 if filename[0 : len(cwd)] == cwd:
41     filename = filename[len(cwd) :]
42     if filename[0] == "/":
43         filename = filename[1:]
44
45 with open(filename) as fd:
46     line_num = 0
47
48     for line in fd:
49         line_num += 1
50         str = line[:-1]  # filter \n
51
52         # Look for search/replace patterns
53         for p in replace_patterns:
54             str = p[0].sub(p[1], str)
55
56         sys.stdout.write(str + "\n")
57
58 # All done