How to use the vcard.vcard_errors.VCardLineError function in vcard

To help you get started, we’ve selected a few vcard examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github l0b0 / vcard / vcard / vcard_validator.py View on Github external
    @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)

    return property_lines
github l0b0 / vcard / vcard / vcard_validator.py View on Github external
group_re = re.compile('^([{0}]*)\.'.format(re.escape(ID_CHARACTERS)))

    group_match = group_re.match(lines[0])
    if group_match is not None:
        group = group_match.group(1)

        # Validate
        if len(group) == 0:
            raise VCardLineError(NOTE_DOT_AT_LINE_START, {})

        for index in range(len(lines)):
            line = lines[index]
            next_match = group_re.match(line)
            if not next_match:
                raise VCardLineError(NOTE_MISSING_GROUP, {'File line': index + 1})
            if next_match.group(1) != group:
                raise VCardNameError(
                    '{0}: {1} != {2}'.format(NOTE_MISMATCH_GROUP, next_match.group(1), group), {'File line': index + 1})
    else:
        # Make sure there are no groups elsewhere
        for index in range(len(lines)):
            next_match = group_re.match(lines[index])
            if next_match:
                raise VCardNameError(
                    '{0}: {1} != {2}'.format(NOTE_MISMATCH_GROUP, next_match.group(1), group), {'File line': index + 1})

    return group
github l0b0 / vcard / vcard / vcard_validator.py View on Github external
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)
github l0b0 / vcard / vcard / vcard_validator.py View on Github external
Get & validate group. RFC 2426 pages 28, 29.

    @param lines: List of unfolded vCard lines
    @return: Group name if one exists, None otherwise
    """
    group = None

    group_re = re.compile('^([{0}]*)\.'.format(re.escape(ID_CHARACTERS)))

    group_match = group_re.match(lines[0])
    if group_match is not None:
        group = group_match.group(1)

        # Validate
        if len(group) == 0:
            raise VCardLineError(NOTE_DOT_AT_LINE_START, {})

        for index in range(len(lines)):
            line = lines[index]
            next_match = group_re.match(line)
            if not next_match:
                raise VCardLineError(NOTE_MISSING_GROUP, {'File line': index + 1})
            if next_match.group(1) != group:
                raise VCardNameError(
                    '{0}: {1} != {2}'.format(NOTE_MISMATCH_GROUP, next_match.group(1), group), {'File line': index + 1})
    else:
        # Make sure there are no groups elsewhere
        for index in range(len(lines)):
            next_match = group_re.match(lines[index])
            if next_match:
                raise VCardNameError(
                    '{0}: {1} != {2}'.format(NOTE_MISMATCH_GROUP, next_match.group(1), group), {'File line': index + 1})