Browse Source

implemented offline caching

master
Yessiest 2 years ago
parent
commit
fdff9ef67d
  1. 56
      decal

56
decal

@ -62,6 +62,9 @@ parser.add_argument("-n",
action="store",
type=int,
help="show n months")
parser.add_argument("-s","--sync",
action="store_true",
help="sync the calendar cache")
args = vars(parser.parse_args())
@ -74,7 +77,10 @@ if args["create"]:
if not os.path.exists(configpath):
config['DEFAULT'] = {'uri': 'your caldav server here',
'user': 'your username here',
'password': 'your pass here'}
'password': 'your pass here',
'cache':os.env['HOME']+'/.cache/decal.json',
'cacheEnabled':1,
'syncAfter':1}
print("Creating an empty config in ~/.config/decal.conf")
with open(configpath,'w') as configfile:
config.write(configfile)
@ -189,13 +195,6 @@ else:
start,end = getbounds(args["year"],args["month"],1)
offset = 1
# connect to the DAV and receive calendars
client = caldav.DAVClient(url = config['DEFAULT']['uri'],
username = config['DEFAULT']['user'],
password = config['DEFAULT']['password'])
principal = client.principal()
calendars = principal.calendars()
# aggregate selected calendars
def aggregateCalendars(calendars):
calendars2 = []
@ -205,10 +204,6 @@ def aggregateCalendars(calendars):
calendars2.append(cal)
return calendars2
if "calendars" in config['DEFAULT']:
calendars = aggregateCalendars(calendars)
def daysOfEvent(event):
event = event.vobject_instance.vevent.contents
curdate = event["dtstart"][0].value
@ -234,7 +229,6 @@ def jsonifyEvent(event):
evdata[key] = event[key][0].value
return evdata
def generateDateTree(calendars):
events = {}
for cal in calendars:
@ -246,10 +240,42 @@ def generateDateTree(calendars):
events[date].append(jsonifyEvent(event))
return events
events = generateDateTree(calendars)
cache = None
if "cache" in config['DEFAULT']:
if os.path.exists(config['DEFAULT']['cache']):
with open(config['DEFAULT']['cache'],"r") as file:
cache = json.loads(file.read())
def updateCriteria():
if not ("cacheEnabled" in config["DEFAULT"]):
return False
if (not (config['DEFAULT']["cacheEnabled"] == "1")):
return False
if not cache:
return True
date = datetime.datetime.strptime(cache["lastsync"],"%Y-%m-%d")
if args["sync"]:
return True
return (date.date() <= today - timedelta(days=int(config['DEFAULT']["syncAfter"])))
#Update cache if update criteria are met.
if updateCriteria():
# connect to the DAV and receive calendars
client = caldav.DAVClient(url = config['DEFAULT']['uri'],
username = config['DEFAULT']['user'],
password = config['DEFAULT']['password'])
principal = client.principal()
calendars = principal.calendars()
if "calendars" in config['DEFAULT']:
calendars = aggregateCalendars(calendars)
cache = generateDateTree(calendars)
cache["lastsync"] = str(today)
with open(config['DEFAULT']['cache'],"w") as file:
file.write(json.dumps(cache))
events = cache
if args["json"]:
print(json.JSONEncoder().encode(events))
print(json.dumps(events))
exit(0)
# and now we're just generating calendar lines

Loading…
Cancel
Save