cbr-schedule

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

commit 36058b4e137587bbd33dee58f533128851e981b9
parent 999ac5581655a60146e0f788bca256142b28cdc1
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Mon, 26 Feb 2018 06:50:54 -0500

Refactor readingplan.py to improve messages

Some informational messages are now debug messages, which are only
printed if READINGPLAN_DEBUG exists in the environment. Others have been
made more user-friendly.

Diffstat:
Mreadingplan.py | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 61 insertions(+), 16 deletions(-)

diff --git a/readingplan.py b/readingplan.py @@ -22,6 +22,7 @@ # SOFTWARE. # import json +import os DAYS_OF_WEEK = ( "Sunday", @@ -48,6 +49,19 @@ ('December', 31,), ) +class ReadingPlanError(Exception): + """ + Exception thrown by the ReadingPlan class + + Attributes: + message - The error message + """ + message = None + + def __init__(self, message): + super().__init__() + self.message = message + class ReadingPlan: """ Once initialized, maintain state of the plan so the schedule @@ -65,11 +79,15 @@ class ReadingPlan: _reading_indices = {} done = False + @staticmethod + def _print_dbg(method, message): + if 'READINGPLAN_DEBUG' in os.environ.keys(): + print('ReadingPlan.' + method + ': ' + message) + def _general_init(self, config): jan1_day_of_week = config['general']['jan1_day_of_week'] for idx, day in enumerate(DAYS_OF_WEEK): if day.upper() == jan1_day_of_week.upper(): - print('Setting _day_of_week to ' + str(idx)) self._day_of_week = idx break @@ -79,7 +97,8 @@ def _days_sections_init(self, config, section): if day_key in config[section].keys(): self._days_sections[day] = config[section][day_key] else: - print('No readings planned for ' + day + 's') + self._print_dbg('_days_sections_init', 'No readings ' \ + + 'planned for ' + day + 's') self._days_sections[day] = None def _days_sections_fallback_init(self, config, section): @@ -89,20 +108,28 @@ def _days_sections_fallback_init(self, config, section): self._days_sections_fallback[day] = \ config[section][day_key] else: - print('No readings planned for ' + day + 's') + self._print_dbg('_days_sections_fallback_init', 'No ' \ + + 'fallback readings planned for ' + day + 's') self._days_sections_fallback[day] = None def _section_init(self, config, section): if 'name' in config[section].keys(): self._sections[section] = config[section]['name'] else: - print('No section name specified for ' + section) - print('Using ' + section + ' as name') + self._print_dbg('_section_init', 'No section name ' + \ + 'specified for ' + section) + self._print_dbg('_section_init', 'Using ' + section + \ + ' as name') self._sections[section] = section reading_dictionary = [] - with open('sections/' + section + '.json', 'r') as sfile: - reading_dictionary = json.load(sfile) - self._reading_dictionaries[section] = reading_dictionary + try: + with open('sections/' + section + '.json', 'r') as sfile: + reading_dictionary = json.load(sfile) + self._reading_dictionaries[section] = reading_dictionary + except FileNotFoundError as error: + raise ReadingPlanError(section + ' provided in ' + \ + 'configuration file, but ' + error.filename + \ + ' does not exist') if 'book' in config[section].keys(): book = config[section]['book'] if 'chapter' in config[section].keys(): @@ -118,14 +145,32 @@ def _section_init(self, config, section): self._reading_indices[section] = 0 def _print_plan_info(self): - print('_sections:' + str(self._sections.keys())) - print('_reading_dictionaries: ' + \ - str(self._reading_dictionaries.keys())) - print('_reading_indices: ' + str(self._reading_indices.values())) - print('_day_of_week: ' + str(self._day_of_week)) - print('_days_sections: ' + str(self._days_sections.keys())) - print('_days_sections_fallback: ' + - str(self._days_sections_fallback.values())) + message = 'Sections configured: ' + for section in self._sections.values(): + message += section + ', ' + message = message[0:-2] + print(message) + print('Jan. 1 configured to ' + DAYS_OF_WEEK[self._day_of_week]) + print('Configured daily readings:') + for day in self._days_sections.keys(): + if self._days_sections[day] is None: + print('\t' + day + ': No reading') + else: + message = '\t' + day + ': ' + for section in self._days_sections[day].split(','): + message += self._sections[section] + ', ' + message = message[0:-2] + print(message) + print('Configured fallback readings:') + for day in self._days_sections_fallback.keys(): + if self._days_sections_fallback[day] is None: + print('\t' + day + ': No reading') + else: + message = '\t' + day + ': ' + for section in self._days_sections_fallback[day].split(','): + message += self._sections[section] + ', ' + message = message[0:-2] + print(message) def build_plan(self, config): for section in config.sections():