Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Turn a parts into a content line.
"""
assert isinstance(params, Parameters)
if hasattr(values, 'to_ical'):
values = values.to_ical()
else:
values = vText(values).to_ical()
# elif isinstance(values, basestring):
# values = escape_char(values)
# TODO: after unicode only, remove this
# Convert back to unicode, after to_ical encoded it.
name = to_unicode(name)
values = to_unicode(values)
if params:
params = to_unicode(params.to_ical(sorted=sorted))
return cls('%s;%s:%s' % (name, params, values))
return cls('%s:%s' % (name, values))
def pop(self, key, default=None):
key = to_unicode(key)
return super(CaselessDict, self).pop(key.upper(), default)
def has_key(self, key):
key = to_unicode(key)
return super(CaselessDict, self).__contains__(key.upper())
def get(self, key, default=None):
key = to_unicode(key)
return super(CaselessDict, self).get(key.upper(), default)
@classmethod
def from_parts(cls, name, params, values, sorted=True):
"""Turn a parts into a content line.
"""
assert isinstance(params, Parameters)
if hasattr(values, 'to_ical'):
values = values.to_ical()
else:
values = vText(values).to_ical()
# elif isinstance(values, basestring):
# values = escape_char(values)
# TODO: after unicode only, remove this
# Convert back to unicode, after to_ical encoded it.
name = to_unicode(name)
values = to_unicode(values)
if params:
params = to_unicode(params.to_ical(sorted=sorted))
return cls('%s;%s:%s' % (name, params, values))
return cls('%s:%s' % (name, values))
def uid(self, host_name='example.com', unique=''):
"""Generates a unique id consisting of:
datetime-uniquevalue@host.
Like:
20050105T225746Z-HKtJMqUgdO0jDUwm@example.com
"""
host_name = to_unicode(host_name)
unique = unique or self.rnd_string()
today = to_unicode(vDatetime(datetime.today()).to_ical())
return vText('%s-%s@%s' % (today,
unique,
host_name))
@classmethod
def from_parts(cls, name, params, values, sorted=True):
"""Turn a parts into a content line.
"""
assert isinstance(params, Parameters)
if hasattr(values, 'to_ical'):
values = values.to_ical()
else:
values = vText(values).to_ical()
# elif isinstance(values, basestring):
# values = escape_char(values)
# TODO: after unicode only, remove this
# Convert back to unicode, after to_ical encoded it.
name = to_unicode(name)
values = to_unicode(values)
if params:
params = to_unicode(params.to_ical(sorted=sorted))
return cls('%s;%s:%s' % (name, params, values))
return cls('%s:%s' % (name, values))
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
def __init__(self, *args, **kwargs):
"""Set keys to upper for initial dict.
"""
super(CaselessDict, self).__init__(*args, **kwargs)
for key, value in self.items():
key_upper = to_unicode(key).upper()
if key != key_upper:
super(CaselessDict, self).__delitem__(key)
self[key_upper] = value
def uid(self, host_name='example.com', unique=''):
"""Generates a unique id consisting of:
datetime-uniquevalue@host.
Like:
20050105T225746Z-HKtJMqUgdO0jDUwm@example.com
"""
host_name = to_unicode(host_name)
unique = unique or self.rnd_string()
today = to_unicode(vDatetime(datetime.today()).to_ical())
return vText('%s-%s@%s' % (today,
unique,
host_name))