1.1 --- a/vCalendar.py Mon Sep 22 18:26:59 2014 +0200
1.2 +++ b/vCalendar.py Wed Sep 24 16:05:03 2014 +0200
1.3 @@ -276,4 +276,7 @@
1.4
1.5 return vContent.iterwrite(stream_or_string, write, encoding, line_length, vCalendarStreamWriter)
1.6
1.7 +to_dict = vContent.to_dict
1.8 +to_node = vContent.to_node
1.9 +
1.10 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/vContent.py Mon Sep 22 18:26:59 2014 +0200
2.2 +++ b/vContent.py Wed Sep 24 16:05:03 2014 +0200
2.3 @@ -620,6 +620,43 @@
2.4 else:
2.5 return codecs.open(stream_or_string, "w", encoding=(encoding or default_encoding))
2.6
2.7 +def items_to_dict(items):
2.8 +
2.9 + """
2.10 + Return the given 'items' as a dictionary mapping names to tuples of the form
2.11 + (value, attributes).
2.12 + """
2.13 +
2.14 + d = {}
2.15 + for name, attr, value in items:
2.16 + if not d.has_key(name):
2.17 + d[name] = []
2.18 + if isinstance(value, list):
2.19 + d[name].append((items_to_dict(value), attr))
2.20 + else:
2.21 + d[name].append((value, attr))
2.22 + return d
2.23 +
2.24 +def dict_to_items(d):
2.25 +
2.26 + """
2.27 + Return 'd' converted to a list of items suitable for serialisation using
2.28 + iterwrite.
2.29 + """
2.30 +
2.31 + items = []
2.32 + for name, value in d.items():
2.33 + if isinstance(value, list):
2.34 + for v, a in value:
2.35 + if isinstance(v, dict):
2.36 + items.append((name, a, dict_to_items(v)))
2.37 + else:
2.38 + items.append((name, a, v))
2.39 + else:
2.40 + v, a = value
2.41 + items.append((name, a, dict_to_items(v)))
2.42 + return items
2.43 +
2.44 # Public functions.
2.45
2.46 def parse(stream_or_string, encoding=None, non_standard_newline=0, parser_cls=None):
2.47 @@ -708,4 +745,17 @@
2.48
2.49 return (writer_cls or StreamWriter)(_writer)
2.50
2.51 +def to_dict(node):
2.52 +
2.53 + "Return the 'node' converted to a dictionary representation."
2.54 +
2.55 + name, attr, items = node
2.56 + return {name : (isinstance(items, list) and items_to_dict(items) or items, attr)}
2.57 +
2.58 +def to_node(d):
2.59 +
2.60 + "Return 'd' converted to a items-based representation."
2.61 +
2.62 + return dict_to_items(d)[0]
2.63 +
2.64 # vim: tabstop=4 expandtab shiftwidth=4