Browse Source

Integrated notification time to see if it works

atexit
Raphael Roberts 6 years ago
parent
commit
b5d1533539
  1. 57
      ctabus/__init__.py
  2. 4
      ctabus/internal/notification.py

57
ctabus/__init__.py

@ -17,6 +17,7 @@ import urllib
from ctabus import fetch
from ctabus.internal.config import log_dir, recent_list
from ctabus.internal.disk_cache import disk_cache, make_key
from ctabus.internal.notification import NotificationManager, HAS_NOTIFICATION
HAS_TOAST = shutil.which("termux-toast") is not None
CHICAGO_TZ = tz.gettz("America/Chicago")
@ -30,7 +31,8 @@ parser.add_argument(
)
parser.add_argument("-r", "--route", default=None)
parser.add_argument("-d", "--direction", default=None)
parser.add_argument("-t", "--disable_toast", action="store_false")
parser.add_argument("-t", "--toast", action="store_true")
parser.add_argument("-n", "--notifications", action="store_true")
parser.add_argument("-k", "--kill-cache", action="store_true")
parser.add_argument(
"arg",
@ -155,6 +157,11 @@ class Table:
return which
class CTABUSNotifictaionManager(NotificationManager):
def on_done(self):
quit()
def gen_list(objs, data, *displays, key=None, sort=0, num_pic=True):
"""Returns an item from objs[data] based on name or index
@ -186,8 +193,13 @@ config = """\
{delta} ({t})\
"""
if HAS_NOTIFICATION:
nm = CTABUSNotifictaionManager()
def show(data, rt_filter=None, _clear=False, enable_toast=False):
def show(
data, rt_filter=None, _clear=False, enable_toast=False, enable_notifications=False
):
times = data["prd"]
now = datetime.datetime.now(CHICAGO_TZ)
arrivals = sorted(times, key=lambda t: t["prdtm"])
@ -195,22 +207,31 @@ def show(data, rt_filter=None, _clear=False, enable_toast=False):
arrivals = filter(lambda arrival: arrival["rt"] == rt_filter, arrivals)
if _clear:
clearscr()
do_toast = True
do_gui = True
for bustime in arrivals:
before = date_parse(bustime["prdtm"])
arrival = before.replace(tzinfo=CHICAGO_TZ)
if arrival > now:
stop_id = bustime["stpid"]
delta = pprint_delta(arrival - now)
t = arrival.strftime("%H:%M:%S")
route = bustime["rt"]
direction = bustime["rtdir"]
end = bustime["des"]
nm = bustime["stpnm"].rstrip()
if do_toast and enable_toast:
toast(config.format(**locals()) + "\n" * 2 + "\n")
do_toast = False
print(config.format(**locals()), end="\n" * 2)
format_dict = {
"stop_id": bustime["stpid"],
"delta": pprint_delta(arrival - now),
"t": arrival.strftime("%H:%M:%S"),
"route": bustime["rt"],
"direction": bustime["rtdir"],
"end": bustime["des"],
"nm": bustime["stpnm"].rstrip(),
}
text_to_show = config.format(**format_dict)
if do_gui and enable_notifications:
nm.set_title("Bustimes")
nm.say(text_to_show)
if do_gui and enable_toast:
toast(text_to_show)
do_gui = False
print(text_to_show, end="\n" * 2)
print("=" * 36)
@ -263,7 +284,13 @@ def _main_periodic(args, stop_id, init_data):
while not _done:
try:
try:
show(data, args.route, True, args.disable_toast and HAS_TOAST)
show(
data,
args.route,
True,
args.toast and HAS_TOAST,
args.notifications and HAS_NOTIFICATION,
)
s = time.perf_counter()
timeout = 1
if args.periodic > timeout:

4
ctabus/internal/notification.py

@ -1,5 +1,6 @@
from concurrent.futures import ThreadPoolExecutor
import os
import shutil
import subprocess
import uuid
@ -7,6 +8,7 @@ from ctabus.internal.config import state_dir
EXECUTOR = ThreadPoolExecutor()
NOTIFICATION_EXE = "termux-notification"
HAS_NOTIFICATION = shutil.which(NOTIFICATION_EXE) is not None
class NotificationException(Exception):
@ -39,9 +41,9 @@ class NotificationManager:
self.show()
def _cleanup(self):
self.on_done()
self._live = False
os.remove(self.listen_fifo_path)
self.on_done()
def _listen(self):

Loading…
Cancel
Save