mkinit.util.util_diff module¶
Port of ubelt utilities + difftext wrapper around difflib
- mkinit.util.util_diff.difftext(text1, text2, context_lines=0, ignore_whitespace=False, colored=False)[source]¶
Uses difflib to return a difference string between two similar texts
- Parameters:
text1 (str) – old text
text2 (str) – new text
context_lines (int) – number of lines of unchanged context
ignore_whitespace (bool)
colored (bool) – if true highlight the diff
- Returns:
formatted difference text message
- Return type:
References
http://www.java2s.com/Code/Python/Utility/IntelligentdiffbetweentextfilesTimPeters.htm
Example
>>> # build test data >>> text1 = 'one\ntwo\nthree' >>> text2 = 'one\ntwo\nfive' >>> # execute function >>> result = difftext(text1, text2) >>> # verify results >>> print(result) - three + five
Example
>>> # build test data >>> text1 = 'one\ntwo\nthree\n3.1\n3.14\n3.1415\npi\n3.4\n3.5\n4' >>> text2 = 'one\ntwo\nfive\n3.1\n3.14\n3.1415\npi\n3.4\n4' >>> # execute function >>> context_lines = 1 >>> result = difftext(text1, text2, context_lines, colored=True) >>> # verify results >>> print(result)
- mkinit.util.util_diff.highlight_code(text, lexer_name='python', **kwargs)[source]¶
Highlights a block of text using ANSI tags based on language syntax.
- Parameters:
text (str) – plain text to highlight
lexer_name (str) – name of language. eg: python, docker, c++
**kwargs – passed to pygments.lexers.get_lexer_by_name
- Returns:
- text - highlighted text
If pygments is not installed, the plain text is returned.
- Return type:
Example
>>> text = 'import mkinit; print(mkinit)' >>> new_text = highlight_code(text) >>> print(new_text)