-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_help.py
executable file
·96 lines (61 loc) · 2.1 KB
/
make_help.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
import os
import re
import sys
class TargetHelp:
def __init__(self, name):
self._name = name
self._help_lines = []
def add_line(self, line):
self._help_lines.append(line)
def get_result(self, max_target_width, output):
template = ' %-{}s %s\n'.format(max_target_width)
if self._help_lines:
output.write(template % (self._name, self._help_lines[0]))
for line in self._help_lines[1:]:
output.write(template % ('', line))
output.write('\n')
class HelpBuilder:
def __init__(self):
self._help = []
self._max_target_width = 0
def found_target(self, name):
self._max_target_width = max(len(name), self._max_target_width)
self._help.append(TargetHelp(name))
def found_help(self, line):
self._help[-1].add_line(line)
def get_result(self, output):
for target_help in self._help:
target_help.get_result(self._max_target_width, output)
class HelpLineDirector:
def __init__(self, _help):
self._help = _help
def add_line(self, line):
match = re.match(r'^\t+@## *(.+) *', line)
if match:
self._help.found_help(match.group(1))
return self
return TargetLineDirector(self._help)
class TargetLineDirector:
def __init__(self, _help):
self._help = _help
def add_line(self, line):
match = re.match(r'^([a-zA-Z_-]+):', line)
if match:
self._help.found_target(match.group(1))
return HelpLineDirector(self._help)
return self
class HelpDirector:
def __init__(self, builder):
self._director = TargetLineDirector(builder)
def build(self, lines):
for line in lines:
self._director = self._director.add_line(line)
def main():
makefile = os.path.join(os.path.dirname(__file__), 'Makefile')
builder = HelpBuilder()
director = HelpDirector(builder)
director = director.build(open(makefile, 'r'))
builder.get_result(sys.stdout)
if __name__ == "__main__":
main()