lint gcal

This commit is contained in:
Dmitry Belyaev 2020-03-07 18:26:37 +03:00
parent 8d64869f06
commit 9e74772852
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
1 changed files with 56 additions and 27 deletions

View File

@ -1,5 +1,4 @@
import logging
import sys
import google.auth
from google.oauth2 import service_account
@ -26,9 +25,10 @@ class GoogleCalendarService():
scopes = ['https://www.googleapis.com/auth/calendar']
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
@staticmethod
def from_srv_acc_file(service_account_file):
"""make service Resource from service account filename (authorize)
@ -38,11 +38,14 @@ class GoogleCalendarService():
"""
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)
service = discovery.build('calendar', 'v3', credentials=scoped_credentials, cache_discovery=False)
service = discovery.build(
'calendar', 'v3', credentials=scoped_credentials,
cache_discovery=False)
return service
@staticmethod
def from_config(config=None):
"""make service Resource from config dict
@ -51,25 +54,27 @@ class GoogleCalendarService():
config -- dict() config with keys:
(optional) service_account: - service account filename
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
Returns:
service Resource
"""
if (not config is None) and 'service_account' in config:
service = GoogleCalendarService.from_srv_acc_file(config['service_account'])
if config is not None and 'service_account' in config:
service = GoogleCalendarService.from_srv_acc_file(
config['service_account'])
else:
service = GoogleCalendarService.default()
return service
def select_event_key(event):
"""select event key for logging
Arguments:
event -- event resource
Returns:
key name or None if no key found
"""
@ -94,11 +99,11 @@ class GoogleCalendar():
def _make_request_callback(self, action, events_by_req):
"""make callback for log result of batch request
Arguments:
action -- action name
events_by_req -- list of events ordered by request id
Returns:
callback function
"""
@ -108,8 +113,10 @@ class GoogleCalendar():
key = select_event_key(event)
if exception is not None:
self.logger.error('failed to %s event with %s: %s, exception: %s',
action, key, event.get(key), str(exception))
self.logger.error(
'failed to %s event with %s: %s, exception: %s',
action, key, event.get(key), str(exception)
)
else:
resp_key = select_event_key(response)
if resp_key is not None:
@ -128,8 +135,11 @@ class GoogleCalendar():
timeMin = utc.normalize(start.astimezone(utc)).replace(
tzinfo=None).isoformat() + 'Z'
while True:
response = self.service.events().list(calendarId=self.calendarId, pageToken=page_token,
singleEvents=True, timeMin=timeMin, fields=fields).execute()
response = self.service.events().list(calendarId=self.calendarId,
pageToken=page_token,
singleEvents=True,
timeMin=timeMin,
fields=fields).execute()
if 'items' in response:
events.extend(response['items'])
page_token = response.get('nextPageToken')
@ -160,8 +170,9 @@ class GoogleCalendar():
if exception is None:
found = ([] != response['items'])
else:
self.logger.error('exception %s, while listing event with UID: %s', str(
exception), event['iCalUID'])
self.logger.error(
'exception %s, while listing event with UID: %s',
str(exception), event['iCalUID'])
if found:
exists.append(
(event, response['items'][0]))
@ -173,7 +184,12 @@ class GoogleCalendar():
for event in events:
events_by_req.append(event)
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
batch.execute()
self.logger.info('%d events exists, %d not found',
@ -196,7 +212,9 @@ class GoogleCalendar():
for event in events:
events_by_req.append(event)
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
batch.execute()
@ -218,7 +236,8 @@ class GoogleCalendar():
continue
events_by_req.append(event_new)
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
batch.execute()
@ -240,7 +259,8 @@ class GoogleCalendar():
continue
events_by_req.append(event_new)
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
batch.execute()
@ -259,7 +279,8 @@ class GoogleCalendar():
for event in events:
events_by_req.append(event)
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
batch.execute()
@ -280,7 +301,9 @@ class GoogleCalendar():
if timeZone is not None:
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']
return created_calendar
@ -300,7 +323,10 @@ class GoogleCalendar():
},
'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):
"""add calendar owner by email
@ -316,4 +342,7 @@ class GoogleCalendar():
},
'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()