implemented offline caching
This commit is contained in:
parent
1cc223e567
commit
fdff9ef67d
56
decal
56
decal
|
@ -62,6 +62,9 @@ parser.add_argument("-n",
|
||||||
action="store",
|
action="store",
|
||||||
type=int,
|
type=int,
|
||||||
help="show n months")
|
help="show n months")
|
||||||
|
parser.add_argument("-s","--sync",
|
||||||
|
action="store_true",
|
||||||
|
help="sync the calendar cache")
|
||||||
|
|
||||||
args = vars(parser.parse_args())
|
args = vars(parser.parse_args())
|
||||||
|
|
||||||
|
@ -74,7 +77,10 @@ if args["create"]:
|
||||||
if not os.path.exists(configpath):
|
if not os.path.exists(configpath):
|
||||||
config['DEFAULT'] = {'uri': 'your caldav server here',
|
config['DEFAULT'] = {'uri': 'your caldav server here',
|
||||||
'user': 'your username 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")
|
print("Creating an empty config in ~/.config/decal.conf")
|
||||||
with open(configpath,'w') as configfile:
|
with open(configpath,'w') as configfile:
|
||||||
config.write(configfile)
|
config.write(configfile)
|
||||||
|
@ -189,13 +195,6 @@ else:
|
||||||
start,end = getbounds(args["year"],args["month"],1)
|
start,end = getbounds(args["year"],args["month"],1)
|
||||||
offset = 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
|
# aggregate selected calendars
|
||||||
def aggregateCalendars(calendars):
|
def aggregateCalendars(calendars):
|
||||||
calendars2 = []
|
calendars2 = []
|
||||||
|
@ -205,10 +204,6 @@ def aggregateCalendars(calendars):
|
||||||
calendars2.append(cal)
|
calendars2.append(cal)
|
||||||
return calendars2
|
return calendars2
|
||||||
|
|
||||||
if "calendars" in config['DEFAULT']:
|
|
||||||
calendars = aggregateCalendars(calendars)
|
|
||||||
|
|
||||||
|
|
||||||
def daysOfEvent(event):
|
def daysOfEvent(event):
|
||||||
event = event.vobject_instance.vevent.contents
|
event = event.vobject_instance.vevent.contents
|
||||||
curdate = event["dtstart"][0].value
|
curdate = event["dtstart"][0].value
|
||||||
|
@ -234,7 +229,6 @@ def jsonifyEvent(event):
|
||||||
evdata[key] = event[key][0].value
|
evdata[key] = event[key][0].value
|
||||||
return evdata
|
return evdata
|
||||||
|
|
||||||
|
|
||||||
def generateDateTree(calendars):
|
def generateDateTree(calendars):
|
||||||
events = {}
|
events = {}
|
||||||
for cal in calendars:
|
for cal in calendars:
|
||||||
|
@ -246,10 +240,42 @@ def generateDateTree(calendars):
|
||||||
events[date].append(jsonifyEvent(event))
|
events[date].append(jsonifyEvent(event))
|
||||||
return events
|
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"]:
|
if args["json"]:
|
||||||
print(json.JSONEncoder().encode(events))
|
print(json.dumps(events))
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
# and now we're just generating calendar lines
|
# and now we're just generating calendar lines
|
||||||
|
|
Loading…
Reference in New Issue