code commenting and cleanup
This commit is contained in:
parent
1a5700e9a0
commit
45cd71cc4b
45
__init__.py
45
__init__.py
@ -65,37 +65,6 @@ class Event:
|
|||||||
if self.allDay:
|
if self.allDay:
|
||||||
print(self.name + "All Day")
|
print(self.name + "All Day")
|
||||||
|
|
||||||
|
|
||||||
def calendarCredentials(token = None):
|
|
||||||
creds = None
|
|
||||||
# The file token.pickle stores the user's access and refresh tokens, and is
|
|
||||||
# created automatically when the authorization flow completes for the first
|
|
||||||
# time.
|
|
||||||
|
|
||||||
if token == None:
|
|
||||||
if os.path.exists('token.pickle'):
|
|
||||||
with open('token.pickle', 'rb') as token:
|
|
||||||
token.seek(0)
|
|
||||||
creds = pickle.load(token)
|
|
||||||
|
|
||||||
else:
|
|
||||||
creds = pickle.load(token)
|
|
||||||
|
|
||||||
# If there are no (valid) credentials available, let the user log in.
|
|
||||||
if not creds or not creds.valid:
|
|
||||||
if creds and creds.expired and creds.refresh_token:
|
|
||||||
creds.refresh(Request())
|
|
||||||
else:
|
|
||||||
flow = InstalledAppFlow.from_client_secrets_file(
|
|
||||||
'credentials.json', SCOPES)
|
|
||||||
creds = flow.run_local_server(port=1234)
|
|
||||||
# Save the credentials for the next run
|
|
||||||
with open('token.pickle', 'wb') as token:
|
|
||||||
pickle.dump(creds, token)
|
|
||||||
|
|
||||||
service = build('calendar', 'v3', credentials=creds)
|
|
||||||
|
|
||||||
return service
|
|
||||||
|
|
||||||
def updateCalendars(user, calendars, colors):
|
def updateCalendars(user, calendars, colors):
|
||||||
|
|
||||||
@ -132,7 +101,7 @@ def calendarsFromDb(user):
|
|||||||
|
|
||||||
return pyCalendars
|
return pyCalendars
|
||||||
|
|
||||||
|
# removes not visible calendars from the list of all calendars
|
||||||
def purgeCalendars(calendars, visibleCalendars):
|
def purgeCalendars(calendars, visibleCalendars):
|
||||||
purged = []
|
purged = []
|
||||||
for calendar in calendars:
|
for calendar in calendars:
|
||||||
@ -140,7 +109,7 @@ def purgeCalendars(calendars, visibleCalendars):
|
|||||||
purged.append(calendar)
|
purged.append(calendar)
|
||||||
return purged
|
return purged
|
||||||
|
|
||||||
|
# remaps a event color id to a calendar color id
|
||||||
def toCalendarColorId(colormap, colorId):
|
def toCalendarColorId(colormap, colorId):
|
||||||
for remap in colormap['eventRemap']:
|
for remap in colormap['eventRemap']:
|
||||||
if remap['from'] == colorId:
|
if remap['from'] == colorId:
|
||||||
@ -149,6 +118,7 @@ def toCalendarColorId(colormap, colorId):
|
|||||||
print(f"failed with {colorId}")
|
print(f"failed with {colorId}")
|
||||||
return colorId
|
return colorId
|
||||||
|
|
||||||
|
# remaps a calendar color ID to its natural color
|
||||||
def toNaturalColor(colormap, orgColor):
|
def toNaturalColor(colormap, orgColor):
|
||||||
for remap in colormap['colors']:
|
for remap in colormap['colors']:
|
||||||
if remap['api'] == orgColor:
|
if remap['api'] == orgColor:
|
||||||
@ -157,25 +127,27 @@ def toNaturalColor(colormap, orgColor):
|
|||||||
print(f"failed with {orgColor}")
|
print(f"failed with {orgColor}")
|
||||||
return orgColor
|
return orgColor
|
||||||
|
|
||||||
|
# use the event color id to convert it to a hex color
|
||||||
def colorizeEvents(events, colors):
|
def colorizeEvents(events, colors):
|
||||||
COLORS_FILE = os.path.join(os.path.dirname(__file__), 'colors.json')
|
COLORS_FILE = os.path.join(os.path.dirname(__file__), 'colors.json')
|
||||||
with open(COLORS_FILE) as f:
|
with open(COLORS_FILE) as f:
|
||||||
colormap = json.load(f)
|
colormap = json.load(f)
|
||||||
|
|
||||||
for event in events:
|
for event in events:
|
||||||
print(event.colorHex, flush=True)
|
|
||||||
if event.eventColorId != None:
|
if event.eventColorId != None:
|
||||||
event.colorHex = forEventGetColor(event, colormap, colors)
|
event.colorHex = forEventGetColor(event, colormap, colors)
|
||||||
|
|
||||||
# this function is only used once for calendar generation stuff
|
# returns a color for a specific calendar color id
|
||||||
def fromColorIdGetColor(color, colormap, colors):
|
def fromColorIdGetColor(color, colormap, colors):
|
||||||
return toNaturalColor(colormap, colors['calendar'][color]['background'])
|
return toNaturalColor(colormap, colors['calendar'][color]['background'])
|
||||||
|
|
||||||
|
# generates the natural color for a event color id
|
||||||
def forEventGetColor(event, colormap, colors):
|
def forEventGetColor(event, colormap, colors):
|
||||||
calColorId = toCalendarColorId(colormap, event.eventColorId)
|
calColorId = toCalendarColorId(colormap, event.eventColorId)
|
||||||
bg = colors['calendar'][calColorId]['background']
|
bg = colors['calendar'][calColorId]['background']
|
||||||
return toNaturalColor(colormap, bg)
|
return toNaturalColor(colormap, bg)
|
||||||
|
|
||||||
|
# exports all events into a jsonable format (excluding all day events)
|
||||||
def toJson(events):
|
def toJson(events):
|
||||||
data = {}
|
data = {}
|
||||||
data['kind'] = 'calendar#events'
|
data['kind'] = 'calendar#events'
|
||||||
@ -195,13 +167,14 @@ def toJson(events):
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
# debug functions which prints color names
|
||||||
def printColors(colors):
|
def printColors(colors):
|
||||||
for i in range(1, 25):
|
for i in range(1, 25):
|
||||||
col = colors['event'][str(i)]['background']
|
col = colors['event'][str(i)]['background']
|
||||||
print(f"{i}: {col}")
|
print(f"{i}: {col}")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
generateJsonFromCalendarEntries()
|
return
|
||||||
|
|
||||||
|
|
||||||
def getTimeStamps():
|
def getTimeStamps():
|
||||||
|
Loading…
Reference in New Issue
Block a user