Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_component_contains():
item = vobject._Component.parse([
'BEGIN:FOO',
'FOO:YES',
'END:FOO'
])
assert 'FOO' in item
assert 'BAZ' not in item
with pytest.raises(ValueError):
42 in item
def test_input_types():
lines = ['BEGIN:FOO', 'FOO:BAR', 'END:FOO']
for x in (lines, '\r\n'.join(lines), '\r\n'.join(lines).encode('ascii')):
c = vobject._Component.parse(x)
assert c.name == 'FOO'
assert c.props == ['FOO:BAR']
assert not c.subcomponents
def parse(self, unparsed):
return vobject._Component.parse(unparsed)
def test_multiple_items():
with pytest.raises(ValueError) as excinfo:
vobject._Component.parse([
'BEGIN:FOO',
'END:FOO',
'BEGIN:FOO',
'END:FOO',
])
assert 'Found 2 components, expected one' in str(excinfo.value)
c1, c2 = vobject._Component.parse([
'BEGIN:FOO',
'END:FOO',
'BEGIN:FOO',
'END:FOO',
], multiple=True)
assert c1.name == c2.name == 'FOO'
def with_uid(self, new_uid):
parsed = _Component.parse(self.raw)
stack = [parsed]
while stack:
component = stack.pop()
stack.extend(component.subcomponents)
if component.name in ('VEVENT', 'VTODO', 'VJOURNAL', 'VCARD'):
del component['UID']
if new_uid:
component['UID'] = new_uid
return Item('\r\n'.join(parsed.dump_lines()))
def split_collection(text):
assert isinstance(text, str)
inline = []
items = {} # uid => item
ungrouped_items = []
for main in _Component.parse(text, multiple=True):
_split_collection_impl(main, main, inline, items, ungrouped_items)
for item in chain(items.values(), ungrouped_items):
item.subcomponents.extend(inline)
yield '\r\n'.join(item.dump_lines())
def parsed(self):
'''Don't cache because the rv is mutable.'''
try:
return _Component.parse(self.raw)
except Exception:
return None
def join_collection(items, wrappers=_default_join_wrappers):
'''
:param wrappers: {
item_type: wrapper_type
}
'''
items1, items2 = tee((_Component.parse(x)
for x in items), 2)
item_type, wrapper_type = _get_item_type(items1, wrappers)
wrapper_props = []
def _get_item_components(x):
if x.name == wrapper_type:
wrapper_props.extend(x.props)
return x.subcomponents
else:
return [x]
components = chain(*(_get_item_components(x) for x in items2))
lines = chain(*uniq(tuple(x.dump_lines()) for x in components))
if wrapper_type is not None:
lines = chain(*(