mirror of
https://github.com/b4tman/sync_ics2gcal
synced 2025-01-21 07:28:24 +00:00
fix type errors
with mypy --strict
This commit is contained in:
parent
ad634e9c6e
commit
80e15b0622
@ -89,6 +89,9 @@ class EventsSearchResults(NamedTuple):
|
|||||||
new: List[EventData]
|
new: List[EventData]
|
||||||
|
|
||||||
|
|
||||||
|
BatchRequestCallback = Callable[[str, Any, Optional[Exception]], None]
|
||||||
|
|
||||||
|
|
||||||
class GoogleCalendarService:
|
class GoogleCalendarService:
|
||||||
"""class for make google calendar service Resource
|
"""class for make google calendar service Resource
|
||||||
|
|
||||||
@ -97,7 +100,7 @@ class GoogleCalendarService:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def default():
|
def default() -> discovery.Resource:
|
||||||
"""make service Resource from default credentials (authorize)
|
"""make service Resource from default credentials (authorize)
|
||||||
( https://developers.google.com/identity/protocols/application-default-credentials )
|
( https://developers.google.com/identity/protocols/application-default-credentials )
|
||||||
( https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#google.auth.default )
|
( https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#google.auth.default )
|
||||||
@ -111,7 +114,7 @@ class GoogleCalendarService:
|
|||||||
return service
|
return service
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_srv_acc_file(service_account_file: str):
|
def from_srv_acc_file(service_account_file: str) -> discovery.Resource:
|
||||||
"""make service Resource from service account filename (authorize)"""
|
"""make service Resource from service account filename (authorize)"""
|
||||||
|
|
||||||
scopes = ["https://www.googleapis.com/auth/calendar"]
|
scopes = ["https://www.googleapis.com/auth/calendar"]
|
||||||
@ -125,7 +128,7 @@ class GoogleCalendarService:
|
|||||||
return service
|
return service
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_config(config: Optional[Dict[str, Optional[str]]] = None):
|
def from_config(config: Optional[Dict[str, str]] = None) -> discovery.Resource:
|
||||||
"""make service Resource from config dict
|
"""make service Resource from config dict
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -137,7 +140,7 @@ class GoogleCalendarService:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if config is not None and "service_account" in config:
|
if config is not None and "service_account" in config:
|
||||||
service_account_filename: str = str(config["service_account"])
|
service_account_filename: str = config["service_account"]
|
||||||
service = GoogleCalendarService.from_srv_acc_file(service_account_filename)
|
service = GoogleCalendarService.from_srv_acc_file(service_account_filename)
|
||||||
else:
|
else:
|
||||||
service = GoogleCalendarService.default()
|
service = GoogleCalendarService.default()
|
||||||
@ -171,7 +174,9 @@ class GoogleCalendar:
|
|||||||
self.service: discovery.Resource = service
|
self.service: discovery.Resource = service
|
||||||
self.calendar_id: str = str(calendar_id)
|
self.calendar_id: str = str(calendar_id)
|
||||||
|
|
||||||
def _make_request_callback(self, action: str, events_by_req: EventList) -> Callable:
|
def _make_request_callback(
|
||||||
|
self, action: str, events_by_req: EventList
|
||||||
|
) -> BatchRequestCallback:
|
||||||
"""make callback for log result of batch request
|
"""make callback for log result of batch request
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -182,7 +187,9 @@ class GoogleCalendar:
|
|||||||
callback function
|
callback function
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def callback(request_id: str, response: Any, exception: Optional[Exception]):
|
def callback(
|
||||||
|
request_id: str, response: Any, exception: Optional[Exception]
|
||||||
|
) -> None:
|
||||||
event: EventData = events_by_req[int(request_id)]
|
event: EventData = events_by_req[int(request_id)]
|
||||||
event_key: Optional[str] = select_event_key(event)
|
event_key: Optional[str] = select_event_key(event)
|
||||||
key: str = event_key if event_key is not None else ""
|
key: str = event_key if event_key is not None else ""
|
||||||
@ -232,7 +239,7 @@ class GoogleCalendar:
|
|||||||
self.logger.info("%d events listed", len(events))
|
self.logger.info("%d events listed", len(events))
|
||||||
return events
|
return events
|
||||||
|
|
||||||
def find_exists(self, events: List) -> EventsSearchResults:
|
def find_exists(self, events: EventList) -> EventsSearchResults:
|
||||||
"""find existing events from list, by 'iCalUID' field
|
"""find existing events from list, by 'iCalUID' field
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -250,7 +257,7 @@ class GoogleCalendar:
|
|||||||
|
|
||||||
def list_callback(
|
def list_callback(
|
||||||
request_id: str, response: Any, exception: Optional[Exception]
|
request_id: str, response: Any, exception: Optional[Exception]
|
||||||
):
|
) -> None:
|
||||||
found: bool = False
|
found: bool = False
|
||||||
cur_event: EventData = events_by_req[int(request_id)]
|
cur_event: EventData = events_by_req[int(request_id)]
|
||||||
if exception is None:
|
if exception is None:
|
||||||
@ -284,7 +291,7 @@ class GoogleCalendar:
|
|||||||
self.logger.info("%d events exists, %d not found", len(exists), len(not_found))
|
self.logger.info("%d events exists, %d not found", len(exists), len(not_found))
|
||||||
return EventsSearchResults(exists, not_found)
|
return EventsSearchResults(exists, not_found)
|
||||||
|
|
||||||
def insert_events(self, events: EventList):
|
def insert_events(self, events: EventList) -> None:
|
||||||
"""insert list of events
|
"""insert list of events
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -308,7 +315,7 @@ class GoogleCalendar:
|
|||||||
i += 1
|
i += 1
|
||||||
batch.execute()
|
batch.execute()
|
||||||
|
|
||||||
def patch_events(self, event_tuples: List[EventTuple]):
|
def patch_events(self, event_tuples: List[EventTuple]) -> None:
|
||||||
"""patch (update) events
|
"""patch (update) events
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -335,7 +342,7 @@ class GoogleCalendar:
|
|||||||
i += 1
|
i += 1
|
||||||
batch.execute()
|
batch.execute()
|
||||||
|
|
||||||
def update_events(self, event_tuples: List[EventTuple]):
|
def update_events(self, event_tuples: List[EventTuple]) -> None:
|
||||||
"""update events
|
"""update events
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -364,7 +371,7 @@ class GoogleCalendar:
|
|||||||
i += 1
|
i += 1
|
||||||
batch.execute()
|
batch.execute()
|
||||||
|
|
||||||
def delete_events(self, events: EventList):
|
def delete_events(self, events: EventList) -> None:
|
||||||
"""delete events
|
"""delete events
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -400,7 +407,7 @@ class GoogleCalendar:
|
|||||||
calendar Resource
|
calendar Resource
|
||||||
"""
|
"""
|
||||||
|
|
||||||
calendar: CalendarData = CalendarData(summary=summary)
|
calendar = CalendarData(summary=summary)
|
||||||
if time_zone is not None:
|
if time_zone is not None:
|
||||||
calendar["timeZone"] = time_zone
|
calendar["timeZone"] = time_zone
|
||||||
|
|
||||||
@ -408,33 +415,27 @@ class GoogleCalendar:
|
|||||||
self.calendar_id = created_calendar["id"]
|
self.calendar_id = created_calendar["id"]
|
||||||
return created_calendar
|
return created_calendar
|
||||||
|
|
||||||
def delete(self):
|
def delete(self) -> None:
|
||||||
"""delete calendar"""
|
"""delete calendar"""
|
||||||
|
|
||||||
self.service.calendars().delete(calendarId=self.calendar_id).execute()
|
self.service.calendars().delete(calendarId=self.calendar_id).execute()
|
||||||
|
|
||||||
def make_public(self):
|
def make_public(self) -> None:
|
||||||
"""make calendar public"""
|
"""make calendar public"""
|
||||||
|
|
||||||
rule_public: ACLRule = ACLRule(scope=ACLScope(type="default"), role="reader")
|
rule_public = ACLRule(scope=ACLScope(type="default"), role="reader")
|
||||||
return (
|
self.service.acl().insert(
|
||||||
self.service.acl()
|
calendarId=self.calendar_id, body=rule_public
|
||||||
.insert(calendarId=self.calendar_id, body=rule_public)
|
).execute()
|
||||||
.execute()
|
|
||||||
)
|
|
||||||
|
|
||||||
def add_owner(self, email: str):
|
def add_owner(self, email: str) -> None:
|
||||||
"""add calendar owner by email
|
"""add calendar owner by email
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
email -- email to add
|
email -- email to add
|
||||||
"""
|
"""
|
||||||
|
|
||||||
rule_owner: ACLRule = ACLRule(
|
rule_owner = ACLRule(scope=ACLScope(type="user", value=email), role="owner")
|
||||||
scope=ACLScope(type="user", value=email), role="owner"
|
self.service.acl().insert(
|
||||||
)
|
calendarId=self.calendar_id, body=rule_owner
|
||||||
return (
|
).execute()
|
||||||
self.service.acl()
|
|
||||||
.insert(calendarId=self.calendar_id, body=rule_owner)
|
|
||||||
.execute()
|
|
||||||
)
|
|
||||||
|
@ -60,7 +60,7 @@ def gcal_date_or_datetime(
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
class EventConverter(Event):
|
class EventConverter(Event): # type: ignore
|
||||||
"""Convert icalendar event to google calendar resource
|
"""Convert icalendar event to google calendar resource
|
||||||
( https://developers.google.com/calendar/v3/reference/events#resource-representations )
|
( https://developers.google.com/calendar/v3/reference/events#resource-representations )
|
||||||
"""
|
"""
|
||||||
@ -75,7 +75,7 @@ class EventConverter(Event):
|
|||||||
string value
|
string value
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self.decoded(prop).decode(encoding="utf-8")
|
return str(self.decoded(prop).decode(encoding="utf-8"))
|
||||||
|
|
||||||
def _datetime_str_prop(self, prop: str) -> str:
|
def _datetime_str_prop(self, prop: str) -> str:
|
||||||
"""utc datetime as string from property
|
"""utc datetime as string from property
|
||||||
@ -131,7 +131,7 @@ class EventConverter(Event):
|
|||||||
prop: EventDataKey,
|
prop: EventDataKey,
|
||||||
func: Callable[[str], str],
|
func: Callable[[str], str],
|
||||||
ics_prop: Optional[str] = None,
|
ics_prop: Optional[str] = None,
|
||||||
):
|
) -> None:
|
||||||
"""get property from ical event if existed, and put to gcal event
|
"""get property from ical event if existed, and put to gcal event
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -179,13 +179,13 @@ class CalendarConverter:
|
|||||||
def __init__(self, calendar: Optional[Calendar] = None):
|
def __init__(self, calendar: Optional[Calendar] = None):
|
||||||
self.calendar: Optional[Calendar] = calendar
|
self.calendar: Optional[Calendar] = calendar
|
||||||
|
|
||||||
def load(self, filename: str):
|
def load(self, filename: str) -> None:
|
||||||
"""load calendar from ics file"""
|
"""load calendar from ics file"""
|
||||||
with open(filename, "r", encoding="utf-8") as f:
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
self.calendar = Calendar.from_ical(f.read())
|
self.calendar = Calendar.from_ical(f.read())
|
||||||
self.logger.info("%s loaded", filename)
|
self.logger.info("%s loaded", filename)
|
||||||
|
|
||||||
def loads(self, string: str):
|
def loads(self, string: str) -> None:
|
||||||
"""load calendar from ics string"""
|
"""load calendar from ics string"""
|
||||||
self.calendar = Calendar.from_ical(string)
|
self.calendar = Calendar.from_ical(string)
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ from . import GoogleCalendar, GoogleCalendarService
|
|||||||
|
|
||||||
|
|
||||||
def load_config(filename: str) -> Optional[Dict[str, Any]]:
|
def load_config(filename: str) -> Optional[Dict[str, Any]]:
|
||||||
result = None
|
result: Optional[Dict[str, Any]] = None
|
||||||
try:
|
try:
|
||||||
with open(filename, "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)
|
||||||
@ -21,7 +21,7 @@ def load_config(filename: str) -> Optional[Dict[str, Any]]:
|
|||||||
class PropertyCommands:
|
class PropertyCommands:
|
||||||
"""get/set google calendar properties"""
|
"""get/set google calendar properties"""
|
||||||
|
|
||||||
def __init__(self, _service):
|
def __init__(self, _service: Any) -> None:
|
||||||
self._service = _service
|
self._service = _service
|
||||||
|
|
||||||
def get(self, calendar_id: str, property_name: str) -> None:
|
def get(self, calendar_id: str, property_name: str) -> None:
|
||||||
@ -146,7 +146,7 @@ class Commands:
|
|||||||
print("{}: {}".format(summary, calendar_id))
|
print("{}: {}".format(summary, calendar_id))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
fire.Fire(Commands, name="manage-ics2gcal")
|
fire.Fire(Commands, name="manage-ics2gcal")
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ class CalendarSync:
|
|||||||
|
|
||||||
return ComparedEvents(items_to_insert, items_to_update, items_to_delete)
|
return ComparedEvents(items_to_insert, items_to_update, items_to_delete)
|
||||||
|
|
||||||
def _filter_events_to_update(self):
|
def _filter_events_to_update(self) -> None:
|
||||||
"""filter 'to_update' events by 'updated' datetime"""
|
"""filter 'to_update' events by 'updated' datetime"""
|
||||||
|
|
||||||
def filter_updated(event_tuple: EventTuple) -> bool:
|
def filter_updated(event_tuple: EventTuple) -> bool:
|
||||||
|
@ -14,7 +14,7 @@ ConfigDate = Union[str, datetime.datetime]
|
|||||||
def load_config() -> Dict[str, Any]:
|
def load_config() -> Dict[str, Any]:
|
||||||
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 # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def get_start_date(date: ConfigDate) -> datetime.datetime:
|
def get_start_date(date: ConfigDate) -> datetime.datetime:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user