1 | # |
2 | # The MIT License (MIT) |
3 | # |
4 | # Copyright (c) 2018 Daniel Moch |
5 | # |
6 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | # of this software and associated documentation files (the "Software"), to deal |
8 | # in the Software without restriction, including without limitation the rights |
9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | # copies of the Software, and to permit persons to whom the Software is |
11 | # furnished to do so, subject to the following conditions: |
12 | # |
13 | # The above copyright notice and this permission notice shall be included in all |
14 | # copies or substantial portions of the Software. |
15 | # |
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | # SOFTWARE. |
23 | # |
24 | import json |
25 | |
26 | class Schedule: |
27 | data = [] |
28 | filename = None |
29 | _year = "2019" |
30 | _column_section_mapping = { |
31 | 'Old Testament': ('Old Testament', 'Psalms',), |
32 | 'New Testament': ('New Testament',) |
33 | } |
34 | |
35 | def _assert_initialized(self): |
36 | if self.filename is None: |
37 | raise AssertionError('Schedule: filename not initialized') |
38 | if self.data == []: |
39 | raise AssertionError('Schedule: data not initialized') |
40 | |
41 | def _json_export(self, outfile): |
42 | json.dump(self.data, outfile) |
43 | |
44 | def _txt_export(self, outfile): |
45 | for entry in self.data: |
46 | line = entry['day_of_week'] + ', ' + entry['month'] + \ |
47 | ' ' + str(entry['day']) + ':\n' |
48 | outfile.write(line) |
49 | for reading in entry['readings']: |
50 | reading_str = '\t' |
51 | reading_str += reading['section'] + ': ' |
52 | reading_str += reading['book'] + ' ' |
53 | reading_str += reading['reading'] + '\n' |
54 | outfile.write(reading_str) |
55 | if entry['readings'] == []: |
56 | outfile.write('\tNo reading\n') |
57 | outfile.write('\n') |
58 | |
59 | def _csv_export(self, outfile): |
60 | outfile.write('"Date"') |
61 | for column in self._column_section_mapping: |
62 | outfile.write(',"' + column + '"') |
63 | outfile.write('\n') |
64 | for entry in self.data: |
65 | outfile.write('"' + entry['day_of_week'] + ', ' + \ |
66 | entry['month'] + ' ' + str(entry['day']) + '"') |
67 | for column in self._column_section_mapping.keys(): |
68 | for reading in entry['readings']: |
69 | if reading['section'] in \ |
70 | self._column_section_mapping[column]: |
71 | outfile.write(',"' + reading['book'] + ' ' + \ |
72 | reading['reading'] + '"') |
73 | outfile.write('\n') |
74 | |
75 | def _latex_export(self, outfile): |
76 | outfile.write('\\documentclass[a4paper]{article}\n\n') |
77 | outfile.write('\\pagenumbering{gobble}\n\n') |
78 | outfile.write('\\usepackage{longtable}\n\n') |
79 | outfile.write('\\usepackage{tabu}\n\n') |
80 | outfile.write('\\usepackage[margin=0.5in]{geometry}\n\n') |
81 | outfile.write('\\title{Bible Reading Plan}\n\n') |
82 | outfile.write('\\author{' + self._year + '}\n\n') |
83 | outfile.write('\\date{}\n\n') |
84 | outfile.write('\\begin{document}\n\n') |
85 | outfile.write('\\maketitle\n\n') |
86 | outfile.write('\\begin{longtabu} to \\linewidth{ l') |
87 | for col in range(len(self._column_section_mapping) - 1): |
88 | outfile.write(' X[2,l]') |
89 | outfile.write(' X[3,l] }\n\n') |
90 | outfile.write('\\rowfont\\bfseries Date') |
91 | for column in self._column_section_mapping: |
92 | outfile.write(' & ' + column) |
93 | outfile.write(' \\\\ \\hline\\endhead\n\n') |
94 | for entry in self.data: |
95 | outfile.write(entry['day_of_week'] + ', ' + entry['month'] \ |
96 | + ' ' + str(entry['day'])) |
97 | for column in self._column_section_mapping.keys(): |
98 | for reading in entry['readings']: |
99 | if reading['section'] in \ |
100 | self._column_section_mapping[column]: |
101 | outfile.write(' & ' + reading['book'] + ' ' + \ |
102 | reading['reading']) |
103 | outfile.write(' \\\\ \\hline\n') |
104 | outfile.write('\n\\end{longtabu}\n\n') |
105 | outfile.write('\\end{document}\n\n') |
106 | |
107 | def export(self): |
108 | self._assert_initialized() |
109 | file_extension = self.filename.split('.')[-1] |
110 | |
111 | with open(self.filename, 'w') as outfile: |
112 | if file_extension == 'json': |
113 | self._json_export(outfile) |
114 | elif file_extension == 'txt': |
115 | self._txt_export(outfile) |
116 | elif file_extension == 'csv': |
117 | self._csv_export(outfile) |
118 | elif file_extension == 'tex': |
119 | self._latex_export(outfile) |
120 | else: |
121 | raise RuntimeError('Schedule: cannot determine export type') |