Skip to content

Commit

Permalink
+ default credentials support
Browse files Browse the repository at this point in the history
  • Loading branch information
b4tman committed Feb 20, 2020
1 parent 3ecd669 commit d146eec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ wget https://raw.githubusercontent.com/b4tman/sync_ics2gcal/develop/sample-confi
* `start_from` - start date:
* full format datetime, `2018-04-03T13:23:25.000001Z` for example
* or just `now`
* `service_account` - service account filename
* *(Optional)* `service_account` - service account filename, remove it from config to use [default credentials](https://developers.google.com/identity/protocols/application-default-credentials)
* *(Optional)* `logging` - [config](https://docs.python.org/3.8/library/logging.config.html#dictionary-schema-details) to setup logging
* `google_id` - target google calendar id, `[email protected]` for example
* `source` - source `.ics` filename, `my-calendar.ics` for example

Expand Down
35 changes: 35 additions & 0 deletions sync_ics2gcal/gcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ class GoogleCalendarService():
service Resource
"""

@staticmethod
def default():
"""make service Resource from default credentials (authorize)
( https://developers.google.com/identity/protocols/application-default-credentials )
( https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#google.auth.default )
Returns:
service Resource
"""

scopes = ['https://www.googleapis.com/auth/calendar']
credentials, _ = google.auth.default(scopes=scopes)
service = discovery.build('calendar', 'v3', credentials=credentials)
return service

@staticmethod
def from_srv_acc_file(service_account_file):
"""make service Resource from service account filename (authorize)
Expand All @@ -27,6 +42,26 @@ def from_srv_acc_file(service_account_file):
scoped_credentials = credentials.with_scopes(scopes)
service = discovery.build('calendar', 'v3', credentials=scoped_credentials)
return service

@staticmethod
def from_config(config):
"""make service Resource from config dict
Arguments:
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 )
Returns:
service Resource
"""

if '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
Expand Down
3 changes: 1 addition & 2 deletions sync_ics2gcal/manage_calendars.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def main():
if 'logging' in config:
logging.config.dictConfig(config['logging'])

srv_acc_file = config['service_account']
service = GoogleCalendarService.from_srv_acc_file(srv_acc_file)
service = GoogleCalendarService.from_config(config)

if 'list' == args.command:
list_calendars(service)
Expand Down
3 changes: 1 addition & 2 deletions sync_ics2gcal/sync_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ def main():

calendarId = config['calendar']['google_id']
ics_filepath = config['calendar']['source']
srv_acc_file = config['service_account']

start = get_start_date(config['start_from'])

converter = CalendarConverter()
converter.load(ics_filepath)

service = GoogleCalendarService.from_srv_acc_file(srv_acc_file)
service = GoogleCalendarService.from_config(config)
gcalendar = GoogleCalendar(service, calendarId)

sync = CalendarSync(gcalendar, converter)
Expand Down

0 comments on commit d146eec

Please sign in to comment.