Skip to content

Commit 88e9f4c

Browse files
committed
Move coloured_diff to consolekit.
1 parent 643003b commit 88e9f4c

File tree

2 files changed

+89
-83
lines changed

2 files changed

+89
-83
lines changed

domdf_python_tools/utils.py

Lines changed: 88 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -359,87 +359,6 @@ def head(obj: Union[Tuple, List, "DataFrame", "Series", String, HasHead], n: int
359359
return str(obj[:n]) + etc # type: ignore
360360

361361

362-
def coloured_diff(
363-
a: Sequence[str],
364-
b: Sequence[str],
365-
fromfile: str = '',
366-
tofile: str = '',
367-
fromfiledate: str = '',
368-
tofiledate: str = '',
369-
n: int = 3,
370-
lineterm: str = "\n",
371-
removed_colour: Colour = Fore.RED,
372-
added_colour: Colour = Fore.GREEN,
373-
) -> str:
374-
r"""
375-
Compare two sequences of lines; generate the delta as a unified diff.
376-
377-
Unified diffs are a compact way of showing line changes and a few
378-
lines of context. The number of context lines is set by ``n`` which
379-
defaults to three.
380-
381-
By default, the diff control lines (those with ``---``, ``+++``, or ``@@``)
382-
are created with a trailing newline. This is helpful so that inputs
383-
created from ``file.readlines()`` result in diffs that are suitable for
384-
``file.writelines()`` since both the inputs and outputs have trailing
385-
newlines.
386-
387-
For inputs that do not have trailing newlines, set the lineterm
388-
argument to ``''`` so that the output will be uniformly newline free.
389-
390-
The unidiff format normally has a header for filenames and modification
391-
times. Any or all of these may be specified using strings for
392-
``fromfile``, ``tofile``, ``fromfiledate``, and ``tofiledate``.
393-
The modification times are normally expressed in the ISO 8601 format.
394-
395-
**Example:**
396-
397-
>>> for line in coloured_diff('one two three four'.split(),
398-
... 'zero one tree four'.split(), 'Original', 'Current',
399-
... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
400-
... lineterm=''):
401-
... print(line) # doctest: +NORMALIZE_WHITESPACE
402-
--- Original 2005-01-26 23:30:50
403-
+++ Current 2010-04-02 10:20:52
404-
@@ -1,4 +1,4 @@
405-
+zero
406-
one
407-
-two
408-
-three
409-
+tree
410-
four
411-
412-
:param a:
413-
:param b:
414-
:param fromfile:
415-
:param tofile:
416-
:param fromfiledate:
417-
:param tofiledate:
418-
:param n:
419-
:param lineterm:
420-
:param removed_colour: The :class:`~domdf_python_tools.terminal_colours.Colour` to use for lines that were removed.
421-
:param added_colour: The :class:`~domdf_python_tools.terminal_colours.Colour` to use for lines that were added.
422-
"""
423-
424-
# this package
425-
from domdf_python_tools.stringlist import StringList
426-
427-
buf = StringList()
428-
diff = difflib.unified_diff(a, b, fromfile, tofile, fromfiledate, tofiledate, n, lineterm)
429-
430-
for line in diff:
431-
if line.startswith('+'):
432-
buf.append(added_colour(line))
433-
elif line.startswith('-'):
434-
buf.append(removed_colour(line))
435-
else:
436-
buf.append(line)
437-
438-
buf.blankline(ensure_single=True)
439-
440-
return str(buf)
441-
442-
443362
def deprecated(
444363
deprecated_in: Optional[str] = None,
445364
removed_in: Optional[str] = None,
@@ -654,7 +573,7 @@ def _inner(*args, **kwargs):
654573
deprecated_in="1.4.0",
655574
removed_in="2.0.0",
656575
current_version=__version__,
657-
details="Import from :mod:`packing_tape.requirements` instead.",
576+
details="Import from :mod:`shippinglabel.requirements` instead.",
658577
)
659578
def check_dependencies(dependencies: Iterable[str], prt: bool = True) -> List[str]:
660579
"""
@@ -686,3 +605,90 @@ def check_dependencies(dependencies: Iterable[str], prt: bool = True) -> List[st
686605
print('')
687606

688607
return missing_modules
608+
609+
610+
@deprecated(
611+
deprecated_in="1.4.0",
612+
removed_in="2.0.0",
613+
current_version=__version__,
614+
details="Import from :mod:`consolekit.utils` (v0.3.0 or later) instead.",
615+
)
616+
def coloured_diff(
617+
a: Sequence[str],
618+
b: Sequence[str],
619+
fromfile: str = '',
620+
tofile: str = '',
621+
fromfiledate: str = '',
622+
tofiledate: str = '',
623+
n: int = 3,
624+
lineterm: str = "\n",
625+
removed_colour: Colour = Fore.RED,
626+
added_colour: Colour = Fore.GREEN,
627+
) -> str:
628+
r"""
629+
Compare two sequences of lines; generate the delta as a unified diff.
630+
631+
Unified diffs are a compact way of showing line changes and a few
632+
lines of context. The number of context lines is set by ``n`` which
633+
defaults to three.
634+
635+
By default, the diff control lines (those with ``---``, ``+++``, or ``@@``)
636+
are created with a trailing newline. This is helpful so that inputs
637+
created from ``file.readlines()`` result in diffs that are suitable for
638+
``file.writelines()`` since both the inputs and outputs have trailing
639+
newlines.
640+
641+
For inputs that do not have trailing newlines, set the lineterm
642+
argument to ``''`` so that the output will be uniformly newline free.
643+
644+
The unidiff format normally has a header for filenames and modification
645+
times. Any or all of these may be specified using strings for
646+
``fromfile``, ``tofile``, ``fromfiledate``, and ``tofiledate``.
647+
The modification times are normally expressed in the ISO 8601 format.
648+
649+
**Example:**
650+
651+
>>> for line in coloured_diff('one two three four'.split(),
652+
... 'zero one tree four'.split(), 'Original', 'Current',
653+
... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
654+
... lineterm=''):
655+
... print(line) # doctest: +NORMALIZE_WHITESPACE
656+
--- Original 2005-01-26 23:30:50
657+
+++ Current 2010-04-02 10:20:52
658+
@@ -1,4 +1,4 @@
659+
+zero
660+
one
661+
-two
662+
-three
663+
+tree
664+
four
665+
666+
:param a:
667+
:param b:
668+
:param fromfile:
669+
:param tofile:
670+
:param fromfiledate:
671+
:param tofiledate:
672+
:param n:
673+
:param lineterm:
674+
:param removed_colour: The :class:`~domdf_python_tools.terminal_colours.Colour` to use for lines that were removed.
675+
:param added_colour: The :class:`~domdf_python_tools.terminal_colours.Colour` to use for lines that were added.
676+
"""
677+
678+
# this package
679+
from domdf_python_tools.stringlist import StringList
680+
681+
buf = StringList()
682+
diff = difflib.unified_diff(a, b, fromfile, tofile, fromfiledate, tofiledate, n, lineterm)
683+
684+
for line in diff:
685+
if line.startswith('+'):
686+
buf.append(added_colour(line))
687+
elif line.startswith('-'):
688+
buf.append(removed_colour(line))
689+
else:
690+
buf.append(line)
691+
692+
buf.blankline(ensure_single=True)
693+
694+
return str(buf)

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010

1111
@pytest.fixture()
12-
def original_datadir(request):
12+
def original_datadir(request) -> Path:
1313
# Work around pycharm confusing datadir with test file.
1414
return Path(os.path.splitext(request.module.__file__)[0] + "_")

0 commit comments

Comments
 (0)