From aaf66a1f105b9baceb13fd59287f8682325270dd Mon Sep 17 00:00:00 2001 From: raphael Date: Wed, 15 Apr 2020 17:43:52 +0000 Subject: [PATCH] changes process the colors are remapped into a more capsuled function --- caltojson.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/caltojson.py b/caltojson.py index ed60051..e54ad87 100644 --- a/caltojson.py +++ b/caltojson.py @@ -172,21 +172,26 @@ def toNaturalColor(colormap, orgColor): print(f"failed with {orgColor}") return orgColor -def colorizeEvents(allEvents, colors): - +def colorizeEvents(allEvents, service): COLORS_FILE = os.path.join(os.path.dirname(__file__), 'colors.json') with open(COLORS_FILE) as f: colormap = json.load(f) + + colors = getCalendarColors(service) for event in allEvents: - if event.calendarColorId != 0: - orgColor = colors['calendar'][event.calendarColorId]['background'] - else: - calColorId = toCalendarColorId(colormap, event.eventColorId) - orgColor = colors['calendar'][calColorId]['background'] - - event.colorHex = toNaturalColor(colormap, orgColor) - + event.colorHex = fromColorIdGetColor(event.calendarColorId, colormap, colors) + +def fromColorIdGetColor(colorId, colormap, colors): + + if colorId != 0: + orgColor = colors['calendar'][colorId]['background'] + else: + calColorId = toCalendarColorId(colormap, colorId) + orgColor = colors['calendar'][calColorId]['background'] + + return toNaturalColor(colormap, orgColor) + def toJson(events): data = {} data['kind'] = 'calendar#events' @@ -225,9 +230,11 @@ def getCalendarList(credentials = None): COLORS_FILE = os.path.join(os.path.dirname(__file__), 'colors.json') with open(COLORS_FILE) as f: colormap = json.load(f) + + colors = getCalendarColors(service) for calendar in calendars: - calendar.color = toNaturalColor(colormap, calendar.color) + calendar.color = fromColorIdGetColor(calendar.color, colormap, colors) return calendars def generateJsonFromCalendarEntries(credentials = None): @@ -244,8 +251,7 @@ def generateJsonFromCalendarEntries(credentials = None): allEvents = getCalendarEvents(service, today, tomorrow) - colors = getCalendarColors(service) - colorizeEvents(allEvents, colors) + colorizeEvents(allEvents, service) # if not events: # print('No upcoming events found.')