aboutsummaryrefslogtreecommitdiff
path: root/readingplan.py
diff options
context:
space:
mode:
Diffstat (limited to 'readingplan.py')
-rw-r--r--readingplan.py77
1 files changed, 61 insertions, 16 deletions
diff --git a/readingplan.py b/readingplan.py
index f8bd7a5..4386121 100644
--- a/readingplan.py
+++ b/readingplan.py
@@ -22,6 +22,7 @@
# SOFTWARE.
#
import json
+import os
DAYS_OF_WEEK = (
"Sunday",
@@ -48,6 +49,19 @@ MONTHS = (
('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 @@ class ReadingPlan:
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 @@ class ReadingPlan:
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 @@ class ReadingPlan:
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():