-
Notifications
You must be signed in to change notification settings - Fork 1
/
tt_main_bits.py
275 lines (228 loc) · 8.88 KB
/
tt_main_bits.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""TagTracker by Julias Hocking.
Various helper bit for the main startup/loop in data entry client of tagtracker.
Copyright (C) 2023-2024 Julias Hocking & Todd Glover
Notwithstanding the licensing information below, this code may not
be used in a commercial (for-profit, non-profit or government) setting
without the copyright-holder's written consent.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from common.tt_tag import TagID
import tt_printer as pr
import common.tt_util as ut
import client_base_config as cfg
from common.tt_time import VTime
import common.tt_constants as k
import tt_default_hours
from common.tt_trackerday import TrackerDay
from common.get_version import get_version_info
from tt_sounds import NoiseMaker
# Import pyfiglet if it's available.
# (To create ascii art splash screen.)
try:
import random
import pyfiglet
PYFIGLET = True
except ImportError:
PYFIGLET = False
def splash():
"""Print a splash message including version & credits."""
if not splash_top_pyfiglet():
splash_top_default()
pr.iprint()
pr.iprint(f"TagTracker version: {get_version_info()}")
pr.iprint("See github.com/ironwoodcall/tagtracker for version details.")
pr.iprint()
def splash_top_pyfiglet():
"""Print a randomly-selected pyfiglet message.
If nothing fits then returns False. If worked ok then True.
"""
def render(
message, font, max_width: int = 80, hpadding: int = 0, vpadding: int = 0
) -> list[str]:
"""Render message in the font, return strings list or None.
On return, all the strings will be of the same length.
Empty strings will be removed from the top and bottom.
Strings will be padded front and back with 'hpadding' spaces,
and 'vpadding' blank lines above & below.
"""
f = pyfiglet.Figlet(font=font)
f.width = 2000
lines = f.renderText(message).split("\n")
lines = [s.rstrip() for s in lines]
longest = max((len(s) for s in lines))
if longest > max_width or longest == len(message):
return None
# Remove empty strings from the end..
while lines and len(lines[-1]) == 0:
lines.pop()
# ..and the beginning of the list
while lines and len(lines[0]) == 0:
lines.pop(0)
# Now add any vertical padding blank lines
vpad = [""] * vpadding
lines = vpad + lines + vpad
# Pad them to the same length, including extra padding.
lines = [" " * hpadding + s.ljust(longest + hpadding) for s in lines]
# Add any vertical padding
# Return the result
return lines
if not PYFIGLET:
return False
available_fonts = pyfiglet.FigletFont.getFonts()
msg_lines = None
while available_fonts and not msg_lines:
font = random.choice(available_fonts)
available_fonts.remove(font)
msg_lines = render("TagTracker", font, hpadding=2, vpadding=1)
if not msg_lines:
return False
# Print the message
for line in msg_lines:
pr.iprint(line, style=k.ANSWER_STYLE)
return True
def splash_top_default():
"""Print the default intro splash message."""
for line in [
r" _______ _______ _ ",
r" |__ __| |__ __| | | ",
r" | | __ _ __ _| |_ __ __ _ ___| | _____ _ __ ",
r" | |/ _` |/ _` | | '__/ _` |/ __| |/ / _ \ '__| ",
r" | | (_| | (_| | | | | (_| | (__| < __/ | ",
r" |_|\__,_|\__, |_|_| \__,_|\___|_|\_\___|_| ",
r" __/ | ",
r" |___/ ",
]:
pr.iprint(
line,
style=k.ANSWER_STYLE,
)
def confirm_hours(today: TrackerDay) -> bool:
# date: k.MaybeDate, current_open: k.MaybeTime, current_close: k.MaybeTime
# ) -> tuple[bool, VTime, VTime]:
"""Get/set operating hours.
Returns flag True if values have changed
Logic:
- On entry, current_open/close are either times or ""
- If "", sets from defaults
- Prompts user for get/confirm
- Changes values in TrackerDay
"""
maybe_open = today.time_open
maybe_close = today.time_closed
if not maybe_open or not maybe_close:
# Set any blank value from config'd defaults
default_open, default_close = tt_default_hours.get_default_hours(today.date)
maybe_open = maybe_open or default_open
maybe_close = maybe_close or default_close
# Prompt user to get/confirm times
done = False
new_open, new_close = VTime(maybe_open), VTime(maybe_close)
while not done:
new_open, new_close = get_operating_hours(new_open, new_close)
if new_open >= new_close:
pr.iprint(
f"Closing time must be later than opening time {new_open}",
style=k.ERROR_STYLE,
)
else:
done = True
# Has anything changed?
data_changed = bool(
new_open != today.time_open or new_close != today.time_closed
)
# Save the changed
today.time_open = new_open
today.time_closed = new_close
# Done, return whether data has changed
return data_changed
def get_operating_hours(opening: str = "", closing: str = "") -> tuple[str, str]:
"""Get/confirm today's operating hours."""
def get_one_time(current_time: str, prompt_bit: str) -> str:
"""Confirm/get an opening or closing time."""
current_time = VTime(current_time)
while True:
if current_time:
pr.iprint(
f"Enter new 24-hour HHMM {prompt_bit} time or press <Enter> "
f"to leave as {current_time.short}: ",
end="",
style=k.SUBPROMPT_STYLE,
)
else:
pr.iprint(
f"Enter 24-hour HHMM {prompt_bit} time: ",
end="",
style=k.SUBPROMPT_STYLE,
)
inp = pr.tt_inp().strip()
if current_time and not inp:
return current_time
new_time = VTime(inp)
if new_time:
return new_time
else:
pr.iprint("Not a time. ", style=k.WARNING_STYLE)
which = "confirm" if opening else "enter"
pr.iprint()
pr.iprint(
f"Please {which} today's operating hours.", style=k.HIGHLIGHT_STYLE, end=""
)
if opening and closing:
pr.iprint(
f" Currently set at {opening.short} - {closing.short}.",
num_indents=0,
style=k.HIGHLIGHT_STYLE,
end="",
)
pr.iprint() # linefeed for end of the above line
opening = get_one_time(opening, "opening")
closing = get_one_time(closing, "closing")
return opening, closing
def check_bike_time_reasonable(bike_time: VTime, day: TrackerDay) -> bool:
"""Check if 'bike_time' is reasonably close to operating hours.
Will give an error message if not.
Returns True if seems reasonable, False otherwise.
If operating hours not set, returns True.
"""
if day.bike_time_reasonable(bike_time):
return True
pr.iprint(
f"Time ({bike_time.short}) is too far outside open hours "
f"({day.time_open.short}-{day.time_closed.short}).",
style=k.WARNING_STYLE,
)
NoiseMaker.play(k.ALERT)
return False
def check_tagid_usable(tagid: TagID, today: TrackerDay) -> bool:
"""Checks if tagid is usable, error msg if not.
In this context, usable means REGULAR or OVERSIZE and
not RETIRED.
Returns True if usable, False if not.
"""
if tagid in today.all_usable_tags():
return True
if tagid in today.retired_tagids:
msg = f"Tag {tagid} is retired."
else:
msg = f"No tag '{tagid.original}' available today."
pr.iprint(msg, style=k.WARNING_STYLE)
NoiseMaker.play(k.ALERT)
return False
def data_owner_notice():
"""Print a data ownership notice."""
if cfg.DATA_OWNER:
pr.iprint()
data_note = (
cfg.DATA_OWNER if isinstance(cfg.DATA_OWNER, list) else [cfg.DATA_OWNER]
)
for line in data_note:
pr.iprint(line, style=k.ANSWER_STYLE)