-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagtracker.py
executable file
·384 lines (313 loc) · 11.9 KB
/
tagtracker.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python3
"""This is the data entry module for the TagTracker suite.
Its configuration file is tagtracker_config.py.
Copyright (C) 2023-2024 Todd Glover & Julias Hocking
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/>.
"""
import os
import sys
import time
# The readline module magically solves arrow keys creating ANSI esc codes
# on the Chromebook. But it isn't on all platforms.
try:
import readline # pylint:disable=unused-import
except ImportError:
pass
# Make sure running usable version of python.
# Yes, this is before the imports.
if sys.version_info < (3, 10):
print("TagTracker requires Python 3.10 or later.")
sys.exit(1)
# pylint: disable=wrong-import-position
import common.tt_constants as k
from common.tt_tag import TagID
import tt_reports as rep
# from tt_realtag import Stay
from common.tt_time import VTime
import common.tt_util as ut
from common.tt_trackerday import TrackerDay, TrackerDayError
import client_base_config as cfg
import tt_printer as pr
import tt_datafile as df
import tt_publish as pub
# import tt_notes as notes
from tt_process_command import process_command, lint_report
# from tt_cmdparse import CmdBits
from tt_commands import (
CmdKeys,
get_parsed_command,
PARSED_OK,
tags_arg
)
from tt_sounds import NoiseMaker
from tt_internet_monitor import InternetMonitorController
import tt_main_bits as bits
# The assignments below are unneccessary but stop pylint from whining.
publishment = None
def deduce_parking_date(filename: str) -> str:
"""Guess what date the current data is for based on the filename.
Logic:
If current_guess is set (presumably read from the contents
of the datafile) then it is used.
Else if there appears to be a date embedded in the name of
the datafile, it is used.
Else today's date is used.
"""
r = k.DATE_PART_RE.search(filename)
if r:
return f"{int(r.group(2)):04d}-{int(r.group(3)):02d}-" f"{int(r.group(4)):02d}"
return ut.date_str("today")
def bikes_on_hand_reminder(day: TrackerDay) -> None:
"""Remind how many bikes should be present, if close to closing time."""
if day.time_closed.num - VTime("now").num < 60: # last hour
num_bikes = len(day.tags_in_use("now"))
pr.iprint(
f"There should currently be {num_bikes} {ut.plural(num_bikes,'bike')}"
" here.",
style=k.HIGHLIGHT_STYLE,
)
def main_loop(today: TrackerDay):
"""Run main program command loop."""
done = False
todays_date = ut.date_str("today")
while not done:
pr.iprint()
# Nag about bikes expected to be present if close to closing time
bikes_on_hand_reminder(day=today)
# # Allow tag notes for this next command
# rep.print_tag_notes(today,"", reset=True)
# Get a command from the user.
cmd_bits = get_parsed_command()
# Exit if exit
if cmd_bits.command == CmdKeys.CMD_EXIT:
break
# If midnight has passed then need to restart
if midnight_passed(todays_date):
midnight_message()
break
# If null input, just ignore
if cmd_bits.status != PARSED_OK:
continue # No input, ignore
# Process the command
data_changed = False
if cmd_bits.status == PARSED_OK:
data_changed = process_command(
cmd_bits=cmd_bits, today=today, publishment=publishment
)
# Print any notes particularly associated with tag(s) in this command.
tag_arg = tags_arg(cmd_bits.command)
if tag_arg is not None:
for tag in cmd_bits.result_args[tag_arg]:
rep.print_tag_notes(today,tag)
# If any time has becomne "24:00" change it to "23:59" (I forget why)
if data_changed and today.fix_2400_events():
pr.iprint(
"(Changed any '24:00' check-in/out times to '23:59'.)",
style=k.WARNING_STYLE,
)
# Save if any data has changed
if data_changed:
today.save_to_file()
data_changed = False
publishment.maybe_publish(today)
##last_published = maybe_publish(last_published)
# Flush any echo buffer
pr.echo_flush()
# Exiting; one last publishing
publishment.publish(today)
def custom_datafile() -> str:
"""Return filepath of custom datafilename specified.
Returns custom filepath if given, "" if not.
File is checked to exist.
"""
if len(sys.argv) <= 1:
return ""
# Custom datafile name or location
file = sys.argv[1]
# File there?
if not os.path.exists(file):
pr.iprint(f"Error: File {file} not found", style=k.ERROR_STYLE)
error_exit()
# This is the custom datafile & it exists
return file
def set_taglists_from_config(day:TrackerDay):
"""Assign oversize, regular, and retired tag IDs from config."""
day.regular_tagids, errors = TagID.parse_tagids_str(
cfg.REGULAR_TAGS, "REGULAR_TAGS configuration"
)
day.oversize_tagids, errs = TagID.parse_tagids_str(
cfg.OVERSIZE_TAGS, "OVERSIZE_TAGS configuration"
)
errors += errs
day.retired_tagids, errs = TagID.parse_tagids_str(
cfg.RETIRED_TAGS, "RETIRED_TAGS configuration"
)
errors += errs
if errors:
raise TrackerDayError(*errors)
overlap = day.regular_tagids.intersection(day.oversize_tagids)
if overlap:
raise TrackerDayError(
f"These tags are configured as both regular and oversize:\n{overlap}"
)
if not day.regular_tagids and not day.oversize_tagids:
raise TrackerDayError("Configuration file defines to tags.")
day.determine_tagids_conformity()
# FIXME: Is this the right place for this? I don't think so.
if not day.biketags:
day.initialize_biketags()
def error_exit() -> None:
"""If an error has occurred, give a message and shut down.
Any specific info about the error should already have been printed.
"""
pr.iprint()
pr.iprint("Closing in 30 seconds", style=k.ERROR_STYLE)
time.sleep(30)
exit()
def midnight_message():
"""Print a "you have to restart" message."""
# Time has rolled over past midnight so need a new datafile.
print("\n\n\n")
pr.iprint("Program has been running since yesterday.", style=k.WARNING_STYLE)
pr.iprint(
"Please restart program to reset for today's data.",
style=k.WARNING_STYLE,
)
pr.iprint()
print("\n\n\n")
print("Automatically exiting in 15 seconds")
time.sleep(15)
def midnight_passed(today_is: str) -> bool:
"""Check if it's still the same day."""
if today_is == ut.date_str("today"):
return False
return True
# def get_taglists_from_old_config(old_config_file: str) -> td.OldTrackerDay:
# """Read tag lists (oversize, etc) from tag config file."""
# # Lists of normal, oversize, retired tags
# # Return a OldTrackerDay object, though its bikes_in/out are meaningless.
# errs = []
# day = df.read_datafile(old_config_file, errs)
# if errs:
# print(f"Errors in file, {errs=}")
# error_exit()
# pr.iprint()
# pr.iprint(
# f"Using tag configurations from deprecated '{old_config_file}' configuration file.",
# style=k.ERROR_STYLE,
# )
# pr.iprint(
# "Define tag configurations in client_local_config.py.",
# style=k.ERROR_STYLE,
# )
# return day
def set_up_today() -> TrackerDay:
"""Initialize today's tracking data."""
datafilepath = custom_datafile()
datafilepath = datafilepath or df.datafile_name(cfg.DATA_FOLDER)
if os.path.exists(datafilepath):
try:
day = TrackerDay.load_from_file(datafilepath)
except TrackerDayError as e:
for s in e.args:
pr.iprint(s,style=k.ERROR_STYLE)
error_exit()
else:
day = TrackerDay(
datafilepath, site_handle=cfg.SITE_HANDLE, site_name=cfg.SITE_NAME
)
# Just in case there's no date, guess it from the filename
if not day.date:
day.date = deduce_parking_date(datafilepath)
# Set some bits, if missing
day.fill_default_bits(site_name = cfg.SITE_NAME,site_handle=cfg.SITE_HANDLE)
# Find the tag reference lists (regular, oversize, etc).
# If there's no tag reference lists, or it's today's date,
# then fetch the tag reference lists from tags config
if day.date == ut.date_str("today") or (
not day.regular_tagids and not day.oversize_tagids
):
ut.squawk("Setting taglists from config",cfg.DEBUG)
try:
set_taglists_from_config(day)
except TrackerDayError as e:
pr.iprint()
for text in e.args:
pr.iprint(text, style=k.ERROR_STYLE)
error_exit()
pr.iprint(
f"Editing {day.site_name} bike parking data for {ut.date_str(day.date,long_date=True)}.",
style=k.HIGHLIGHT_STYLE,
)
# In case doing a date that's not today, warn
if day.date != ut.date_str("today"):
pr.iprint(f"Warning: Data is from {day.date}, not today", style=k.ERROR_STYLE)
# Get/set operating hours
bits.confirm_hours(day)
# Look for inconsistencies
if lint_report(today=day, strict_datetimes=True, chatty=False):
error_exit()
# Save
day.save_to_file()
return day
if __name__ == "__main__":
# Set up TagTracker system
# Set colour module's colour flag based on config
pr.COLOUR_ACTIVE = cfg.USE_COLOUR
# Possibly turn on echo. Print any error msgs later, though
echo_msg = ""
if cfg.ECHO:
if not cfg.ECHO_FOLDER:
echo_msg = "No echo folder configured, setting echo off."
pr.set_echo(False)
elif not ut.writable_dir(cfg.ECHO_FOLDER):
echo_msg = (
f"Echo folder '{cfg.ECHO_FOLDER}' "
"missing or not writeable, setting echo off."
)
pr.set_echo(False)
else:
pr.set_echo(True)
pr.clear_screen()
bits.splash()
# echo error messages now
if echo_msg:
pr.iprint()
pr.iprint(echo_msg, style=k.WARNING_STYLE)
# Check that data directory is writable
if not ut.writable_dir(cfg.DATA_FOLDER):
pr.iprint()
pr.iprint(
f"Data folder '{cfg.DATA_FOLDER}' missing or not writeable.",
style=k.ERROR_STYLE,
)
sys.exit(1)
# Set up publishing
publishment = pub.Publisher(cfg.REPORTS_FOLDER, cfg.PUBLISH_FREQUENCY)
# Check that sounds can work (if enabled).
NoiseMaker.init_check()
# Start internet monitoring (if enabled in config)
InternetMonitorController.start_monitor()
# Initialize today's tracking data
today_data = set_up_today()
# Display data owner notice
bits.data_owner_notice()
# Set UC if needed (NB: datafiles are always LC)
TagID.uc(cfg.TAGS_UPPERCASE)
# Start tracking tags
main_loop(today_data)
# Finished, turn off echo
pr.set_echo(False)
# ==========================================