Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def unfold_vcard_lines(lines):
"""
Un-split lines in vCard, warning about short lines. RFC 2426 page 8.
@param lines: List of potentially folded vCard lines
@return: List of lines, one per property
"""
property_lines = []
for index in range(len(lines)):
line = lines[index]
if not line.endswith(NEWLINE_CHARACTERS):
raise VCardLineError(NOTE_INVALID_LINE_SEPARATOR, {'File line': index + 1})
if len(line) > VCARD_LINE_MAX_LENGTH_RAW:
warnings.warn('Long line in vCard: {0}'.format(line.encode('utf-8')))
if line.startswith(' '):
if index == 0:
raise VCardLineError(NOTE_CONTINUATION_AT_START, {'File line': index + 1})
elif len(lines[index - 1]) < VCARD_LINE_MAX_LENGTH_RAW:
warnings.warn('Short folded line at line {0:d}'.format(index - 1))
elif line == SPACE_CHARACTER + NEWLINE_CHARACTERS:
warnings.warn('Empty folded line at line {0:d}'.format(index))
property_lines[-1] = \
property_lines[-1][:-len(NEWLINE_CHARACTERS)] + line[1:]
else:
property_lines.append(line)
"""
if filename == '-':
file_pointer = sys.stdin
else:
file_pointer = codecs.open(filename, 'r', 'utf-8')
contents = file_pointer.read().splitlines(True)
vcard_text = ''
result = ''
try:
for index in range(len(contents)):
line = contents[index]
vcard_text += line
if line == NEWLINE_CHARACTERS:
try:
vcard = VCard(vcard_text, filename)
vcard_text = ''
if verbose:
print(vcard)
except VCardError as error:
error.context['File'] = filename
error.context['File line'] = index
err_type = type(error)
raise err_type(error.message, error.context)
except VCardError as error:
result += str(error)
if vcard_text != '' and result == '':
result += 'Could not process entire %s - %i lines remain' % (filename, len(vcard_text.splitlines(False)))
def get_vcard_property_values(values_string):
"""
Get the property values.
@param values_string: Multiple value string
@return: List of values (RFC 2426 page 12)
"""
values = []
# Strip line ending
values_string = values_string[:-len(NEWLINE_CHARACTERS)]
sub_value_strings = vcard_utils.split_unescaped(values_string, ';')
for sub in sub_value_strings:
values.append(get_vcard_property_sub_values(sub))
return values