-
Notifications
You must be signed in to change notification settings - Fork 19
/
discover_numerics
executable file
·83 lines (68 loc) · 3.59 KB
/
discover_numerics
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# numerics discovery for irc-defs files
# Written by Daniel Oaks <[email protected]>, under CC0 license
"""discover_numerics - Feed this a numerics.h file or something similar, and
this will discover which numerics exist in that file but do not exist in our
numerics list.
Usage:
discover_numerics search <numeric.h>... [--pedantic] [--yaml <datafile>]
discover_numerics search-dir <directory> [--pedantic] [--yaml <datafile>]
discover_numerics (-h | --help)
Options:
--pedantic Complain about more things than usual
--yaml <datafile> Path to the numerics.yaml file [default: _data/numerics.yaml]
"""
__version__ = '0.1.0'
import glob
import re
import yaml
from docopt import docopt
RE_IRC2_INSP_NUMERIC_H = re.compile(r'^\s*(?P<name>(?:ERR_|RPL_)[0-9A-Z_]+)\s+=\s+(?P<numeric>[0-9]+)')
RE_CHARY_NUMERIC_H = re.compile(r'^#define\s+(?P<name>(?:ERR_|RPL_)[0-9A-Z_]+)\s+(?P<numeric>[0-9]+)')
RE_INSP_NUMERIC_ALONE = re.compile(r'(?P<name>)(?:AddRow|Write(?:Remote)?Numeric)\((?P<numeric>[0-9]+)[^0-9]')
if __name__ == '__main__':
arguments = docopt(__doc__, version=__version__)
files = []
if arguments['search']:
files = arguments['<numeric.h>']
elif arguments['search-dir']:
for ext in [ 'c', 'cpp', 'h', 'hpp' ]:
files += glob.glob("{base}/**/*.{ext}".format(base=arguments['<directory>'], ext=ext), recursive=True)
if files:
data = yaml.safe_load(open(arguments['--yaml'], 'r').read())
for numerics_filename in files:
lines = None
try:
lines = open(numerics_filename, 'r').read().split('\n')
except UnicodeDecodeError:
# skip binary files
continue
for line in lines:
matches = RE_IRC2_INSP_NUMERIC_H.findall(line) + RE_CHARY_NUMERIC_H.findall(line) + RE_INSP_NUMERIC_ALONE.findall(line)
for match in matches:
name, numeric = match
numeric = numeric.zfill(3)
found = False
stdname = None
for val in data['values']:
if name:
if val['name'] == name and val['numeric'] == numeric:
found = True
elif val['numeric'] == numeric and name in val.get('comment', ''):
found = True
stdname = val['name']
else:
# if there's no name we can only check by numeric, dodgy but it's all we can do.
if val['numeric'] == numeric:
found = True
stdname = val['name']
if not found:
if name:
print('Could not find numeric {name} ({numeric}): {file}'.format(name=name, numeric=numeric, file=numerics_filename))
else:
print('Could not find numeric {numeric}: {file}'.format(numeric=numeric, file=numerics_filename))
elif arguments['--pedantic']:
if not name:
print('{stdname} ({numeric}) is being used without a name: {file}'.format(stdname=stdname, numeric=numeric, file=numerics_filename))
elif stdname:
print('{stdname} ({numeric}) is being used with a non-standard name ({name}): {file}'.format(stdname=stdname, numeric=numeric, name=name, file=numerics_filename))