Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try:
recur = cls()
for pairs in ical.split(';'):
try:
key, vals = pairs.split('=')
except ValueError:
# E.g. incorrect trailing semicolon, like (issue #157):
# FREQ=YEARLY;BYMONTH=11;BYDAY=1SU;
continue
recur[key] = cls.parse_type(key, vals)
return dict(recur)
except:
raise ValueError('Error in recurrence rule: %s' % ical)
class vText(compat.unicode_type):
"""Simple text.
"""
def __new__(cls, value, encoding=DEFAULT_ENCODING):
value = to_unicode(value, encoding=encoding)
self = super(vText, cls).__new__(cls, value)
self.encoding = encoding
self.params = Parameters()
return self
def __repr__(self):
return "vText('%s')" % self.to_ical()
def to_ical(self):
return escape_char(self).encode(self.encoding)
stack = [] # a stack of components
comps = []
for line in Contentlines.from_ical(st): # raw parsing
if not line:
continue
try:
name, params, vals = line.parts()
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.errors.append((None, unicode_type(e)))
continue
uname = name.upper()
# check for start of component
if uname == 'BEGIN':
# try and create one of the components defined in the spec,
# otherwise get a general Components for robustness.
c_name = vals.upper()
c_class = component_factory.get(c_name, Component)
# If component factory cannot resolve ``c_name``, the generic
# ``Component`` class is used which does not have the name set.
# That's opposed to the usage of ``cls``, which represents a
# more concrete subclass with a name set (e.g. VCALENDAR).
component = c_class()
if not getattr(component, 'name', ''): # undefined components
component.name = c_name
def unescape_string(val):
return val.replace('%2C', ',').replace('%3A', ':')\
.replace('%3B', ';').replace('%5C', '\\')
def unescape_list_or_string(val):
if isinstance(val, list):
return [unescape_string(s) for s in val]
else:
return unescape_string(val)
#########################################
# parsing and generation of content lines
class Contentline(compat.unicode_type):
"""A content line is basically a string that can be folded and parsed into
parts.
"""
def __new__(cls, value, strict=False, encoding=DEFAULT_ENCODING):
value = to_unicode(value, encoding=encoding)
assert '\n' not in value, ('Content line can not contain unescaped '
'new line characters.')
self = super(Contentline, cls).__new__(cls, value)
self.strict = strict
return self
@classmethod
def from_parts(cls, name, params, values, sorted=True):
"""Turn a parts into a content line.
"""
assert isinstance(params, Parameters)
def to_ical(self, sorted=True):
result = []
items = list(self.items())
if sorted:
items.sort()
for key, value in items:
value = param_value(value)
if isinstance(value, compat.unicode_type):
value = value.encode(DEFAULT_ENCODING)
# CaselessDict keys are always unicode
key = key.upper().encode(DEFAULT_ENCODING)
result.append(key + b'=' + value)
return b';'.join(result)
return self
def to_ical(self):
if self:
return b'TRUE'
return b'FALSE'
@classmethod
def from_ical(cls, ical):
try:
return cls.BOOL_MAP[ical]
except:
raise ValueError("Expected 'TRUE' or 'FALSE'. Got %s" % ical)
class vCalAddress(compat.unicode_type):
"""This just returns an unquoted string.
"""
def __new__(cls, value, encoding=DEFAULT_ENCODING):
value = to_unicode(value, encoding=encoding)
self = super(vCalAddress, cls).__new__(cls, value)
self.params = Parameters()
return self
def __repr__(self):
return "vCalAddress('%s')" % self.to_ical()
def to_ical(self):
return self.encode(DEFAULT_ENCODING)
@classmethod
def from_ical(cls, ical):
factory = types_factory.for_property(name)
component = stack[-1] if stack else None
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:
if name in datetime_names and 'TZID' in params:
vals = factory(factory.from_ical(vals, params['TZID']))
else:
vals = factory(factory.from_ical(vals))
except ValueError as e:
if not component.ignore_exceptions:
raise
component.errors.append((uname, unicode_type(e)))
component.add(name, None, encode=0)
else:
vals.params = params
component.add(name, vals, encode=0)
if multiple:
return comps
if len(comps) > 1:
raise ValueError('Found multiple components where '
'only one is allowed: {st!r}'.format(**locals()))
if len(comps) < 1:
raise ValueError('Found no components where '
'exactly one is required: '
'{st!r}'.format(**locals()))
return comps[0]
def foldline(line, limit=75, fold_sep='\r\n '):
"""Make a string folded as defined in RFC5545
Lines of text SHOULD NOT be longer than 75 octets, excluding the line
break. Long content lines SHOULD be split into a multiple line
representations using a line "folding" technique. That is, a long
line can be split between any two characters by inserting a CRLF
immediately followed by a single linear white-space character (i.e.,
SPACE or HTAB).
"""
assert isinstance(line, compat.unicode_type)
assert '\n' not in line
# Use a fast and simple variant for the common case that line is all ASCII.
try:
line.encode('ascii')
except (UnicodeEncodeError, UnicodeDecodeError):
pass
else:
return fold_sep.join(
line[i:i + limit - 1] for i in range(0, len(line), limit - 1)
)
ret_chars = []
byte_count = 0
for char in line:
char_byte_len = len(char.encode(DEFAULT_ENCODING))
def to_unicode(value, encoding='utf-8'):
"""Converts a value to unicode, even if it is already a unicode string.
"""
if isinstance(value, compat.unicode_type):
return value
elif isinstance(value, compat.bytes_type):
try:
value = value.decode(encoding)
except UnicodeDecodeError:
value = value.decode('utf-8', 'replace')
return value
def to_ical(self):
return compat.unicode_type(self).encode('utf-8')
def to_ical(self):
return compat.unicode_type(self).encode('utf-8')