diff options
author | ng0 <ng0@n0.is> | 2018-03-05 13:06:41 +0000 |
---|---|---|
committer | ng0 <ng0@n0.is> | 2018-03-05 13:06:41 +0000 |
commit | d036b626b8eea15f99d91faf309843936289fde7 (patch) | |
tree | 311fef01d5b460297c7af1b3906f44416099e121 /contrib | |
parent | 0be7fa64a64245f66fa46bcbb2fe298a931a8b4d (diff) | |
download | gnunet-d036b626b8eea15f99d91faf309843936289fde7.tar.gz gnunet-d036b626b8eea15f99d91faf309843936289fde7.zip |
contrib/find_typedes.py: flake8
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/find_typedefs.py | 160 |
1 files changed, 82 insertions, 78 deletions
diff --git a/contrib/find_typedefs.py b/contrib/find_typedefs.py index d6db597da..68f5c2782 100644 --- a/contrib/find_typedefs.py +++ b/contrib/find_typedefs.py | |||
@@ -3,93 +3,97 @@ import os | |||
3 | import re | 3 | import re |
4 | import sys | 4 | import sys |
5 | 5 | ||
6 | |||
6 | debug = False | 7 | debug = False |
7 | 8 | ||
8 | def get_td_from_function_signature (line, file, num): | 9 | |
9 | left_paren = line.find ('(') | 10 | def get_td_from_function_signature(line, file, num): |
10 | if left_paren > 0: | 11 | left_paren = line.find('(') |
11 | left_paren += 1 | 12 | if left_paren > 0: |
12 | line = line[left_paren:] | 13 | left_paren += 1 |
13 | right_paren = line.find (')') | 14 | li = line[left_paren:] |
14 | if right_paren > 0 and right_paren > left_paren and line[right_paren:].find ('(') >= 0: | 15 | right_paren = line.find(')') |
15 | fname = line[:right_paren] | 16 | if right_paren > 0 and right_paren > left_paren and line[right_paren:].find('(') >= 0: |
16 | fname = fname.lstrip (' ').lstrip ('*').lstrip (' ').rstrip (' ') | 17 | fname = line[:right_paren] |
17 | if len (fname) > 0: | 18 | fname = fname.lstrip(' ').lstrip('*').lstrip(' ').rstrip(' ') |
19 | if len(fname) > 0: | ||
20 | if debug: | ||
21 | print("from {0}:{1}".format(file, num)) | ||
22 | print("-T {0}".format(fname)) | ||
23 | |||
24 | |||
25 | def get_td_from_simple_type(line, file, num): | ||
26 | line = line.rstrip(' ').rstrip('\t').rstrip(' ').rstrip('\t') | ||
27 | right_space = line.rfind(' ') | ||
28 | right_tab = line.rfind('\t') | ||
29 | sep = right_tab if right_tab > right_space else right_space | ||
30 | sep += 1 | ||
31 | tname = line[sep:] | ||
32 | tname = tname.lstrip('*') | ||
33 | if len(tname) > 0: | ||
18 | if debug: | 34 | if debug: |
19 | print ("from {0}:{1}".format (file, num)) | 35 | print("from {0}:{1}".format(file, num)) |
20 | print ("-T {0}".format (fname)) | 36 | print("-T {0}".format(tname)) |
21 | 37 | ||
22 | def get_td_from_simple_type (line, file, num): | ||
23 | line = line.rstrip (' ').rstrip ('\t').rstrip (' ').rstrip ('\t') | ||
24 | right_space = line.rfind (' ') | ||
25 | right_tab = line.rfind ('\t') | ||
26 | sep = right_tab if right_tab > right_space else right_space | ||
27 | sep += 1 | ||
28 | tname = line[sep:] | ||
29 | tname = tname.lstrip ('*') | ||
30 | if len (tname) > 0: | ||
31 | if debug: | ||
32 | print ("from {0}:{1}".format (file, num)) | ||
33 | print ("-T {0}".format (tname)) | ||
34 | 38 | ||
35 | def find_typedefs (file): | 39 | def find_typedefs(file): |
36 | with open (file, 'rb') as f: | 40 | with open(file, 'rb') as f: |
37 | td = False | ||
38 | td_struct = False | ||
39 | td_level = 0 | ||
40 | td_line = [] | ||
41 | data = f.read () | ||
42 | for i, l in enumerate (data.splitlines (False)): | ||
43 | # Don't try to be too smart: only count lines that begin with 'typedef ' | ||
44 | l = l.rstrip (' ').rstrip ('\t') | ||
45 | if len (l) == 0: | ||
46 | continue | ||
47 | if not td: | ||
48 | if l[:8] != 'typedef ': | ||
49 | continue | ||
50 | else: | ||
51 | td = True | ||
52 | if l[8:].lstrip (' ').lstrip ('\t')[:6] == 'struct': | ||
53 | td_struct = True | ||
54 | if td_struct: | ||
55 | leftcbrace = l.find ('{') | ||
56 | if leftcbrace >= 0: | ||
57 | if td_level == 0: | ||
58 | td_line.append (l[:leftcbrace]) | ||
59 | l = l[leftcbrace + 1:] | ||
60 | td_level += 1 | ||
61 | rightcbrace = l.rfind ('}') | ||
62 | if rightcbrace >= 0: | ||
63 | td_level -= 1 | ||
64 | if td_level == 0: | ||
65 | td_line.append (l[rightcbrace + 1:]) | ||
66 | else: | ||
67 | td_line.append (l) | ||
68 | if len (l) > 0 and l[-1] == ';' and (not td_struct or td_level == 0): | ||
69 | td_line = ' '.join (td_line) | ||
70 | td_line = td_line[:-1] | ||
71 | if len (td_line) > 0: | ||
72 | if td_line[-1] == ')': | ||
73 | get_td_from_function_signature (td_line, file, i) | ||
74 | else: | ||
75 | get_td_from_simple_type (td_line, file, i) | ||
76 | td_line = [] | ||
77 | td = False | 41 | td = False |
78 | td_struct = False | 42 | td_struct = False |
79 | td_level = 0 | 43 | td_level = 0 |
80 | 44 | td_line = [] | |
45 | data = f.read() | ||
46 | for i, l in enumerate(data.splitlines(False)): | ||
47 | # Don't try to be too smart: only count lines that begin with 'typedef ' | ||
48 | l = l.rstrip(' ').rstrip('\t') | ||
49 | if len(l) == 0: | ||
50 | continue | ||
51 | if not td: | ||
52 | if l[:8] != 'typedef ': | ||
53 | continue | ||
54 | else: | ||
55 | td = True | ||
56 | if l[8:].lstrip(' ').lstrip('\t')[:6] == 'struct': | ||
57 | td_struct = True | ||
58 | if td_struct: | ||
59 | leftcbrace = l.find('{') | ||
60 | if leftcbrace >= 0: | ||
61 | if td_level == 0: | ||
62 | td_line.append(l[:leftcbrace]) | ||
63 | l = l[leftcbrace + 1:] | ||
64 | td_level += 1 | ||
65 | rightcbrace = l.rfind('}') | ||
66 | if rightcbrace >= 0: | ||
67 | td_level -= 1 | ||
68 | if td_level == 0: | ||
69 | td_line.append(l[rightcbrace + 1:]) | ||
70 | else: | ||
71 | td_line.append(l) | ||
72 | if len(l) > 0 and l[-1] == ';' and(not td_struct or td_level == 0): | ||
73 | td_line = ' '.join(td_line) | ||
74 | td_line = td_line[:-1] | ||
75 | if len(td_line) > 0: | ||
76 | if td_line[-1] == ')': | ||
77 | get_td_from_function_signature(td_line, file, i) | ||
78 | else: | ||
79 | get_td_from_simple_type(td_line, file, i) | ||
80 | td_line = [] | ||
81 | td = False | ||
82 | td_struct = False | ||
83 | td_level = 0 | ||
84 | |||
81 | 85 | ||
82 | def scan_dir (d): | 86 | def scan_dir(d): |
83 | for dirpath, dirs, files in os.walk (d): | 87 | for dirpath, dirs, files in os.walk(d): |
84 | for f in files: | 88 | for f in files: |
85 | if re.match (r'(?!lt_).+\.(c|cc|h)$', f): | 89 | if re.match(r'(?!lt_).+\.(c|cc|h)$', f): |
86 | file = os.path.join (dirpath, f) | 90 | file = os.path.join(dirpath, f) |
87 | find_typedefs (file) | 91 | find_typedefs(file) |
88 | 92 | ||
89 | 93 | ||
90 | if __name__ == '__main__': | 94 | if __name__ == '__main__': |
91 | if len (sys.argv[1:]) == 0: | 95 | if len(sys.argv[1:]) == 0: |
92 | arg = os.getcwd () | 96 | arg = os.getcwd() |
93 | else: | 97 | else: |
94 | arg = sys.argv[1] | 98 | arg = sys.argv[1] |
95 | scan_dir (arg) \ No newline at end of file | 99 | scan_dir(arg) |