reviewboard.diffviewer.chunk_generator¶
-
class
NoWrapperHtmlFormatter
(*args, **kwargs)[source]¶ Bases:
pygments.formatters.html.HtmlFormatter
An HTML Formatter for Pygments that doesn’t wrap items in a div.
-
class
RawDiffChunkGenerator
(old, new, orig_filename, modified_filename, enable_syntax_highlighting=True, encoding_list=None, diff_compat=2)[source]¶ Bases:
object
A generator for chunks for a diff that can be used for rendering.
Each chunk represents an insert, delete, replace, or equal section. It contains all the data needed to render the portion of the diff.
This is general-purpose and meant to operate on strings each consisting of at least one line of text, or lists of lines of text.
If the caller passes lists of lines instead of strings, then the caller will also be responsible for applying any syntax highlighting and dealing with newline differences.
-
__init__
(old, new, orig_filename, modified_filename, enable_syntax_highlighting=True, encoding_list=None, diff_compat=2)[source]¶ Initialize the chunk generator.
Parameters: - old (bytes or list of bytes) – The old data being modified.
- new (bytes or list of bytes) – The new data.
- orig_filename (unicode) – The filename corresponding to the old data.
- modified_filename (unicode) – The filename corresponding to the new data.
- enable_syntax_highlighting (bool, optional) – Whether to syntax-highlight the lines.
- encoding_list (list of unicode, optional) – A list of encodings to try for the
old
andnew
data, when converting to Unicode. If not specified, this defaults toiso-8859-15
. - diff_compat (int, optional) – A specific diff compatibility version to use for any diffing logic.
-
get_chunks
(cache_key=None)[source]¶ Return the chunks for the given diff information.
If a cache key is provided and there are chunks already computed in the cache, they will be yielded. Otherwise, new chunks will be generated, stored in cache (given a cache key), and yielded.
-
generate_chunks
(old, new, old_encoding_list=None, new_encoding_list=None)[source]¶ Generate chunks for the difference between two strings.
The strings will be normalized, ensuring they’re of the proper encoding and ensuring they have consistent newlines. They’re then syntax-highlighted (if requested).
Once the strings are ready, chunks are built from the strings and yielded to the caller. Each chunk represents information on an equal, inserted, deleted, or replaced set of lines.
The number of lines of each chunk type are stored in the
counts
dictionary, which can then be accessed after yielding all chunks.Parameters: - old (bytes or list of bytes) – The old data being modified.
- new (bytes or list of bytes) – The new data.
- old_encoding_list (list of unicode, optional) – An optional list of encodings that
old
may be encoded in. If not provided,encoding_list
is used. - new_encoding_list (list of unicode, optional) – An optional list of encodings that
new
may be encoded in. If not provided,encoding_list
is used.
Yields: dict – A rendered chunk containing the following keys:
index
(int)The 0-based index of the chunk.
lines
(list of unicode):The rendered list of lines.
numlines
(int):The number of lines in the chunk.
change
(unicode):The type of change (
delete
,equal
,insert
orreplace
).collapsable
(bool):Whether the chunk can be collapsed.
meta
(dict):Metadata on the chunk.
-
normalize_source_string
(s, encoding_list, **kwargs)[source]¶ Normalize a source string of text to use for the diff.
This will normalize the encoding of the string and the newlines, returning a tuple containing the normalized string and a list of lines split from the source.
Both the original and modified strings used for the diff will be normalized independently.
This is only used if the caller passes a string instead of a list for the original or new values.
Subclasses can override this to provide custom behavior.
Parameters: Returns: A tuple containing:
- The full normalized string
- The list of lines from the string
Return type: Raises: UnicodeDecodeError
– The string could not be converted to Unicode.
-
normalize_source_list
(l, encoding_list, **kwargs)[source]¶ Normalize a list of source lines to use for the diff.
This will normalize the encoding of the lines.
Both the original and modified lists of lines used for the diff will be normalized independently.
This is only used if the caller passes a list instead of a string for the original or new values.
Subclasses can override this to provide custom behavior.
Parameters: - l (list of bytes) – The list of lines to normalize.
- encoding_list (list of unicode) – The list of encodings to try when converting the lines to Unicode.
- **kwargs (dict) – Additional keyword arguments, for future expansion.
Returns: The resulting list of normalized lines.
Return type: list of unicode
Raises: UnicodeDecodeError
– One or more lines could not be converted to Unicode.
-
normalize_path_for_display
(filename)[source]¶ Normalize a file path for display to the user.
By default, this returns the filename as-is. Subclasses can override the behavior to return a variant of the filename.
Parameters: filename (unicode) – The filename to normalize. Returns: The normalized filename. Return type: unicode
-
get_line_changed_regions
(old_line_num, old_line, new_line_num, new_line)[source]¶ Return information on changes between two lines.
This returns a tuple containing a list of tuples of ranges in the old line, and a list of tuples of ranges in the new line, that should be highlighted.
This defaults to simply wrapping get_line_changed_regions() from diffutils. Subclasses can override to provide custom behavior.
-
-
class
DiffChunkGenerator
(request, filediff, interfilediff=None, force_interdiff=False, enable_syntax_highlighting=True, base_filediff=None)[source]¶ Bases:
reviewboard.diffviewer.chunk_generator.RawDiffChunkGenerator
A generator for chunks for a FileDiff that can be used for rendering.
Each chunk represents an insert, delete, replace, or equal section. It contains all the data needed to render the portion of the diff.
There are three ways this can operate, based on provided parameters.
filediff, no interfilediff - Returns chunks for a single filediff. This is the usual way people look at diffs in the diff viewer.
In this mode, we get the original file based on the filediff and then patch it to get the resulting file.
This is also used for interdiffs where the source revision has no equivalent modified file but the interdiff revision does. It’s no different than a standard diff.
filediff, interfilediff - Returns chunks showing the changes between a source filediff and the interdiff.
This is the typical mode used when showing the changes between two diffs. It requires that the file is included in both revisions of a diffset.
filediff, no interfilediff, force_interdiff - Returns chunks showing the changes between a source diff and an unmodified version of the diff.
This is used when the source revision in the diffset contains modifications to a file which have then been reverted in the interdiff revision. We don’t actually have an interfilediff in this case, so we have to indicate that we are indeed in interdiff mode so that we can special-case this and not grab a patched file for the interdiff version.
-
__init__
(request, filediff, interfilediff=None, force_interdiff=False, enable_syntax_highlighting=True, base_filediff=None)[source]¶ Initialize the DiffChunkGenerator.
Parameters: - request (django.http.HttpRequest) – The HTTP request from the client.
- filediff (reviewboard.diffviewer.models.filediff.FileDiff) – The FileDiff to generate chunks for.
- interfilediff (reviewboard.diffviewer.models.filediff.FileDiff, optional) – If provided, the chunks will be generated for the differences
between the result of applying
filediff
and the result of applyinginterfilediff
. - force_interdiff (bool, optional) – Whether or not to force an interdiff.
- enable_syntax_highlighting (bool, optional) – Whether or not to enable syntax highlighting.
- base_filediff (reviewboard.diffviewer.models.filediff.FileDiff, optional) – An ancestor of
filediff
that we want to use as the base. Using this argument will result in the history betweenbase_filediff
andfilediff
being applied.
-
get_chunks
()[source]¶ Return the chunks for the given diff information.
If the file is binary or is an added or deleted 0-length file, or if the file has moved with no additional changes, then an empty list of chunks will be returned.
If there are chunks already computed in the cache, they will be yielded. Otherwise, new chunks will be generated, stored in cache, and yielded.
-
compute_chunk_last_header
(lines, numlines, meta, last_header=None)[source]¶ Computes information for the displayed function/class headers.
This will record the displayed headers, their line numbers, and expansion offsets relative to the header’s collapsed line range.
The last_header variable, if provided, will be modified, which is important when processing several chunks at once. It will also be returned as a convenience.
-
get_diff_chunk_generator_class
()[source]¶ Returns the DiffChunkGenerator class used for generating chunks.