From 9620f68d729418097d6c9a48223bd479d3a64e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Thu, 9 Dec 2021 15:24:27 +0100 Subject: [PATCH] Added webdav cache mechanism and message of no events for calendar printout --- smallprint.py | 27 ++++++++++++++++++--------- webdav.py | 35 +++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/smallprint.py b/smallprint.py index f57ed1c..11c613b 100644 --- a/smallprint.py +++ b/smallprint.py @@ -354,16 +354,24 @@ def print_calendar(printer): font="b", smooth=True) - for event in events: - hour = event[1].hour - minutes = event[1].minute - if minutes < 10: - printer.text(f"{hour}:0{minutes}\n") + if len(events) > 0: + for event in events: + hour = event[1].hour + minutes = event[1].minute + if minutes < 10: + printer.text(f"{hour}:0{minutes}\n") - else: - printer.text(f"{hour}:{minutes}\n") + else: + printer.text(f"{hour}:{minutes}\n") - printer.text(f"{event[0]}\n--\n") + printer.text(f"{event[0]}\n--\n") + + else: + printer.set(align="center", + text_type="NORMAL", + font="b", + smooth=True) + printer.text("No events today\n") def printing_script(printer): @@ -396,9 +404,10 @@ def printing_script(printer): if key == 'TEXT': print_text(printer, value) + printer.text('\n') if key == 'CALENDAR': - print_text(printer) + print_calendar(printer) printer.set(align="center", text_type="NORMAL", diff --git a/webdav.py b/webdav.py index 32141df..0d8e0d8 100644 --- a/webdav.py +++ b/webdav.py @@ -1,11 +1,14 @@ import datetime import os import os.path -import tempfile +from pathlib import Path from icalendar import Calendar from webdav3.client import Client as WDClient +CACHE_DIR = "~/.cache/smallprint" + + class Client: def __init__(self, config): """ @@ -19,6 +22,13 @@ class Client: self.calendar_path = config["CALENDAR_PATH"] self.client = WDClient(options) + cache_path = Path(CACHE_DIR).expanduser() # Generate the full path + if not cache_path.is_dir(): + # Create the cache dir + os.mkdir(cache_path) + + self.cache_path = cache_path + def retrieve_calendars_list(self): """ Retrieves the calendar list @@ -30,15 +40,20 @@ class Client: """ Given a filename, tries to read the calendar event """ - f, temp_path = tempfile.mkstemp() - rpath = os.path.join(self.calendar_path, pathname) - self.client.download_sync(remote_path=rpath, - local_path=temp_path) - # Read the temporary data, close the file, remove it - fics = open(temp_path, 'r') - data = fics.read() - fics.close() - os.remove(temp_path) + cache_file = os.path.join(self.cache_path, pathname) + + if not os.path.exists(cache_file): + print(f"Downloading {pathname}") + # File not in cache, download + rpath = os.path.join(self.calendar_path, pathname) + self.client.download_sync(remote_path=rpath, + local_path=cache_file) + + # Read the downloaded or cached data + with open(cache_file, 'r') as fics: + fics = open(cache_file, 'r') + data = fics.read() + # Now with the data read, we can process it return self.parse_calendar(data)