Added webdav cache mechanism and message of no events for calendar printout
This commit is contained in:
parent
984f3d578f
commit
9620f68d72
2 changed files with 43 additions and 19 deletions
|
@ -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",
|
||||
|
|
35
webdav.py
35
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue