reviewboard.diffviewer.chunk_generator¶
- class NoWrapperHtmlFormatter(*args, **kwargs)[source]¶
Bases:
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=None, encoding_list=None, diff_compat=2, *, diff_settings=None)[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.
Chunk generator instances must be recreated for each new file being parsed.
Changed in version 5.0: Added
all_code_safety_results
.- all_code_safety_results¶
Code safety warnings were found while processing the diff.
This is in the form of:
{ '<checker_id>': { 'warnings': {'<result_id>', ...}, 'errors': {'<result_id>', ...}, }, ... }
All keys are optional.
New in version 5.0.
- Type:
- __init__(old, new, orig_filename, modified_filename, enable_syntax_highlighting=None, encoding_list=None, diff_compat=2, *, diff_settings=None)[source]¶
Initialize the chunk generator.
Changed in version 5.0.2:
Added
diff_settings
, which will be required starting in Review Board 6.Deprecated
enable_syntax_highlighting
in favor ofdiff_settings
.
- Parameters:
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 default to enabling syntax highlighting if
diff_settings
is not provided.Deprecated since version 5.0.2: This has been replaced with
diff_settings
.encoding_list (
list
ofunicode
, optional) – A list of encodings to try for theold
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.diff_settings (
reviewboard.diffviewer.settings.DiffSettings
) –The settings used to control the display of diffs.
New in version 5.0.2.
- diff_settings: DiffSettings¶
Settings used for the generation of the diff.
New in version 5.0.2.
- Type:
reviewboard.diffviewer.settings.DiffSettings
- 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:
- 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.
- check_line_code_safety(orig_line, modified_line, extra_state={}, **kwargs)[source]¶
Check the safety of a line of code.
This will run the original and modified line through all registered code safety checkers. If any checker produces warnings or errors, those will be associated with the line.
New in version 5.0.
- Parameters:
orig_line (
str
) – The original line to check.modified_line (
str
) – The modiifed line to check.extra_state (
dict
, optional) – Extra state to pass to the checker for the original or modified line content item. Used by subclasses to produce additional information that may be useful for some code safety checkers.**kwargs (
dict
, optional) – Unused keyword arguments, for future expansion.
- Returns:
A list of code safety results containing warnings or errors. Each item is a tuple containing:
The registered checker ID.
A dictionary with
errors
and/orwarnings
keys.
- Return type:
- 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(lines, 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:
- Returns:
The resulting list of normalized lines.
- Return type:
- 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.
- 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.
- __annotations__ = {'diff_settings': <class 'reviewboard.diffviewer.settings.DiffSettings'>}¶
- class DiffChunkGenerator(request, filediff, interfilediff=None, force_interdiff=False, enable_syntax_highlighting=None, base_filediff=None, *, diff_settings=None)[source]¶
Bases:
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=None, base_filediff=None, *, diff_settings=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 applyingfilediff
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 offilediff
that we want to use as the base. Using this argument will result in the history betweenbase_filediff
andfilediff
being applied.
- make_cache_key() str [source]¶
Return a new cache key for any generated chunks.
- Returns:
The new cache key.
- Return type:
- 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.
- check_line_code_safety(orig_line, modified_line, extra_state={}, **kwargs)[source]¶
Check the safety of a line of code.
This will run the original and modified line through all registered code safety checkers. If any checker produces warnings or errors, those will be associated with the line.
This is a specialization of
RawDiffChunkGenerator.check_line_code_safety()
that provides arepository
key for each item to check, for use in the code safety checker.New in version 5.0.
- Parameters:
orig_line (
str
) – The original line to check.modified_line (
str
) – The modiifed line to check.extra_state (
dict
, optional) – Extra state to pass to the checker for the original or modified line content item. Used by subclasses to produce additional information that may be useful for some code safety checkers.**kwargs (
dict
, optional) – Unused keyword arguments, for future expansion.
- Returns:
A list of code safety results containing warnings or errors. Each item is a tuple containing:
The registered checker ID.
A dictionary with
errors
and/orwarnings
keys.
- Return type:
- normalize_path_for_display(filename)[source]¶
Normalize a file path for display to the user.
This uses the associated
SCMTool
to normalize the filename.
- diff_settings: DiffSettings¶
Settings used for the generation of the diff.
New in version 5.0.2.
- Type:
reviewboard.diffviewer.settings.DiffSettings
- 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.