Skip to content

Commit

Permalink
Fixes bug when date-time is recognized as time
Browse files Browse the repository at this point in the history
Date-time was recognized incorrectly as a date or time. This resulted
in wrong representation of some iCalendar strings.

Also adds "errors" list in Component for saving error strings from parsing.

collective#174
collective#168
  • Loading branch information
stlaz committed Dec 18, 2015
1 parent ff1f2ee commit 0555aa8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
16 changes: 8 additions & 8 deletions src/icalendar/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def __init__(self, *args, **kwargs):
super(Component, self).__init__(*args, **kwargs)
# set parameters here for properties that use non-default values
self.subcomponents = [] # Components can be nested.
self.is_broken = False # True if we ignored an exception while
# parsing a property
self.errors = list() # If we ignored exception(s) while
# parsing a property, contains error strings

# def is_compliant(self, name):
# """Returns True is the given property name is compliant with the
Expand Down Expand Up @@ -309,14 +309,14 @@ def from_ical(cls, st, multiple=False):

try:
name, params, vals = line.parts()
except ValueError:
except ValueError as e:
# if unable to parse a line within a component
# that ignores exceptions, mark the component
# as broken and skip the line. otherwise raise.
component = stack[-1] if stack else None
if not component or not component.ignore_exceptions:
raise
component.is_broken = True
component.errors.append((None, str(e)))
continue

uname = name.upper()
Expand All @@ -338,8 +338,7 @@ def from_ical(cls, st, multiple=False):
if not stack: # we are at the end
comps.append(component)
else:
if not component.is_broken:
stack[-1].add_component(component)
stack[-1].add_component(component)
if vals == 'VTIMEZONE' and \
'TZID' in component and \
component['TZID'] not in pytz.all_timezones and \
Expand All @@ -356,10 +355,11 @@ def from_ical(cls, st, multiple=False):
vals = factory(factory.from_ical(vals, params['TZID']))
else:
vals = factory(factory.from_ical(vals))
except ValueError:
except ValueError as e:
if not component.ignore_exceptions:
raise
component.is_broken = True
component.errors.append((uname, str(e)))
component.add(name, None, encode=0)
else:
vals.params = params
component.add(name, vals, encode=0)
Expand Down
13 changes: 7 additions & 6 deletions src/icalendar/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,14 @@ def from_ical(cls, ical, timezone=None):
u = ical.upper()
if u.startswith(('P', '-P', '+P')):
return vDuration.from_ical(ical)
try:
if len(ical) >= 15:
return vDatetime.from_ical(ical, timezone=timezone)
except ValueError:
try:
return vDate.from_ical(ical)
except ValueError:
return vTime.from_ical(ical)
elif len(ical) == 8:
return vDate.from_ical(ical)
elif len(ical) < 8:
return vTime.from_ical(ical)
else:
raise ValueError("Wrong datetime format: %s" % ical)


class vDate(object):
Expand Down

0 comments on commit 0555aa8

Please sign in to comment.