def parts(self): """Split the content line up into (name, parameters, values) parts. """ try: st = escape_string(self) name_split = None value_split = None in_quotes = False for i, ch in enumerate(st): if not in_quotes: if ch in ':;' and not name_split: name_split = i if ch == ':' and not value_split: value_split = i if ch == '"': in_quotes = not in_quotes name = unsescape_string(st[:name_split]) if not name: raise ValueError('Key name is required') validate_token(name) if not name_split or name_split + 1 == value_split: raise ValueError('Invalid content line') params = Parameters.from_ical(st[name_split + 1: value_split], strict=self.strict) params = Parameters( (unsescape_string(key), unsescape_string(value)) for key, value in compat.iteritems(params) ) values = unsescape_string(st[value_split + 1:]) return (name, params, values) except ValueError as exc: raise ValueError( u"Content line could not be parsed into parts: %r: %s" % (self, exc) )
def update(self, *args, **kwargs): # Multiple keys where key1.upper() == key2.upper() will be lost. mappings = list(args) + [kwargs] for mapping in mappings: if hasattr(mapping, 'items'): mapping = iteritems(mapping) for key, value in mapping: self[key] = value
def data_encode(data, encoding=DEFAULT_ENCODING): """Encode all datastructures to the given encoding. Currently unicode strings, dicts and lists are supported. """ # http://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str if isinstance(data, compat.unicode_type): return data.encode(encoding) elif isinstance(data, dict): return dict(map(data_encode, compat.iteritems(data))) elif isinstance(data, list) or isinstance(data, tuple): return list(map(data_encode, data)) else: return data