From e27ff480d0ea827a1b84d995c90ca78690a26d20 Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Wed, 16 Sep 2020 15:21:52 +0200 Subject: [PATCH] Allow custom pager support in patronictl edit-config (#1696) Fixes #1695 --- patroni/ctl.py | 14 ++++++++++++-- requirements.txt | 2 +- tests/test_ctl.py | 5 ++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/patroni/ctl.py b/patroni/ctl.py index 3d900dfd..30070545 100644 --- a/patroni/ctl.py +++ b/patroni/ctl.py @@ -7,7 +7,6 @@ import codecs import datetime import dateutil.parser import dateutil.tz -import cdiff import copy import difflib import io @@ -34,6 +33,10 @@ from patroni.request import PatroniRequest from patroni.version import __version__ from prettytable import ALL, FRAME, PrettyTable from six.moves.urllib_parse import urlparse +try: + from ydiff import markup_to_pager, PatchStream +except ImportError: # pragma: no cover + from cdiff import markup_to_pager, PatchStream CONFIG_DIR_PATH = click.get_app_dir('patroni') CONFIG_FILE_PATH = os.path.join(CONFIG_DIR_PATH, 'patronictl.yaml') @@ -1086,7 +1089,14 @@ def show_diff(before_editing, after_editing): side_by_side = False width = 80 tab_width = 8 - cdiff.markup_to_pager(cdiff.PatchStream(buf), opts) + wrap = True + if find_executable('less'): + pager = None + else: + pager = 'more.com' if sys.platform == 'win32' else 'more' + pager_options = None + + markup_to_pager(PatchStream(buf), opts) else: for line in unified_diff: click.echo(line.rstrip('\n')) diff --git a/requirements.txt b/requirements.txt index 5c30d3a0..8e568c9f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,4 @@ prettytable>=0.7 python-dateutil pysyncobj>=0.3.5 psutil>=2.0.0 -cdiff +ydiff>=1.2.0 diff --git a/tests/test_ctl.py b/tests/test_ctl.py index e63da17d..d93c5d1f 100644 --- a/tests/test_ctl.py +++ b/tests/test_ctl.py @@ -561,7 +561,7 @@ class TestCtl(unittest.TestCase): self.assertRaises(PatroniCtlException, apply_config_changes, before_editing, config, ['a']) @patch('sys.stdout.isatty', return_value=False) - @patch('cdiff.markup_to_pager') + @patch('patroni.ctl.markup_to_pager') def test_show_diff(self, mock_markup_to_pager, mock_isatty): show_diff("foo:\n bar: 1\n", "foo:\n bar: 2\n") mock_markup_to_pager.assert_not_called() @@ -570,6 +570,9 @@ class TestCtl(unittest.TestCase): show_diff("foo:\n bar: 1\n", "foo:\n bar: 2\n") mock_markup_to_pager.assert_called_once() + with patch('patroni.ctl.find_executable', Mock(return_value=None)): + show_diff("foo:\n bar: 1\n", "foo:\n bar: 2\n") + # Test that unicode handling doesn't fail with an exception show_diff(b"foo:\n bar: \xc3\xb6\xc3\xb6\n".decode('utf-8'), b"foo:\n bar: \xc3\xbc\xc3\xbc\n".decode('utf-8'))