:param wrappers: { item_type: wrapper_type }
(items, wrappers=_default_join_wrappers)
| 182 | |
| 183 | |
| 184 | def join_collection(items, wrappers=_default_join_wrappers): |
| 185 | ''' |
| 186 | :param wrappers: { |
| 187 | item_type: wrapper_type |
| 188 | } |
| 189 | ''' |
| 190 | |
| 191 | items1, items2 = tee((_Component.parse(x) |
| 192 | for x in items), 2) |
| 193 | item_type, wrapper_type = _get_item_type(items1, wrappers) |
| 194 | wrapper_props = [] |
| 195 | |
| 196 | def _get_item_components(x): |
| 197 | if x.name == wrapper_type: |
| 198 | wrapper_props.extend(x.props) |
| 199 | return x.subcomponents |
| 200 | else: |
| 201 | return [x] |
| 202 | |
| 203 | components = chain(*(_get_item_components(x) for x in items2)) |
| 204 | lines = chain(*uniq(tuple(x.dump_lines()) for x in components)) |
| 205 | |
| 206 | if wrapper_type is not None: |
| 207 | lines = chain(*( |
| 208 | ['BEGIN:{}'.format(wrapper_type)], |
| 209 | # XXX: wrapper_props is a list of lines (with line-wrapping), so |
| 210 | # filtering out duplicate lines will almost certainly break |
| 211 | # multiline-values. Since the only props we usually need to |
| 212 | # support are PRODID and VERSION, I don't care. |
| 213 | uniq(wrapper_props), |
| 214 | lines, |
| 215 | ['END:{}'.format(wrapper_type)] |
| 216 | )) |
| 217 | return ''.join(line + '\r\n' for line in lines) |
| 218 | |
| 219 | |
| 220 | def _get_item_type(components, wrappers): |
no test coverage detected