This utility has officially moved to the Chrysoberyl repo now.
Chris Pressey
6 years ago
0 | #!/usr/bin/env python | |
1 | ||
2 | from argparse import ArgumentParser | |
3 | import json | |
4 | import sys | |
5 | ||
6 | if __name__ == '__main__': | |
7 | argparser = ArgumentParser() | |
8 | argparser.add_argument('filenames', metavar='FILENAME', type=str, nargs='+', | |
9 | help='The files to put in the JSON. If a file is listed ' | |
10 | 'more than once, it will only be included the first time.') | |
11 | argparser.add_argument('-v', '--varname', metavar='VARNAME', type=str, default='files', | |
12 | help='The Javascript variable to assign the JSON value to.') | |
13 | options = argparser.parse_args(sys.argv[1:]) | |
14 | ||
15 | seen = set() | |
16 | data = [] | |
17 | ||
18 | for filename in options.filenames: | |
19 | if filename in seen: | |
20 | continue | |
21 | seen.add(filename) | |
22 | with open(filename, 'r') as f: | |
23 | data.append([filename, f.read()]) | |
24 | ||
25 | sys.stdout.write("""\ | |
26 | {} = {}; | |
27 | """.format(options.varname, json.dumps(data, indent=4, sort_keys=True))) |