|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | 4 | """ |
5 | | - Script to generate README with pieces of source files |
| 5 | + Script to generate FILE with pieces of source files |
6 | 6 |
|
7 | | - :copyright: (c) 2015 Jauhien Piatlicki |
| 7 | + :copyright: (c) 2015-2016 Jauhien Piatlicki |
8 | 8 | :license: Apache-2.0 or MIT, see LICENSE-* for details |
9 | 9 | """ |
10 | 10 |
|
11 | 11 | import argparse |
12 | | -import re |
13 | 12 | import sys |
14 | 13 |
|
15 | | -def parse_source(source): |
16 | | - snippets = {} |
17 | | - active_snippets = set() |
18 | | - for line in source: |
19 | | - if line[0:3] == "//<": |
20 | | - active_snippets.update(line[3:].split()) |
21 | | - elif line[0:3] == "//>": |
22 | | - active_snippets = active_snippets.difference(set(line[3:].split())) |
23 | | - else: |
24 | | - join = False |
25 | | - w = False |
26 | | - if line[0:5] == "/*j*/": |
27 | | - line = line[5:] |
28 | | - join = True |
29 | | - elif line[0:6] == "/*jw*/": |
30 | | - line = line[6:] |
31 | | - join = True |
32 | | - w = True |
33 | | - if join: |
34 | | - line = line.lstrip() |
35 | | - if w: |
36 | | - line = " " + line |
37 | | - |
38 | | - for snippet in active_snippets: |
39 | | - try: |
40 | | - snippets[snippet].append(line) |
41 | | - except KeyError: |
42 | | - snippets[snippet] = [line] |
43 | | - |
44 | | - if join: |
45 | | - try: |
46 | | - snippets[snippet][-2] = snippets[snippet][-2].rstrip('\n') |
47 | | - except IndexError: |
48 | | - pass |
49 | | - |
50 | | - return snippets |
51 | | - |
52 | | -def parse_source_file(fname): |
53 | | - source = [] |
54 | | - with open(fname) as f: |
55 | | - source = f.readlines() |
56 | | - return parse_source(source) |
57 | | - |
58 | | -class Snippets(object): |
59 | | - __slots__ = ['snippets'] |
60 | | - |
61 | | - def __init__(self): |
62 | | - self.snippets = {} |
63 | | - |
64 | | - def get(self, fname, name): |
65 | | - snippets = {} |
66 | | - try: |
67 | | - snippets = self.snippets[fname] |
68 | | - except KeyError: |
69 | | - snippets = parse_source_file(fname) |
70 | | - self.snippets[fname] = snippets |
71 | | - return snippets[name] |
72 | | - |
73 | | -def process_readme(readme): |
74 | | - snippets = Snippets() |
75 | | - result = [] |
76 | | - regexp = re.compile('<<<(.*):(.*)>>>') |
77 | | - for line in readme: |
78 | | - match = regexp.match(line) |
79 | | - if match: |
80 | | - fname = match.group(1) |
81 | | - name = match.group(2) |
82 | | - result.extend(snippets.get(fname, name)) |
83 | | - else: |
84 | | - result.append(line) |
85 | | - return result |
| 14 | +import snippets |
86 | 15 |
|
87 | 16 | def main(): |
88 | | - parser = argparse.ArgumentParser("Generate README with code snippets") |
89 | | - parser.add_argument('readme_in', help='README template') |
90 | | - parser.add_argument('readme_out', help='README to be generated') |
| 17 | + parser = argparse.ArgumentParser("Generate FILE with code snippets") |
| 18 | + parser.add_argument('file_in', help='FILE template') |
| 19 | + parser.add_argument('file_out', help='FILE to be generated') |
91 | 20 | args = parser.parse_args() |
92 | | - readme_in = [] |
93 | | - with open(args.readme_in) as f: |
94 | | - readme_in = f.readlines() |
95 | | - readme = process_readme(readme_in) |
96 | | - with open(args.readme_out, 'w') as f: |
97 | | - f.write("".join(readme)) |
| 21 | + file_in = [] |
| 22 | + with open(args.file_in) as f: |
| 23 | + file_in = f.readlines() |
| 24 | + text = snippets.process(file_in) |
| 25 | + with open(args.file_out, 'w') as f: |
| 26 | + f.write("".join(text)) |
98 | 27 |
|
99 | 28 | if __name__ == "__main__": |
100 | 29 | sys.exit(main()) |
0 commit comments