Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
thet committed Oct 15, 2021
1 parent bbab457 commit ee137f7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
31 changes: 15 additions & 16 deletions src/icalendar/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def is_broken(self):
#############################
# handling of property values

def _encode(self, name, value, parameters=None, encode=1):
def _encode(self, name, value, parameters=None, encode=True):
"""Encode values to icalendar property values.
:param name: Name of the property.
Expand Down Expand Up @@ -152,7 +152,7 @@ def _encode(self, name, value, parameters=None, encode=1):
valuetype=parameters.get('VALUE') if parameters else None,
nativetype=type(value)
)
if types_factory.is_list_property(name):
if types_factory.is_date_list_property(name):
obj = vDDDLists(value, klass)
else:
obj = klass(value)
Expand All @@ -161,7 +161,7 @@ def _encode(self, name, value, parameters=None, encode=1):
obj.params = parameters
return obj

def add(self, name, value, parameters=None, encode=1):
def add(self, name, value, parameters=None, encode=True):
"""Add a property.
:param name: Name of the property.
Expand Down Expand Up @@ -193,7 +193,7 @@ def add(self, name, value, parameters=None, encode=1):

# encode value
if encode and isinstance(value, list) \
and not types_factory.is_list_property(name)\
and not types_factory.is_date_list_property(name)\
and name.lower() not in ('categories',):
# Individually convert each value to an ical type except rdate and
# exdate, where lists of dates might be passed to vDDDLists.
Expand Down Expand Up @@ -255,20 +255,20 @@ def decoded(self, name, default=_marker):
# Inline values. A few properties have multiple values inlined in in one
# property line. These methods are used for splitting and joining these.

def get_inline(self, name, decode=1):
def get_inline(self, name, decode=True):
"""Returns a list of values (split on comma).
"""
vals = [v.strip('" ') for v in q_split(self[name])]
if decode:
return [self._decode(name, val) for val in vals]
return vals

def set_inline(self, name, values, encode=1):
def set_inline(self, name, values, encode=True):
"""Converts a list of values into comma seperated string and sets value
to that.
"""
if encode:
values = [self._encode(name, value, encode=1) for value in values]
values = [self._encode(name, value, encode=True) for value in values]
self[name] = types_factory['inline'](q_join(values))

#########################
Expand Down Expand Up @@ -383,31 +383,30 @@ def from_ical(cls, st, multiple=False):
if not component:
raise ValueError('Property "{prop}" does not have '
'a parent component.'.format(prop=name))
datetime_names = ('DTSTART', 'DTEND', 'RECURRENCE-ID', 'DUE',
'FREEBUSY', 'RDATE', 'EXDATE')

try:
factory = types_factory.for_property(name,
params.get('VALUE'))
valuetype=params.get('VALUE'))
except ValueError as e:
if not component.ignore_exceptions:
raise
else:
# add error message and fall back to vText value type
component.errors.append((uname, str(e)))
factory = types_factory['text']

try:
if (types_factory.is_list_property(name) and
if (types_factory.is_date_list_property(name) and
factory != vText):
# TODO: list type currenty supports only datetime types
vals = vDDDLists(
vDDDLists.from_ical(vals, params.get('TZID'),
factory))
elif uname in types_factory.datetime_names and 'TZID' in params:
vals = factory(factory.from_ical(vals, params['TZID']))
else:
if name in datetime_names and 'TZID' in params:
vals = factory(
factory.from_ical(vals, params['TZID']))
else:
vals = factory(factory.from_ical(vals))
vals = factory(factory.from_ical(vals))

except ValueError as e:
if not component.ignore_exceptions:
raise
Expand Down
2 changes: 1 addition & 1 deletion src/icalendar/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def params(self):
# TODO?
# Later, when I get more time... need to finish this off now. The last major
# thing missing.
# def _encode(self, name, value, cond=1):
# def _encode(self, name, value, cond=True):
# # internal, for conditional convertion of values.
# if cond:
# klass = types_factory.for_property(name)
Expand Down
30 changes: 22 additions & 8 deletions src/icalendar/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def __init__(self, td):
if type(td) is not timedelta:
raise ValueError('Value MUST be a timedelta instance')
self.td = td
self.params = Parameters()
self.params = Parameters({'value': 'DURATION'})

@property
def dt(self):
Expand Down Expand Up @@ -559,7 +559,7 @@ def __init__(self, per):
if start > end:
raise ValueError("Start time is greater than end time")

self.params = Parameters()
self.params = Parameters({'value': 'PERIOD'})
# set the timezone identifier
# does not support different timezones for start and end
tzid = tzid_from_dt(start)
Expand Down Expand Up @@ -1079,10 +1079,25 @@ def __init__(self, *args, **kwargs):
date: 'date',
}

list_properties = ('exdate', 'rdate')

def is_list_property(self, name):
if name.lower() in self.list_properties:
datetime_names = (
'COMPLETED',
'CREATED',
'DTEND',
'DTSTAMP',
'DTSTART',
'DUE',
'DURATION',
'EXDATE'
'FREEBUSY',
'LAST-MODIFIED',
'RDATE',
'RECURRENCE-ID',
'TRIGGER',
)
date_list_properties = ('EXDATE', 'RDATE')

def is_date_list_property(self, name):
if name.upper() in self.date_list_properties:
return True
return False

Expand Down Expand Up @@ -1142,8 +1157,7 @@ def from_ical(self, name, value, valuetype=None):
encoded string to a primitive python type.
"""
type_class = self.for_property(name, valuetype)

if name.lower() in self.list_properties:
if name.upper() in self.date_list_properties:
# this property is of list type
decoded = vDDDLists.from_ical(value, unit_type=type_class)
else:
Expand Down

0 comments on commit ee137f7

Please sign in to comment.