at master
1 | #!/usr/bin/env python3 |
2 | # |
3 | # The MIT License (MIT) |
4 | # |
5 | # Copyright (c) 2018 Daniel Moch |
6 | # |
7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | # of this software and associated documentation files (the "Software"), to deal |
9 | # in the Software without restriction, including without limitation the rights |
10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | # copies of the Software, and to permit persons to whom the Software is |
12 | # furnished to do so, subject to the following conditions: |
13 | # |
14 | # The above copyright notice and this permission notice shall be included in all |
15 | # copies or substantial portions of the Software. |
16 | # |
17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23 | # SOFTWARE. |
24 | # |
25 | import json |
26 | from sys import argv |
27 | |
28 | def generate_readings(infilename): |
29 | with open(infilename, 'r') as infile: |
30 | section = json.load(infile) |
31 | |
32 | filename = str.join('.', infilename.split('.')[0:-1]) + '.json' |
33 | readings = [] |
34 | idx = 0 |
35 | for book, info in section: |
36 | subchapter_readings = () |
37 | if len(info) > 1: |
38 | subchapter_readings = info[1] |
39 | chapters = info[0] |
40 | chapter = 1 |
41 | while chapter <= chapters: |
42 | chapter_str = str(chapter) |
43 | if chapter_str in subchapter_readings: |
44 | for subchapter_reading in \ |
45 | subchapter_readings[chapter_str]: |
46 | reading = {} |
47 | reading['index'] = idx |
48 | reading['book'] = book |
49 | reading['reading'] = chapter_str + ':' + \ |
50 | subchapter_reading |
51 | readings.append(reading) |
52 | idx += 1 |
53 | chapter += 1 |
54 | else: |
55 | reading = {} |
56 | reading['index'] = idx |
57 | reading['book'] = book |
58 | reading['reading'] = chapter_str |
59 | readings.append(reading) |
60 | idx += 1 |
61 | chapter += 1 |
62 | |
63 | with open(filename, 'w') as outfile: |
64 | json.dump(readings, outfile) |
65 | |
66 | def print_usage(): |
67 | print('Usage: generate_readings infile') |
68 | print('\tinfile: JSON-encoded input file') |
69 | |
70 | if __name__ == '__main__': |
71 | if len(argv) != 2: |
72 | print_usage() |
73 | exit(1) |
74 | else: |
75 | generate_readings(argv[1]) |