mirror of
https://github.com/b4tman/sync_ics2gcal
synced 2025-01-21 23:38:58 +00:00
lint gcal
This commit is contained in:
parent
8d64869f06
commit
9e74772852
@ -1,5 +1,4 @@
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
|
||||||
|
|
||||||
import google.auth
|
import google.auth
|
||||||
from google.oauth2 import service_account
|
from google.oauth2 import service_account
|
||||||
@ -26,9 +25,10 @@ class GoogleCalendarService():
|
|||||||
|
|
||||||
scopes = ['https://www.googleapis.com/auth/calendar']
|
scopes = ['https://www.googleapis.com/auth/calendar']
|
||||||
credentials, _ = google.auth.default(scopes=scopes)
|
credentials, _ = google.auth.default(scopes=scopes)
|
||||||
service = discovery.build('calendar', 'v3', credentials=credentials, cache_discovery=False)
|
service = discovery.build(
|
||||||
|
'calendar', 'v3', credentials=credentials, cache_discovery=False)
|
||||||
return service
|
return service
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_srv_acc_file(service_account_file):
|
def from_srv_acc_file(service_account_file):
|
||||||
"""make service Resource from service account filename (authorize)
|
"""make service Resource from service account filename (authorize)
|
||||||
@ -38,11 +38,14 @@ class GoogleCalendarService():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
scopes = ['https://www.googleapis.com/auth/calendar']
|
scopes = ['https://www.googleapis.com/auth/calendar']
|
||||||
credentials = service_account.Credentials.from_service_account_file(service_account_file)
|
credentials = service_account.Credentials.from_service_account_file(
|
||||||
|
service_account_file)
|
||||||
scoped_credentials = credentials.with_scopes(scopes)
|
scoped_credentials = credentials.with_scopes(scopes)
|
||||||
service = discovery.build('calendar', 'v3', credentials=scoped_credentials, cache_discovery=False)
|
service = discovery.build(
|
||||||
|
'calendar', 'v3', credentials=scoped_credentials,
|
||||||
|
cache_discovery=False)
|
||||||
return service
|
return service
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_config(config=None):
|
def from_config(config=None):
|
||||||
"""make service Resource from config dict
|
"""make service Resource from config dict
|
||||||
@ -51,25 +54,27 @@ class GoogleCalendarService():
|
|||||||
config -- dict() config with keys:
|
config -- dict() config with keys:
|
||||||
(optional) service_account: - service account filename
|
(optional) service_account: - service account filename
|
||||||
if key not in dict then default credentials will be used
|
if key not in dict then default credentials will be used
|
||||||
( https://developers.google.com/identity/protocols/application-default-credentials )
|
( https://developers.google.com/identity/protocols/application-default-credentials )
|
||||||
-- None: default credentials will be used
|
-- None: default credentials will be used
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
service Resource
|
service Resource
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if (not config is None) and 'service_account' in config:
|
if config is not None and 'service_account' in config:
|
||||||
service = GoogleCalendarService.from_srv_acc_file(config['service_account'])
|
service = GoogleCalendarService.from_srv_acc_file(
|
||||||
|
config['service_account'])
|
||||||
else:
|
else:
|
||||||
service = GoogleCalendarService.default()
|
service = GoogleCalendarService.default()
|
||||||
return service
|
return service
|
||||||
|
|
||||||
|
|
||||||
def select_event_key(event):
|
def select_event_key(event):
|
||||||
"""select event key for logging
|
"""select event key for logging
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
event -- event resource
|
event -- event resource
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
key name or None if no key found
|
key name or None if no key found
|
||||||
"""
|
"""
|
||||||
@ -94,11 +99,11 @@ class GoogleCalendar():
|
|||||||
|
|
||||||
def _make_request_callback(self, action, events_by_req):
|
def _make_request_callback(self, action, events_by_req):
|
||||||
"""make callback for log result of batch request
|
"""make callback for log result of batch request
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
action -- action name
|
action -- action name
|
||||||
events_by_req -- list of events ordered by request id
|
events_by_req -- list of events ordered by request id
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
callback function
|
callback function
|
||||||
"""
|
"""
|
||||||
@ -108,8 +113,10 @@ class GoogleCalendar():
|
|||||||
key = select_event_key(event)
|
key = select_event_key(event)
|
||||||
|
|
||||||
if exception is not None:
|
if exception is not None:
|
||||||
self.logger.error('failed to %s event with %s: %s, exception: %s',
|
self.logger.error(
|
||||||
action, key, event.get(key), str(exception))
|
'failed to %s event with %s: %s, exception: %s',
|
||||||
|
action, key, event.get(key), str(exception)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
resp_key = select_event_key(response)
|
resp_key = select_event_key(response)
|
||||||
if resp_key is not None:
|
if resp_key is not None:
|
||||||
@ -128,8 +135,11 @@ class GoogleCalendar():
|
|||||||
timeMin = utc.normalize(start.astimezone(utc)).replace(
|
timeMin = utc.normalize(start.astimezone(utc)).replace(
|
||||||
tzinfo=None).isoformat() + 'Z'
|
tzinfo=None).isoformat() + 'Z'
|
||||||
while True:
|
while True:
|
||||||
response = self.service.events().list(calendarId=self.calendarId, pageToken=page_token,
|
response = self.service.events().list(calendarId=self.calendarId,
|
||||||
singleEvents=True, timeMin=timeMin, fields=fields).execute()
|
pageToken=page_token,
|
||||||
|
singleEvents=True,
|
||||||
|
timeMin=timeMin,
|
||||||
|
fields=fields).execute()
|
||||||
if 'items' in response:
|
if 'items' in response:
|
||||||
events.extend(response['items'])
|
events.extend(response['items'])
|
||||||
page_token = response.get('nextPageToken')
|
page_token = response.get('nextPageToken')
|
||||||
@ -160,8 +170,9 @@ class GoogleCalendar():
|
|||||||
if exception is None:
|
if exception is None:
|
||||||
found = ([] != response['items'])
|
found = ([] != response['items'])
|
||||||
else:
|
else:
|
||||||
self.logger.error('exception %s, while listing event with UID: %s', str(
|
self.logger.error(
|
||||||
exception), event['iCalUID'])
|
'exception %s, while listing event with UID: %s',
|
||||||
|
str(exception), event['iCalUID'])
|
||||||
if found:
|
if found:
|
||||||
exists.append(
|
exists.append(
|
||||||
(event, response['items'][0]))
|
(event, response['items'][0]))
|
||||||
@ -173,7 +184,12 @@ class GoogleCalendar():
|
|||||||
for event in events:
|
for event in events:
|
||||||
events_by_req.append(event)
|
events_by_req.append(event)
|
||||||
batch.add(self.service.events().list(calendarId=self.calendarId,
|
batch.add(self.service.events().list(calendarId=self.calendarId,
|
||||||
iCalUID=event['iCalUID'], showDeleted=True, fields=fields), request_id=str(i))
|
iCalUID=event['iCalUID'],
|
||||||
|
showDeleted=True,
|
||||||
|
fields=fields
|
||||||
|
),
|
||||||
|
request_id=str(i)
|
||||||
|
)
|
||||||
i += 1
|
i += 1
|
||||||
batch.execute()
|
batch.execute()
|
||||||
self.logger.info('%d events exists, %d not found',
|
self.logger.info('%d events exists, %d not found',
|
||||||
@ -196,7 +212,9 @@ class GoogleCalendar():
|
|||||||
for event in events:
|
for event in events:
|
||||||
events_by_req.append(event)
|
events_by_req.append(event)
|
||||||
batch.add(self.service.events().insert(
|
batch.add(self.service.events().insert(
|
||||||
calendarId=self.calendarId, body=event, fields=fields), request_id=str(i))
|
calendarId=self.calendarId, body=event, fields=fields),
|
||||||
|
request_id=str(i)
|
||||||
|
)
|
||||||
i += 1
|
i += 1
|
||||||
batch.execute()
|
batch.execute()
|
||||||
|
|
||||||
@ -218,7 +236,8 @@ class GoogleCalendar():
|
|||||||
continue
|
continue
|
||||||
events_by_req.append(event_new)
|
events_by_req.append(event_new)
|
||||||
batch.add(self.service.events().patch(
|
batch.add(self.service.events().patch(
|
||||||
calendarId=self.calendarId, eventId=event_old['id'], body=event_new), fields=fields, request_id=str(i))
|
calendarId=self.calendarId, eventId=event_old['id'],
|
||||||
|
body=event_new), fields=fields, request_id=str(i))
|
||||||
i += 1
|
i += 1
|
||||||
batch.execute()
|
batch.execute()
|
||||||
|
|
||||||
@ -240,7 +259,8 @@ class GoogleCalendar():
|
|||||||
continue
|
continue
|
||||||
events_by_req.append(event_new)
|
events_by_req.append(event_new)
|
||||||
batch.add(self.service.events().update(
|
batch.add(self.service.events().update(
|
||||||
calendarId=self.calendarId, eventId=event_old['id'], body=event_new, fields=fields), request_id=str(i))
|
calendarId=self.calendarId, eventId=event_old['id'],
|
||||||
|
body=event_new, fields=fields), request_id=str(i))
|
||||||
i += 1
|
i += 1
|
||||||
batch.execute()
|
batch.execute()
|
||||||
|
|
||||||
@ -259,7 +279,8 @@ class GoogleCalendar():
|
|||||||
for event in events:
|
for event in events:
|
||||||
events_by_req.append(event)
|
events_by_req.append(event)
|
||||||
batch.add(self.service.events().delete(
|
batch.add(self.service.events().delete(
|
||||||
calendarId=self.calendarId, eventId=event['id']), request_id=str(i))
|
calendarId=self.calendarId,
|
||||||
|
eventId=event['id']), request_id=str(i))
|
||||||
i += 1
|
i += 1
|
||||||
batch.execute()
|
batch.execute()
|
||||||
|
|
||||||
@ -280,7 +301,9 @@ class GoogleCalendar():
|
|||||||
if timeZone is not None:
|
if timeZone is not None:
|
||||||
calendar['timeZone'] = timeZone
|
calendar['timeZone'] = timeZone
|
||||||
|
|
||||||
created_calendar = self.service.calendars().insert(body=calendar).execute()
|
created_calendar = self.service.calendars().insert(
|
||||||
|
body=calendar
|
||||||
|
).execute()
|
||||||
self.calendarId = created_calendar['id']
|
self.calendarId = created_calendar['id']
|
||||||
return created_calendar
|
return created_calendar
|
||||||
|
|
||||||
@ -300,7 +323,10 @@ class GoogleCalendar():
|
|||||||
},
|
},
|
||||||
'role': 'reader'
|
'role': 'reader'
|
||||||
}
|
}
|
||||||
return self.service.acl().insert(calendarId=self.calendarId, body=rule_public).execute()
|
return self.service.acl().insert(
|
||||||
|
calendarId=self.calendarId,
|
||||||
|
body=rule_public
|
||||||
|
).execute()
|
||||||
|
|
||||||
def add_owner(self, email):
|
def add_owner(self, email):
|
||||||
"""add calendar owner by email
|
"""add calendar owner by email
|
||||||
@ -316,4 +342,7 @@ class GoogleCalendar():
|
|||||||
},
|
},
|
||||||
'role': 'owner'
|
'role': 'owner'
|
||||||
}
|
}
|
||||||
return self.service.acl().insert(calendarId=self.calendarId, body=rule_owner).execute()
|
return self.service.acl().insert(
|
||||||
|
calendarId=self.calendarId,
|
||||||
|
body=rule_owner
|
||||||
|
).execute()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user