+ manage_calendars: list hidden & deleted

This commit is contained in:
Dmitry Belyaev 2020-03-07 18:05:51 +03:00
parent b0a39a1b8c
commit ab00cb09c8
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
1 changed files with 35 additions and 15 deletions

View File

@ -12,7 +12,13 @@ def parse_args():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="manage google calendars in service account") description="manage google calendars in service account")
command_subparsers = parser.add_subparsers(help='command', dest='command') command_subparsers = parser.add_subparsers(help='command', dest='command')
command_subparsers.add_parser('list', help='list calendars') # list
parser_list = command_subparsers.add_parser('list', help='list calendars')
parser_list.add_argument(
'--show-hidden', default=False, action='store_true', help='show hidden calendars')
parser_list.add_argument(
'--show-deleted', default=False, action='store_true', help='show deleted calendars')
# create
parser_create = command_subparsers.add_parser( parser_create = command_subparsers.add_parser(
'create', help='create calendar') 'create', help='create calendar')
parser_create.add_argument( parser_create.add_argument(
@ -21,36 +27,41 @@ def parse_args():
default=None, required=False, help='new calendar timezone') default=None, required=False, help='new calendar timezone')
parser_create.add_argument( parser_create.add_argument(
'--public', default=False, action='store_true', help='make calendar public') '--public', default=False, action='store_true', help='make calendar public')
# add_owner
parser_add_owner = command_subparsers.add_parser( parser_add_owner = command_subparsers.add_parser(
'add_owner', help='add owner to calendar') 'add_owner', help='add owner to calendar')
parser_add_owner.add_argument('id', action='store', help='calendar id') parser_add_owner.add_argument('id', action='store', help='calendar id')
parser_add_owner.add_argument( parser_add_owner.add_argument(
'owner_email', action='store', help='new owner email') 'owner_email', action='store', help='new owner email')
# remove
parser_remove = command_subparsers.add_parser( parser_remove = command_subparsers.add_parser(
'remove', help='remove calendar') 'remove', help='remove calendar')
parser_remove.add_argument( parser_remove.add_argument(
'id', action='store', help='calendar id to remove') 'id', action='store', help='calendar id to remove')
# rename
parser_rename = command_subparsers.add_parser( parser_rename = command_subparsers.add_parser(
'rename', help='rename calendar') 'rename', help='rename calendar')
parser_rename.add_argument( parser_rename.add_argument(
'id', action='store', help='calendar id') 'id', action='store', help='calendar id')
parser_rename.add_argument( parser_rename.add_argument(
'summary', action='store', help='new summary') 'summary', action='store', help='new summary')
# get
parser_get = command_subparsers.add_parser( parser_get = command_subparsers.add_parser(
'get', help='get calendar property') 'get', help='get calendar property')
parser_get.add_argument( parser_get.add_argument(
'id', action='store', help='calendar id') 'id', action='store', help='calendar id')
parser_get.add_argument( parser_get.add_argument(
'property', action='store', help='property key') 'property', action='store', help='property key')
parser_get = command_subparsers.add_parser( # set
parser_set = command_subparsers.add_parser(
'set', help='set calendar property') 'set', help='set calendar property')
parser_get.add_argument( parser_set.add_argument(
'id', action='store', help='calendar id') 'id', action='store', help='calendar id')
parser_get.add_argument( parser_set.add_argument(
'property', action='store', help='property key') 'property', action='store', help='property key')
parser_get.add_argument( parser_set.add_argument(
'property_value', action='store', help='property value') 'property_value', action='store', help='property value')
args = parser.parse_args() args = parser.parse_args()
if args.command is None: if args.command is None:
parser.print_usage() parser.print_usage()
@ -64,20 +75,23 @@ def load_config():
result = yaml.safe_load(f) result = yaml.safe_load(f)
except FileNotFoundError: except FileNotFoundError:
pass pass
return result return result
def list_calendars(service): def list_calendars(service, show_hidden, show_deleted):
calendars = [] calendars = []
page_token = None page_token = None
while True: while True:
response = service.calendarList().list(fields='nextPageToken,items(id,summary)', pageToken=page_token).execute() response = service.calendarList().list(fields='nextPageToken,items(id,summary)',
pageToken=page_token,
showHidden=show_hidden,
showDeleted=show_deleted).execute()
if 'items' in response: if 'items' in response:
calendars.extend(response['items']) calendars.extend(response['items'])
page_token = response.get('nextPageToken') page_token = response.get('nextPageToken')
if not page_token: if not page_token:
break break
for calendar in calendars: for calendar in calendars:
print('{summary}: {id}'.format_map(calendar)) print('{summary}: {id}'.format_map(calendar))
@ -101,20 +115,24 @@ def remove_calendar(service, id):
calendar.delete() calendar.delete()
print('removed: {}'.format(id)) print('removed: {}'.format(id))
def rename_calendar(service, id, summary): def rename_calendar(service, id, summary):
calendar = {'summary': summary} calendar = {'summary': summary}
service.calendars().patch(body=calendar, calendarId=id).execute() service.calendars().patch(body=calendar, calendarId=id).execute()
print('{}: {}'.format(summary, id)) print('{}: {}'.format(summary, id))
def get_calendar_property(service, id, property): def get_calendar_property(service, id, property):
response = service.calendarList().get(calendarId=id, fields=property).execute() response = service.calendarList().get(calendarId=id, fields=property).execute()
print(response.get(property)) print(response.get(property))
def set_calendar_property(service, id, property, property_value): def set_calendar_property(service, id, property, property_value):
body = {property: property_value} body = {property: property_value}
response = service.calendarList().patch(body=body, calendarId=id).execute() response = service.calendarList().patch(body=body, calendarId=id).execute()
print(response) print(response)
def main(): def main():
args = parse_args() args = parse_args()
config = load_config() config = load_config()
@ -125,7 +143,7 @@ def main():
service = GoogleCalendarService.from_config(config) service = GoogleCalendarService.from_config(config)
if 'list' == args.command: if 'list' == args.command:
list_calendars(service) list_calendars(service, args.show_hidden, args.show_deleted)
elif 'create' == args.command: elif 'create' == args.command:
create_calendar(service, args.summary, args.timezone, args.public) create_calendar(service, args.summary, args.timezone, args.public)
elif 'add_owner' == args.command: elif 'add_owner' == args.command:
@ -137,7 +155,9 @@ def main():
elif 'get' == args.command: elif 'get' == args.command:
get_calendar_property(service, args.id, args.property) get_calendar_property(service, args.id, args.property)
elif 'set' == args.command: elif 'set' == args.command:
set_calendar_property(service, args.id, args.property, args.property_value) set_calendar_property(
service, args.id, args.property, args.property_value)
if __name__ == '__main__': if __name__ == '__main__':
main() main()