From 6d92f64d22961f681d856707c92e3166c911e3ec Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 30 Jun 2013 17:00:26 -0400 Subject: [PATCH] entry: Add parameter parsing to lines Split the initial key/params/value parsing into its own function to keep Entry._parse from growing unwieldy. --- pycalendar/entry.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pycalendar/entry.py b/pycalendar/entry.py index c0de990..2ed2fdc 100644 --- a/pycalendar/entry.py +++ b/pycalendar/entry.py @@ -80,9 +80,8 @@ class Entry (dict): 'VEVENT' >>> event.content # doctest: +ELLIPSIS 'BEGIN:VEVENT\r\nUID:...\r\nEND:VEVENT\r\n' - >>> sorted(event.keys()) # doctest: +NORMALIZE_WHITESPACE - ['DTEND;VALUE=DATE', 'DTSTAMP', 'DTSTART;VALUE=DATE', 'GEO', - 'LOCATION', 'SUMMARY', 'UID', 'URL'] + >>> sorted(event.keys()) + ['DTEND', 'DTSTAMP', 'DTSTART', 'GEO', 'LOCATION', 'SUMMARY', 'UID', 'URL'] >>> event['LOCATION'] 'Snow Hill\\, Dover\\, Massachusetts' @@ -137,7 +136,7 @@ class Entry (dict): stack = [] child_lines = [] for i,line in enumerate(self._lines[1:-1]): - key,value = [x.strip() for x in line.split(':', 1)] + key,parameters,value = self._parse_key_value(line) if key == 'BEGIN': _LOG.debug('{!r}: begin {}'.format(self, value)) stack.append(value) @@ -167,6 +166,20 @@ class Entry (dict): 'cannot parse VERSION {} feed'.format(v)) self._add_value(key=key, value=value) + def _parse_key_value(self, line): + key,value = [x.strip() for x in line.split(':', 1)] + parameters = key.split(';') + key = parameters.pop(0) + parameters = {tuple(x.split('=', 1)) for x in parameters} + for k,v in parameters: + if ',' in v: + parameters = v.split(',') + if parameters and key in ['BEGIN', 'END']: + raise ValueError( + 'parameters are not allowed with {}: {}'.format( + key, line)) + return (key, parameters, value) + def _add_value(self, key, value, force_list=False): if force_list and key not in self: self[key] = [] -- 2.26.2