1
0
mirror of https://github.com/b4tman/sync_ics2gcal synced 2025-02-01 20:28:30 +00:00

move scripts

This commit is contained in:
Dmitry Belyaev 2020-02-19 13:15:20 +03:00
parent 2af3eeda5b
commit 98dd00905a
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
3 changed files with 162 additions and 158 deletions

View File

@ -32,5 +32,10 @@ setuptools.setup(
'pytz', 'pytz',
'PyYAML>=3.13' 'PyYAML>=3.13'
], ],
scripts=['manage-calendars.py', 'sync-calendar.py'] entry_points={
"console_scripts": [
"sync-ics2gcal = sync_ics2gcal.sync_calendar:main",
"manage-ics2gcal = sync_ics2gcal.manage_calendars:main",
]
}
) )

View File

@ -1,104 +1,104 @@
import argparse import argparse
import datetime import datetime
import logging.config import logging.config
import yaml import yaml
from pytz import utc from pytz import utc
from sync_ics2gcal import GoogleCalendar, GoogleCalendarService from . import GoogleCalendar, GoogleCalendarService
def parse_args(): 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') command_subparsers.add_parser('list', help='list calendars')
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(
'summary', action='store', help='new calendar summary') 'summary', action='store', help='new calendar summary')
parser_create.add_argument('--timezone', action='store', parser_create.add_argument('--timezone', action='store',
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')
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')
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')
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')
args = parser.parse_args() args = parser.parse_args()
if args.command is None: if args.command is None:
parser.print_usage() parser.print_usage()
return args return args
def load_config(): def load_config():
with open('config.yml', 'r', encoding='utf-8') as f: with open('config.yml', 'r', encoding='utf-8') as f:
result = yaml.safe_load(f) result = yaml.safe_load(f)
return result return result
def list_calendars(service): def list_calendars(service):
response = service.calendarList().list(fields='items(id,summary)').execute() response = service.calendarList().list(fields='items(id,summary)').execute()
for calendar in response.get('items'): for calendar in response.get('items'):
print('{summary}: {id}'.format_map(calendar)) print('{summary}: {id}'.format_map(calendar))
def create_calendar(service, summary, timezone, public): def create_calendar(service, summary, timezone, public):
calendar = GoogleCalendar(service, None) calendar = GoogleCalendar(service, None)
calendar.create(summary, timezone) calendar.create(summary, timezone)
if public: if public:
calendar.make_public() calendar.make_public()
print('{}: {}'.format(summary, calendar.calendarId)) print('{}: {}'.format(summary, calendar.calendarId))
def add_owner(service, id, owner_email): def add_owner(service, id, owner_email):
calendar = GoogleCalendar(service, id) calendar = GoogleCalendar(service, id)
calendar.add_owner(owner_email) calendar.add_owner(owner_email)
print('to {} added owner: {}'.format(id, owner_email)) print('to {} added owner: {}'.format(id, owner_email))
def remove_calendar(service, id): def remove_calendar(service, id):
calendar = GoogleCalendar(service, id) calendar = GoogleCalendar(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 main(): def main():
args = parse_args() args = parse_args()
config = load_config() config = load_config()
if 'logging' in config: if 'logging' in config:
logging.config.dictConfig(config['logging']) logging.config.dictConfig(config['logging'])
srv_acc_file = config['service_account'] srv_acc_file = config['service_account']
service = GoogleCalendarService.from_srv_acc_file(srv_acc_file) service = GoogleCalendarService.from_srv_acc_file(srv_acc_file)
if 'list' == args.command: if 'list' == args.command:
list_calendars(service) list_calendars(service)
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:
add_owner(service, args.id, args.owner_email) add_owner(service, args.id, args.owner_email)
elif 'remove' == args.command: elif 'remove' == args.command:
remove_calendar(service, args.id) remove_calendar(service, args.id)
elif 'rename' == args.command: elif 'rename' == args.command:
rename_calendar(service, args.id, args.summary) rename_calendar(service, args.id, args.summary)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,53 +1,52 @@
import yaml import yaml
import dateutil.parser import dateutil.parser
import datetime import datetime
import logging import logging
import logging.config import logging.config
from sync_ics2gcal import ( from . import (
CalendarConverter, CalendarConverter,
GoogleCalendarService, GoogleCalendarService,
GoogleCalendar, GoogleCalendar,
CalendarSync CalendarSync
) )
def load_config():
def load_config(): with open('config.yml', 'r', encoding='utf-8') as f:
with open('config.yml', 'r', encoding='utf-8') as f: result = yaml.safe_load(f)
result = yaml.safe_load(f) return result
return result
def get_start_date(date_str):
def get_start_date(date_str): result = datetime.datetime(1,1,1)
result = datetime.datetime(1,1,1) if 'now' == date_str:
if 'now' == date_str: result = datetime.datetime.utcnow()
result = datetime.datetime.utcnow() else:
else: result = dateutil.parser.parse(date_str)
result = dateutil.parser.parse(date_str) return result
return result
def main():
def main(): config = load_config()
config = load_config()
if 'logging' in config:
if 'logging' in config: logging.config.dictConfig(config['logging'])
logging.config.dictConfig(config['logging'])
calendarId = config['calendar']['google_id']
calendarId = config['calendar']['google_id'] ics_filepath = config['calendar']['source']
ics_filepath = config['calendar']['source'] srv_acc_file = config['service_account']
srv_acc_file = config['service_account']
start = get_start_date(config['start_from'])
start = get_start_date(config['start_from'])
converter = CalendarConverter()
converter = CalendarConverter() converter.load(ics_filepath)
converter.load(ics_filepath)
service = GoogleCalendarService.from_srv_acc_file(srv_acc_file)
service = GoogleCalendarService.from_srv_acc_file(srv_acc_file) gcalendar = GoogleCalendar(service, calendarId)
gcalendar = GoogleCalendar(service, calendarId)
sync = CalendarSync(gcalendar, converter)
sync = CalendarSync(gcalendar, converter) sync.prepare_sync(start)
sync.prepare_sync(start) sync.apply()
sync.apply()
if __name__ == '__main__':
if __name__ == '__main__': main()
main()