mirror of
https://github.com/b4tman/sync_ics2gcal
synced 2025-01-21 23:38:58 +00:00
commit
8b2f35b3a9
13
README.md
13
README.md
@ -63,18 +63,23 @@ wget https://raw.githubusercontent.com/b4tman/sync_ics2gcal/develop/sample-confi
|
|||||||
### Manage calendars
|
### Manage calendars
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
manage-ics2gcal <subcommand> [-h] [options]
|
manage-ics2gcal GROUP | COMMAND
|
||||||
```
|
```
|
||||||
|
|
||||||
subcomands:
|
**GROUPS**:
|
||||||
|
|
||||||
|
* **property** - get/set properties (see [CalendarList resource](https://developers.google.com/calendar/v3/reference/calendarList#resource)), subcommands:
|
||||||
|
- **get** - get calendar property
|
||||||
|
- **set** - set calendar property
|
||||||
|
|
||||||
|
**COMMANDS**:
|
||||||
|
|
||||||
* **list** - list calendars
|
* **list** - list calendars
|
||||||
* **create** - create calendar
|
* **create** - create calendar
|
||||||
* **add_owner** - add owner to calendar
|
* **add_owner** - add owner to calendar
|
||||||
* **remove** - remove calendar
|
* **remove** - remove calendar
|
||||||
* **rename** - rename calendar
|
* **rename** - rename calendar
|
||||||
* **get** - get calendar property (see [CalendarList resource](https://developers.google.com/calendar/v3/reference/calendarList#resource))
|
|
||||||
* **set** - set calendar property
|
|
||||||
|
|
||||||
Use **-h** for more info.
|
Use **-h** for more info.
|
||||||
|
|
||||||
|
@ -3,3 +3,4 @@ google-api-python-client==2.3.0
|
|||||||
icalendar==4.0.7
|
icalendar==4.0.7
|
||||||
pytz==2021.1
|
pytz==2021.1
|
||||||
PyYAML==5.4.1
|
PyYAML==5.4.1
|
||||||
|
fire==0.4.0
|
@ -1,80 +1,16 @@
|
|||||||
import argparse
|
|
||||||
import logging.config
|
import logging.config
|
||||||
from typing import Optional, Dict, Any
|
from typing import Optional, Dict, Any, List
|
||||||
|
|
||||||
|
import fire
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from . import GoogleCalendar, GoogleCalendarService
|
from . import GoogleCalendar, GoogleCalendarService
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def load_config(filename: str) -> Optional[Dict[str, Any]]:
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
description="manage google calendars in service account")
|
|
||||||
command_subparsers = parser.add_subparsers(help='command', dest='command')
|
|
||||||
# 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(
|
|
||||||
'create', help='create calendar')
|
|
||||||
parser_create.add_argument(
|
|
||||||
'summary', action='store', help='new calendar summary')
|
|
||||||
parser_create.add_argument('--timezone', action='store',
|
|
||||||
default=None, required=False,
|
|
||||||
help='new calendar timezone')
|
|
||||||
parser_create.add_argument(
|
|
||||||
'--public', default=False,
|
|
||||||
action='store_true', help='make calendar public')
|
|
||||||
# add_owner
|
|
||||||
parser_add_owner = command_subparsers.add_parser(
|
|
||||||
'add_owner', help='add owner to calendar')
|
|
||||||
parser_add_owner.add_argument('id', action='store', help='calendar id')
|
|
||||||
parser_add_owner.add_argument(
|
|
||||||
'owner_email', action='store', help='new owner email')
|
|
||||||
# remove
|
|
||||||
parser_remove = command_subparsers.add_parser(
|
|
||||||
'remove', help='remove calendar')
|
|
||||||
parser_remove.add_argument(
|
|
||||||
'id', action='store', help='calendar id to remove')
|
|
||||||
# rename
|
|
||||||
parser_rename = command_subparsers.add_parser(
|
|
||||||
'rename', help='rename calendar')
|
|
||||||
parser_rename.add_argument(
|
|
||||||
'id', action='store', help='calendar id')
|
|
||||||
parser_rename.add_argument(
|
|
||||||
'summary', action='store', help='new summary')
|
|
||||||
# get
|
|
||||||
parser_get = command_subparsers.add_parser(
|
|
||||||
'get', help='get calendar property')
|
|
||||||
parser_get.add_argument(
|
|
||||||
'id', action='store', help='calendar id')
|
|
||||||
parser_get.add_argument(
|
|
||||||
'property', action='store', help='property key')
|
|
||||||
# set
|
|
||||||
parser_set = command_subparsers.add_parser(
|
|
||||||
'set', help='set calendar property')
|
|
||||||
parser_set.add_argument(
|
|
||||||
'id', action='store', help='calendar id')
|
|
||||||
parser_set.add_argument(
|
|
||||||
'property', action='store', help='property key')
|
|
||||||
parser_set.add_argument(
|
|
||||||
'property_value', action='store', help='property value')
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
if args.command is None:
|
|
||||||
parser.print_usage()
|
|
||||||
return args
|
|
||||||
|
|
||||||
|
|
||||||
def load_config() -> Optional[Dict[str, Any]]:
|
|
||||||
result = None
|
result = None
|
||||||
try:
|
try:
|
||||||
with open('config.yml', 'r', encoding='utf-8') as f:
|
with open(filename, 'r', encoding='utf-8') as f:
|
||||||
result = yaml.safe_load(f)
|
result = yaml.safe_load(f)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
@ -82,87 +18,126 @@ def load_config() -> Optional[Dict[str, Any]]:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def list_calendars(service, show_hidden: bool, show_deleted: bool) -> None:
|
class PropertyCommands:
|
||||||
fields = 'nextPageToken,items(id,summary)'
|
""" get/set google calendar properties """
|
||||||
calendars = []
|
|
||||||
page_token = None
|
def __init__(self, _service):
|
||||||
while True:
|
self._service = _service
|
||||||
response = service.calendarList().list(fields=fields,
|
|
||||||
pageToken=page_token,
|
def get(self, calendar_id: str, property_name: str) -> None:
|
||||||
showHidden=show_hidden,
|
""" get calendar property
|
||||||
showDeleted=show_deleted
|
|
||||||
).execute()
|
Args:
|
||||||
if 'items' in response:
|
calendar_id: calendar id
|
||||||
calendars.extend(response['items'])
|
property_name: property key
|
||||||
page_token = response.get('nextPageToken')
|
"""
|
||||||
if not page_token:
|
response = self._service.calendarList().get(calendarId=calendar_id,
|
||||||
break
|
fields=property_name).execute()
|
||||||
for calendar in calendars:
|
print(response.get(property_name))
|
||||||
print('{summary}: {id}'.format_map(calendar))
|
|
||||||
|
def set(self, calendar_id: str, property_name: str, property_value: str) -> None:
|
||||||
|
""" set calendar property
|
||||||
|
|
||||||
|
Args:
|
||||||
|
calendar_id: calendar id
|
||||||
|
property_name: property key
|
||||||
|
property_value: property value
|
||||||
|
"""
|
||||||
|
body = {property_name: property_value}
|
||||||
|
response = self._service.calendarList().patch(body=body, calendarId=calendar_id).execute()
|
||||||
|
print(response)
|
||||||
|
|
||||||
|
|
||||||
def create_calendar(service, summary: str, timezone: str, public: bool) -> None:
|
class Commands:
|
||||||
calendar = GoogleCalendar(service, None)
|
""" manage google calendars in service account """
|
||||||
calendar.create(summary, timezone)
|
|
||||||
if public:
|
|
||||||
calendar.make_public()
|
|
||||||
print('{}: {}'.format(summary, calendar.calendarId))
|
|
||||||
|
|
||||||
|
def __init__(self, config: str = 'config.yml'):
|
||||||
|
"""
|
||||||
|
|
||||||
def add_owner(service, calendar_id: str, owner_email: str) -> None:
|
Args:
|
||||||
calendar = GoogleCalendar(service, calendar_id)
|
config(str): config filename
|
||||||
calendar.add_owner(owner_email)
|
"""
|
||||||
print('to {} added owner: {}'.format(calendar_id, owner_email))
|
self._config: Optional[Dict[str, Any]] = load_config(config)
|
||||||
|
if self._config is not None and 'logging' in self._config:
|
||||||
|
logging.config.dictConfig(self._config['logging'])
|
||||||
|
self._service = GoogleCalendarService.from_config(self._config)
|
||||||
|
self.property = PropertyCommands(self._service)
|
||||||
|
|
||||||
|
def list(self, show_hidden: bool = False, show_deleted: bool = False) -> None:
|
||||||
|
""" list calendars
|
||||||
|
|
||||||
def remove_calendar(service, calendar_id: str) -> None:
|
Args:
|
||||||
calendar = GoogleCalendar(service, calendar_id)
|
show_hidden: show hidden calendars
|
||||||
calendar.delete()
|
show_deleted: show deleted calendars
|
||||||
print('removed: {}'.format(calendar_id))
|
"""
|
||||||
|
|
||||||
|
fields: str = 'nextPageToken,items(id,summary)'
|
||||||
|
calendars: List[Dict[str, Any]] = []
|
||||||
|
page_token: Optional[str] = None
|
||||||
|
while True:
|
||||||
|
calendars_api = self._service.calendarList()
|
||||||
|
response = calendars_api.list(fields=fields,
|
||||||
|
pageToken=page_token,
|
||||||
|
showHidden=show_hidden,
|
||||||
|
showDeleted=show_deleted
|
||||||
|
).execute()
|
||||||
|
if 'items' in response:
|
||||||
|
calendars.extend(response['items'])
|
||||||
|
page_token = response.get('nextPageToken')
|
||||||
|
if page_token is None:
|
||||||
|
break
|
||||||
|
for calendar in calendars:
|
||||||
|
print('{summary}: {id}'.format_map(calendar))
|
||||||
|
|
||||||
def rename_calendar(service, calendar_id: str, summary: str) -> None:
|
def create(self, summary: str, timezone: Optional[str] = None, public: bool = False) -> None:
|
||||||
calendar = {'summary': summary}
|
""" create calendar
|
||||||
service.calendars().patch(body=calendar, calendarId=calendar_id).execute()
|
|
||||||
print('{}: {}'.format(summary, calendar_id))
|
|
||||||
|
|
||||||
|
Args:
|
||||||
|
summary: new calendar summary
|
||||||
|
timezone: new calendar timezone
|
||||||
|
public: make calendar public
|
||||||
|
"""
|
||||||
|
calendar = GoogleCalendar(self._service, None)
|
||||||
|
calendar.create(summary, timezone)
|
||||||
|
if public:
|
||||||
|
calendar.make_public()
|
||||||
|
print('{}: {}'.format(summary, calendar.calendarId))
|
||||||
|
|
||||||
def get_calendar_property(service, calendar_id: str, property_name: str) -> None:
|
def add_owner(self, calendar_id: str, email: str) -> None:
|
||||||
response = service.calendarList().get(calendarId=calendar_id,
|
""" add owner to calendar
|
||||||
fields=property_name).execute()
|
|
||||||
print(response.get(property_name))
|
|
||||||
|
|
||||||
|
Args:
|
||||||
|
calendar_id: calendar id
|
||||||
|
email: new owner email
|
||||||
|
"""
|
||||||
|
calendar = GoogleCalendar(self._service, calendar_id)
|
||||||
|
calendar.add_owner(email)
|
||||||
|
print('to {} added owner: {}'.format(calendar_id, email))
|
||||||
|
|
||||||
def set_calendar_property(service, calendar_id: str, property_name: str, property_value: str) -> None:
|
def remove(self, calendar_id: str) -> None:
|
||||||
body = {property_name: property_value}
|
""" remove calendar
|
||||||
response = service.calendarList().patch(body=body, calendarId=calendar_id).execute()
|
|
||||||
print(response)
|
Args:
|
||||||
|
calendar_id: calendar id
|
||||||
|
"""
|
||||||
|
calendar = GoogleCalendar(self._service, calendar_id)
|
||||||
|
calendar.delete()
|
||||||
|
print('removed: {}'.format(calendar_id))
|
||||||
|
|
||||||
|
def rename(self, calendar_id: str, summary: str) -> None:
|
||||||
|
""" rename calendar
|
||||||
|
|
||||||
|
Args:
|
||||||
|
calendar_id: calendar id
|
||||||
|
summary:
|
||||||
|
"""
|
||||||
|
calendar = {'summary': summary}
|
||||||
|
self._service.calendars().patch(body=calendar, calendarId=calendar_id).execute()
|
||||||
|
print('{}: {}'.format(summary, calendar_id))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
fire.Fire(Commands, name='manage-ics2gcal')
|
||||||
config = load_config()
|
|
||||||
|
|
||||||
if config is not None and 'logging' in config:
|
|
||||||
logging.config.dictConfig(config['logging'])
|
|
||||||
|
|
||||||
service = GoogleCalendarService.from_config(config)
|
|
||||||
|
|
||||||
if 'list' == args.command:
|
|
||||||
list_calendars(service, args.show_hidden, args.show_deleted)
|
|
||||||
elif 'create' == args.command:
|
|
||||||
create_calendar(service, args.summary, args.timezone, args.public)
|
|
||||||
elif 'add_owner' == args.command:
|
|
||||||
add_owner(service, args.id, args.owner_email)
|
|
||||||
elif 'remove' == args.command:
|
|
||||||
remove_calendar(service, args.id)
|
|
||||||
elif 'rename' == args.command:
|
|
||||||
rename_calendar(service, args.id, args.summary)
|
|
||||||
elif 'get' == args.command:
|
|
||||||
get_calendar_property(service, args.id, args.property)
|
|
||||||
elif 'set' == args.command:
|
|
||||||
set_calendar_property(
|
|
||||||
service, args.id, args.property, args.property_value)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user