Bugfixes + speed improvements
Fixed issue with duplicate events Switched from list comprehensions to generator expressions for better performance Problems: Date sorting not working as expected
This commit is contained in:
parent
c0e2431e76
commit
85dc6871a0
@ -86,9 +86,9 @@ class iCalendar:
|
|||||||
returns a list of iCalendars as string (raw)
|
returns a list of iCalendars as string (raw)
|
||||||
"""
|
"""
|
||||||
if type(url) == list:
|
if type(url) == list:
|
||||||
ical = [Calendar.from_ical(open(path)) for path in filepath]
|
ical = (Calendar.from_ical(open(path)) for path in filepath)
|
||||||
elif type(url) == str:
|
elif type(url) == str:
|
||||||
ical = [Calendar.from_ical(open(path))]
|
ical = (Calendar.from_ical(open(path)))
|
||||||
else:
|
else:
|
||||||
raise Exception ("Input: '{}' is not a string or list!".format(url))
|
raise Exception ("Input: '{}' is not a string or list!".format(url))
|
||||||
|
|
||||||
@ -111,33 +111,22 @@ class iCalendar:
|
|||||||
raise Exception('Please input a valid arrow (time) object!')
|
raise Exception('Please input a valid arrow (time) object!')
|
||||||
|
|
||||||
# parse non-recurring events
|
# parse non-recurring events
|
||||||
events = [
|
|
||||||
{
|
|
||||||
'title':events.get('summary').lstrip(),
|
|
||||||
'begin':arrow.get(events.get('dtstart').dt).to(timezone
|
|
||||||
if arrow.get(events.get('dtstart').dt).format('HH:mm') != '00:00' else 'UTC'),
|
|
||||||
'end':arrow.get(events.get('dtend').dt).to(timezone
|
|
||||||
if arrow.get(events.get('dtend').dt).format('HH:mm') != '00:00' else 'UTC')
|
|
||||||
}
|
|
||||||
for ical in self.icalendars for events in ical.walk()
|
|
||||||
if events.name == "VEVENT" and
|
|
||||||
t_start <= arrow.get(events.get('dtstart').dt) <= t_end and
|
|
||||||
t_start <= arrow.get(events.get('dtend').dt) <= t_end
|
|
||||||
]
|
|
||||||
|
|
||||||
# if any recurring events were found, add them to parsed_events
|
|
||||||
if events: self.parsed_events += events
|
|
||||||
|
|
||||||
# Recurring events time-span has to be in this format:
|
# Recurring events time-span has to be in this format:
|
||||||
# "%Y%m%dT%H%M%SZ" (python strftime)
|
# "%Y%m%dT%H%M%SZ" (python strftime)
|
||||||
fmt = lambda date: (date.year, date.month, date.day, date.hour, date.minute,
|
fmt = lambda date: (date.year, date.month, date.day, date.hour,
|
||||||
date.second)
|
date.minute, date.second)
|
||||||
|
|
||||||
# Parse recurring events
|
t_start_recurring = fmt(t_start)
|
||||||
recurring_events = [recurring_ical_events.of(ical).between(
|
t_end_recurring = fmt(t_end)
|
||||||
fmt(t_start),fmt(t_end)) for ical in self.icalendars]
|
|
||||||
|
|
||||||
re_events = [{
|
# Fetch recurring events
|
||||||
|
recurring_events = (recurring_ical_events.of(ical).between(
|
||||||
|
t_start_recurring, t_end_recurring)
|
||||||
|
for ical in self.icalendars)
|
||||||
|
|
||||||
|
re_events = (
|
||||||
|
{
|
||||||
'title':events.get('SUMMARY').lstrip(),
|
'title':events.get('SUMMARY').lstrip(),
|
||||||
'begin':arrow.get(events.get('DTSTART').dt).to(timezone
|
'begin':arrow.get(events.get('DTSTART').dt).to(timezone
|
||||||
if arrow.get(events.get('dtstart').dt).format('HH:mm') != '00:00'
|
if arrow.get(events.get('dtstart').dt).format('HH:mm') != '00:00'
|
||||||
@ -145,10 +134,11 @@ class iCalendar:
|
|||||||
'end':arrow.get(events.get("DTEND").dt).to(timezone
|
'end':arrow.get(events.get("DTEND").dt).to(timezone
|
||||||
if arrow.get(events.get('dtstart').dt).format('HH:mm') != '00:00'
|
if arrow.get(events.get('dtstart').dt).format('HH:mm') != '00:00'
|
||||||
else 'UTC')
|
else 'UTC')
|
||||||
} for ical in recurring_events for events in ical]
|
}
|
||||||
|
for ical in recurring_events for events in ical)
|
||||||
|
|
||||||
# if any recurring events were found, add them to parsed_events
|
# if any recurring events were found, add them to parsed_events
|
||||||
if re_events: self.parsed_events += re_events
|
if re_events: self.parsed_events += list(re_events)
|
||||||
|
|
||||||
# Sort events by their beginning date
|
# Sort events by their beginning date
|
||||||
self.sort()
|
self.sort()
|
||||||
@ -160,9 +150,11 @@ class iCalendar:
|
|||||||
if not self.parsed_events:
|
if not self.parsed_events:
|
||||||
logger.debug('no events found to be sorted')
|
logger.debug('no events found to be sorted')
|
||||||
else:
|
else:
|
||||||
|
# Not working as expected....
|
||||||
by_date = lambda event: event['begin']
|
by_date = lambda event: event['begin']
|
||||||
self.parsed_events.sort(key=by_date)
|
self.parsed_events.sort(key=by_date)
|
||||||
|
|
||||||
|
|
||||||
def clear_events(self):
|
def clear_events(self):
|
||||||
"""clear previously parsed events"""
|
"""clear previously parsed events"""
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user