/
daily_reading.py
100 lines (83 loc) · 2.91 KB
/
daily_reading.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Get a daily reading.
Copyright (C) 2015 K. C. Juntunen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import json
import sys
import time
import datetime
def todays_date():
"""
Returns today's date as a string.
"""
now = datetime.datetime.now()
return "{}-{}-{}".format(now.year, now.month, now.day)
def day_of_year():
"""
Returns the day of the year as an integer.
"""
return int(time.strftime("%j"))
if __name__ == "__main__":
from optparse import OptionParser
prsr = OptionParser()
prsr.add_option("-p", "--plan"
, dest="json_file",
help="Reading plan; FILE in json format"
, metavar="FILE")
class DailyReading(object):
"""
Takes a json file, and can print a daily reading passage.
"""
def __init__(self, file_name):
self.json_file = file_name
try:
with open(self.json_file, "r") as jf:
self.schedule = json.load(jf)
except IOError as ioe:
sys.stderr.write("[Error {}]: Couldn't open {}\n".format(
ioe.errno, file_name))
return
def get_passages(self):
"""
Return a list containing todays passages.
"""
if self.schedule is not None:
return self.schedule.get("data2")[day_of_year() - 1]
else:
return
def print_passages(self):
"""
Prints a nicely formatted passage list.
"""
info = self.schedule.get("info")
date_string = todays_date()
hbar_length = len(info) + len(date_string) + 4
message = "{} -- {}\n{}\n{}\n".format(
self.schedule.get("info"),
date_string,
"=" * hbar_length,
", ".join(self.schedule.get("data2")[day_of_year() - 1]))
sys.stdout.writelines(message)
if __name__ == "__main__":
from argparse import ArgumentParser
prsr = ArgumentParser()
prsr.add_argument("-j", "--json", dest="json_file",
help="Reading plan; FILE in json format", metavar="FILE")
prsr.add_argument("-p", "--pretty", action="store_true")
args = prsr.parse_args()
pasg = DailyReading(args.json_file)
if args.pretty:
pasg.print_passages()
else:
for x in pasg.get_passages():
print(x)