How to use the unidiff.constants.LINE_TYPE_CONTEXT function in unidiff

To help you get started, we’ve selected a few unidiff 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 matiasb / python-unidiff / unidiff / patch.py View on Github external
expected_target_end = target_line_no + hunk.target_length

        for diff_line_no, line in diff:
            if encoding is not None:
                line = line.decode(encoding)

            valid_line = RE_HUNK_BODY_LINE.match(line)
            if not valid_line:
                valid_line = RE_HUNK_EMPTY_BODY_LINE.match(line)

            if not valid_line:
                raise UnidiffParseError('Hunk diff line expected: %s' % line)

            line_type = valid_line.group('line_type')
            if line_type == LINE_TYPE_EMPTY:
                line_type = LINE_TYPE_CONTEXT
            value = valid_line.group('value')
            original_line = Line(value, line_type=line_type)
            if line_type == LINE_TYPE_ADDED:
                original_line.target_line_no = target_line_no
                target_line_no += 1
            elif line_type == LINE_TYPE_REMOVED:
                original_line.source_line_no = source_line_no
                source_line_no += 1
            elif line_type == LINE_TYPE_CONTEXT:
                original_line.target_line_no = target_line_no
                target_line_no += 1
                original_line.source_line_no = source_line_no
                source_line_no += 1
            elif line_type == LINE_TYPE_NO_NEWLINE:
                pass
            else:
github matiasb / python-unidiff / unidiff / patch.py View on Github external
if not valid_line:
                raise UnidiffParseError('Hunk diff line expected: %s' % line)

            line_type = valid_line.group('line_type')
            if line_type == LINE_TYPE_EMPTY:
                line_type = LINE_TYPE_CONTEXT
            value = valid_line.group('value')
            original_line = Line(value, line_type=line_type)
            if line_type == LINE_TYPE_ADDED:
                original_line.target_line_no = target_line_no
                target_line_no += 1
            elif line_type == LINE_TYPE_REMOVED:
                original_line.source_line_no = source_line_no
                source_line_no += 1
            elif line_type == LINE_TYPE_CONTEXT:
                original_line.target_line_no = target_line_no
                target_line_no += 1
                original_line.source_line_no = source_line_no
                source_line_no += 1
            elif line_type == LINE_TYPE_NO_NEWLINE:
                pass
            else:
                original_line = None

            # stop parsing if we got past expected number of lines
            if (source_line_no > expected_source_end or
                    target_line_no > expected_target_end):
                raise UnidiffParseError('Hunk is longer than expected')

            if original_line:
                original_line.diff_line_no = diff_line_no
github matiasb / python-unidiff / unidiff / parser.py View on Github external
for line in diff:
        valid_line = RE_HUNK_BODY_LINE.match(line)
        if not valid_line:
            raise UnidiffParseError('Hunk diff line expected: %s' % line)

        line_type = valid_line.group('line_type')
        value = valid_line.group('value')
        original_line = Line(value, line_type=line_type)
        if line_type == LINE_TYPE_ADDED:
            original_line.target_line_no = target_line_no
            target_line_no += 1
        elif line_type == LINE_TYPE_REMOVED:
            original_line.source_line_no = source_line_no
            source_line_no += 1
        elif line_type == LINE_TYPE_CONTEXT:
            original_line.target_line_no = target_line_no
            target_line_no += 1
            original_line.source_line_no = source_line_no
            source_line_no += 1
        else:
            original_line = None

        if original_line:
            hunk.append(original_line)

        # check hunk len(old_lines) and len(new_lines) are ok
        if hunk.is_valid():
            break

    return hunk
github matiasb / python-unidiff / unidiff / patch.py View on Github external
def is_context(self):
        return self.line_type == LINE_TYPE_CONTEXT