cbr-schedule

An unofficial Bible-reading-plan generator
git clone git://git.danielmoch.com/cbr-schedule.git
Log | Files | Refs | README | LICENSE

commit c43fb61ee66c81b6cf28ea89cbcaa1235c6fe24a
parent 3b8afdabcab2f058d1f10892b7b510729d2301cd
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Sat, 24 Feb 2018 21:56:35 -0500

Add Schedule class

Class is responsible for taking the data generated by the ReadingPlan
and exporting it to a file in one of a number of supported formats.

Currently supported formats:
1. JSON
2. Text file

Formats on the future plan:
1. CSV
2. LaTeX

Diffstat:
Mgenerate_schedule | 22++++++++++++++++------
Mreadingplan.py | 2+-
Aschedule.py | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+), 7 deletions(-)

diff --git a/generate_schedule b/generate_schedule @@ -23,14 +23,18 @@ # SOFTWARE. # from readingplan import ReadingPlan +from schedule import Schedule from sys import argv import configparser def print_usage(): - print('Usage: generate_schedule [config]') + print('Usage: generate_schedule filename [config]') + print('\tfilename - File name to export schedule to. Export type is') + print('\t determined from extension. Supported types are:') + print('\t\tJSON (ext: .json)') print('\tconfig - Path to schedule config file (default: schedule.ini)') -def generate_schedule(configfile='schedule.ini'): +def generate_schedule(filename, configfile='schedule.ini'): """ Generate the reading plan, using the ReadingPlan class as a generator. This function takes the day-of-week that January 1st @@ -40,14 +44,20 @@ def generate_schedule(configfile='schedule.ini'): config.read(configfile) plan = ReadingPlan() plan.build_plan(config) + schedule = Schedule() + schedule.filename = filename while not plan.done: - print(plan.next_reading()) + schedule.data.append(plan.next_reading()) + schedule.export() if __name__ == "__main__": - if len(argv) > 2: + if len(argv) > 3: print_usage() exit(1) elif len(argv) < 2: - generate_schedule() - else: + print_usage() + exit(1) + elif len(argv) == 2: generate_schedule(argv[1]) + else: + generate_schedule(argv[1], argv[2]) diff --git a/readingplan.py b/readingplan.py @@ -189,7 +189,7 @@ def next_reading(self): for reading in self._readings(today_sections_fallback): readings.append(reading) results = { - 'day_of_week': self._day_of_week, + 'day_of_week': DAYS_OF_WEEK[self._day_of_week], 'day': self._current_day, 'month': MONTHS[self._current_month][0], 'readings': readings diff --git a/schedule.py b/schedule.py @@ -0,0 +1,66 @@ +# +# The MIT License (MIT) +# +# Copyright (c) 2018 Daniel Moch +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +import json + +class Schedule: + data = [] + filename = None + + def _assert_initialized(self): + if self.filename is None: + raise AssertionError('Schedule: filename not initialized') + if self.data == []: + raise AssertionError('Schedule: data not initialized') + + def _json_export(self): + self._assert_initialized() + with open(self.filename, 'w') as outfile: + json.dump(self.data, outfile) + + def _txt_export(self): + self._assert_initialized() + with open(self.filename, 'w') as outfile: + for entry in self.data: + line = entry['day_of_week'] + ', ' + entry['month'] + \ + ' ' + str(entry['day']) + ':\n' + outfile.write(line) + for reading in entry['readings']: + reading_str = '\t' + reading_str += reading['section'] + ': ' + reading_str += reading['book'] + ' ' + reading_str += reading['reading'] + '\n' + outfile.write(reading_str) + if entry['readings'] == []: + outfile.write('\tNo reading\n') + outfile.write('\n') + + def export(self): + file_extension = self.filename.split('.')[-1] + + if file_extension == 'json': + self._json_export() + elif file_extension == 'txt': + self._txt_export() + else: + raise RuntimeError('Schedule: cannot determine export type')