This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample
229 lines (199 loc) · 7.31 KB
/
example
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- python -*-
# See LICENSE file for copyright and license details.
import time, pwd
from latex import *
from gnupg import *
from fortune import *
from events import *
from wotd import *
from leapsec import *
from solar import *
from summertime import *
from xkcd import *
doc = documentclass('memoir', '11pt', 'oneside', 'a4paper')
doc += fontencoding('T1')
doc += usepackage('inputenc', 'utf8')
doc += usepackage('xcolor')
doc += usepackage('geometry', margin = '1in')
doc += usepackage('microtype')
doc += usepackage('graphicx')
doc += usepackage('adjustbox', 'export')
doc += usepackage('ulem', 'normalem')
doc += pagestyle('empty')
doc += '\\newcommand{\\whiteonblack}[1]{\\colorbox{black}{\\textcolor{white}{#1}}}\n'
def whiteonblack(text, end = ''):
return '\\whiteonblack{%s}%s' % (text, end)
doc += begin('document')
prologue = doc
def standalonetest(content):
content = prologue + content + '\n' + end('document')
with open('stand-alone-test.tex', 'wb') as file:
file.write(content.encode('utf-8'))
file.flush()
try:
spawn('pdflatex', '-halt-on-error', '--', 'stand-alone-test.tex')
except:
return False
return True
# (prologue)
now = time.localtime()
holidays = filter_events(swedish_holidays(), 30) # include for the next 30 days
if holidays is None:
holidays = []
holiday_today = 2 if now.tm_wday == 6 else (1 if len([x for _, x, __ in holidays if x == 0]) > 0 else 0)
holiday_tomorrow = 2 if now.tm_wday == 5 else (1 if len([x for _, x, __ in holidays if x == 1]) > 0 else 0)
holiday_str = ('don\'t think so', 'believe so', 'yes')
## Title
doc += begin('center')
doc += Huge('Report of the day, %s' % time.strftime('%Y-(%m)%b-%d', now))
doc += end('center')
## Misc.
doc += '\n\n'
doc += '\\hspace*{-2em}'
doc += begin('tabular', 'll')
doc += ln(bf('Week:', ' & ' + time.strftime('%V', now)))
doc += ln(bf('Weekday:', ' & ' + time.strftime('%A', now)))
doc += ln(bf('Holiday today?', ' & ' + holiday_str[holiday_today]))
doc += ln(bf('Holiday tomorrow?', ' & ' + holiday_str[holiday_tomorrow]))
doc += end('tabular')
doc += '\n\n'
## GnuPG key-expiry
keys = gnupg_expiry() # for 60 days: gnupg_expiry(60)
if keys is not None and len(keys) > 0:
doc += sectionx(whiteonblack('GnuPG key-expiry'))
for key, days in keys:
text = '%s will expire in %i days' % (tt(key, ''), days)
if days <= 5:
text = '\\textcolor{red!75!black}{%s}' % text
doc += ln(text)
doc += '\n\n'
## Today's/Upcoming events, include switch from or to summer time
events = holidays
events += filter_events([('John Doe\'s birthday', '07-01')], 30) # first of july, warn 30 days before
events += filter_events(swedish_events())
events += filter_events(leap_seconds_to_strings(leap_seconds(), include_desc = True))
summer_time, date = is_summer_time()
for i in range(30):
(summer_time_then, date) = is_summer_time(i + 1)
if summer_time_then != summer_time:
tz = 'summer time' if summer_time_then else 'standard time'
events.append(('Switch to %s' % tz, i + 1, date))
break
events.sort(key = lambda x : x[1])
today = [e for e in events if e[1] == 0]
events = [e for e in events if e[1] > 0]
if len(today) > 0:
doc += sectionx(whiteonblack('Today\'s events'))
for event in today:
doc += ln(event[0])
if len(events) > 0:
doc += sectionx(whiteonblack('Upcoming events'))
for event, days, date in events:
unit = 'day' if days == 1 else 'days'
date = time.strftime('%Y-%m-%d', date)
text = 'In %i %s: %s (%s)' % (days, unit, event, date)
if days <= 5:
text = '\\textcolor{red!75!black}{%s}' % text
doc += ln(text)
## Fortune of the day
quote_tries = 100
while quote_tries > 0:
quote, quote_tries = fortune(max_tries = quote_tries)
if quote is not None:
quote = begin('verbatim') + quote + '\n' + end('verbatim')
if not standalonetest(quote):
continue
doc += sectionx(whiteonblack('Fortune of the day'))
doc += quote
doc += '\n\n'
break
## Word of the day
have_wotd = False
wday = time.localtime().tm_wday
for lang, wdays in [('en', None), ('sv', [0, 1])]:
if wdays is not None and wday not in wdays:
continue
word = wotd(lang = lang)
if word is not None:
if standalonetest(word):
if not have_wotd:
doc += sectionx(whiteonblack('Word of the day'))
have_wotd = True
doc += '\\noindent\n'
doc += word
doc += '\n\n'
## Solar events
try:
sol = Solar()
except:
sol = None
if sol is not None:
doc += sectionx(whiteonblack('Solar events'))
doc += '\\hspace*{-0.6em}'
doc += begin('tabular', 'llll')
doc += ln(bf('Summer or winter: ', ' & %s & &' % sol.season()))
doc += ln(bf('Next equinox: ', ' & %s & &' % sol.next_equinox()))
doc += ln(bf('Next solstice: ', ' & %s & &' % sol.next_solstice()))
elevs = ['Astronomical dawn', 'Nautical dawn', 'Civil dawn', 'Sunrise', 'Solar noon',
'Sunset', 'Civil dusk', 'Nautical dusk', 'Astronomical dusk']
n = lambda x : '—' if x is None else x
today, tomorrow = sol.elevations(0), sol.elevations(1)
for elev, today, tomorrow, length in zip(elevs, today, tomorrow, sol.lengths(today, tomorrow)):
doc += ln(bf('%s: ' % elev, ' & %s & %s & %s' % (n(today), n(tomorrow), length)))
gold = sol.golden_hour(0)
n = lambda x : '—' if x is None else x.split(' ')[1]
doc += ln(bf('Morning golden hour: ', ' & %s–%s & %s &' % (n(gold[0]), n(gold[1]), gold[2])))
doc += ln(bf('Evening golden hour: ', ' & %s–%s & %s &' % (n(gold[3]), n(gold[4]), gold[5])))
blue = sol.blue_hour(0)
doc += ln(bf('Morning blue hour: ', ' & %s–%s & %s &' % (n(blue[0]), n(blue[1]), blue[2])))
doc += ln(bf('Evening blue hour: ', ' & %s–%s & %s &' % (n(blue[3]), n(blue[4]), blue[5])))
doc += end('tabular')
doc += '\n\n'
## xkcd
try:
(last, img, title, text) = xkcd()
state = pwd.getpwuid(os.getuid()).pw_dir + '/.var/lib/rotd'
try:
os.makedirs(state)
except FileExistsError:
pass
state += '/xkcd'
if os.path.exists(state):
with open(state, 'rb') as file:
have = file.read()
have = eval(have.decode('utf-8', 'strict'))
else:
have = set()
if last not in have:
index = last
else:
index = 0, None
for i in range(1, last):
if i not in have:
index = i
break
if index is not None:
(index, img, title, text) = xkcd(index)
have.add(index)
spawn('curl', img, abort_timer = 2, get_stdout = 'xkcd.png')
title = Huge(escape(title))
text = escape(text, end = '\n')
doc += sectionx(whiteonblack('xkcd'))
if standalonetest(title):
doc += '\\noindent\n'
doc += title + '\n\n'
doc += '\\vspace{1em}\n'
doc += '\includegraphics[max width=\\linewidth,max height=\\textheight]{%s}\n\n' % 'xkcd.png'
doc += '\\vspace{1em}\n'
if standalonetest(text):
doc += '\\noindent\n'
doc += text + '\n\n'
have = repr(have).encode('utf-8')
with open(state, 'wb') as file:
file.write(have)
file.flush()
except:
pass
## (done)
doc += end('document')
write(doc)