Skip to content

Commit

Permalink
Merge pull request #470 from patroni/master
Browse files Browse the repository at this point in the history
Syncing from upstream patroni/patroni (master)
  • Loading branch information
bt-admin authored Nov 20, 2024
2 parents d10bf02 + a903438 commit adea18c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 14 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ jobs:
with:
version: 1.1.385

ydiff:
name: Test compatibility with the latest version of ydiff
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install dependencies
run: python .github/workflows/install_deps.py
- name: Update ydiff
run: python -m pip install -U ydiff
- name: Run tests
run: python -m pytest tests/test_ctl.py -v

docs:
runs-on: ubuntu-latest
steps:
Expand Down
16 changes: 12 additions & 4 deletions patroni/ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@
import urllib3
import yaml

from prettytable import ALL, FRAME, PrettyTable
from prettytable import PrettyTable

try: # pragma: no cover
from prettytable import HRuleStyle
hrule_all = HRuleStyle.ALL
hrule_frame = HRuleStyle.FRAME
except ImportError: # pragma: no cover
from prettytable import ALL as hrule_all, FRAME as hrule_frame

if TYPE_CHECKING: # pragma: no cover
from psycopg import Cursor
Expand Down Expand Up @@ -437,7 +444,7 @@ def print_output(columns: Optional[List[str]], rows: List[List[Any]], alignment:
else:
# If any value is multi-line, then add horizontal between all table rows while printing to get a clear
# visual separation of rows.
hrules = ALL if any(any(isinstance(c, str) and '\n' in c for c in r) for r in rows) else FRAME
hrules = hrule_all if any(any(isinstance(c, str) and '\n' in c for c in r) for r in rows) else hrule_frame
table = PatronictlPrettyTable(header, columns, hrules=hrules)
table.align = 'l'
for k, v in (alignment or {}).items():
Expand Down Expand Up @@ -1889,12 +1896,13 @@ def listify(string: str) -> List[str]:
unified_diff = difflib.unified_diff(listify(before_editing), listify(after_editing))

if sys.stdout.isatty():
buf = io.StringIO()
buf = io.BytesIO()
for line in unified_diff:
buf.write(str(line))
buf.write(line.encode('utf-8'))
buf.seek(0)

class opts:
theme = 'default'
side_by_side = False
width = 80
tab_width = 8
Expand Down
6 changes: 3 additions & 3 deletions patroni/dcs/consul.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.token = kwargs.get('token')
super(ConsulClient, self).__init__(*args, **kwargs)

def connect(self, *args: Any, **kwargs: Any) -> HTTPClient:
def http_connect(self, *args: Any, **kwargs: Any) -> HTTPClient:
kwargs.update(dict(zip(['host', 'port', 'scheme', 'verify'], args)))
if self._cert:
kwargs['cert'] = self._cert
Expand All @@ -191,8 +191,8 @@ def connect(self, *args: Any, **kwargs: Any) -> HTTPClient:
kwargs['token'] = self.token
return HTTPClient(**kwargs)

def http_connect(self, *args: Any, **kwargs: Any) -> HTTPClient:
return self.connect(*args, **kwargs) # pragma: no cover
def connect(self, *args: Any, **kwargs: Any) -> HTTPClient:
return self.http_connect(*args, **kwargs) # pragma: no cover

def reload_config(self, config: Dict[str, Any]) -> None:
self.http.token = self.token = config.get('token')
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ python-dateutil
pysyncobj>=0.3.8
cryptography>=1.4
psutil>=2.0.0
ydiff>=1.2.0
ydiff>=1.2.0,<1.5,!=1.4.0,!=1.4.1
python-json-logger>=2.0.2
22 changes: 20 additions & 2 deletions tests/test_ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
import etcd

from click.testing import CliRunner
from prettytable import ALL, PrettyTable
from prettytable import PrettyTable

try:
from prettytable import HRuleStyle
hrule_all = HRuleStyle.ALL
except ImportError:
from prettytable import ALL as hrule_all

from urllib3 import PoolManager

from patroni import global_config
Expand Down Expand Up @@ -682,6 +689,17 @@ def test_show_diff(self, mock_which, mock_env_get, mock_markup_to_pager, mock_is
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'))

@patch('subprocess.Popen')
@patch('os.environ.get', Mock(return_value='cat'))
@patch('sys.stdout.isatty', Mock(return_value=True))
@patch('shutil.which', Mock(return_value='cat'))
def test_show_diff_pager(self, mock_popen):
show_diff("foo:\n bar: 1\n", "foo:\n bar: 2\n")
self.assertEqual(mock_popen.return_value.stdin.write.call_count, 6)
self.assertIn(b' bar: ', mock_popen.return_value.stdin.write.call_args_list[5][0][0])
self.assertIn(b' bar: ', mock_popen.return_value.stdin.write.call_args_list[4][0][0])
self.assertIn(b' foo:', mock_popen.return_value.stdin.write.call_args_list[3][0][0])

@patch('subprocess.call', return_value=1)
def test_invoke_editor(self, mock_subprocess_call):
os.environ.pop('EDITOR', None)
Expand Down Expand Up @@ -747,7 +765,7 @@ def test_reinit_wait(self):
class TestPatronictlPrettyTable(unittest.TestCase):

def setUp(self):
self.pt = PatronictlPrettyTable(' header', ['foo', 'bar'], hrules=ALL)
self.pt = PatronictlPrettyTable(' header', ['foo', 'bar'], hrules=hrule_all)

def test__get_hline(self):
expected = '+-----+-----+'
Expand Down
2 changes: 1 addition & 1 deletion typings/cdiff/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import io
from typing import Any
class PatchStream:
def __init__(self, diff_hdl: io.TextIOBase) -> None: ...
def __init__(self, diff_hdl: io.BytesIOBase) -> None: ...
def markup_to_pager(stream: Any, opts: Any) -> None: ...
8 changes: 6 additions & 2 deletions typings/prettytable/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from enum import IntEnum
from typing import Any, Dict, List
FRAME = 1
ALL = 1
class HRuleStyle(IntEnum):
FRAME = 0
ALL = 1
FRAME = HRuleStyle.FRAME
ALL = HRuleStyle.ALL
class PrettyTable:
def __init__(self, *args: str, **kwargs: Any) -> None: ...
def _stringify_hrule(self, options: Dict[str, Any], where: str = '') -> str: ...
Expand Down
2 changes: 1 addition & 1 deletion typings/ydiff/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import io
from typing import Any
class PatchStream:
def __init__(self, diff_hdl: io.TextIOBase) -> None: ...
def __init__(self, diff_hdl: io.BytesIOBase) -> None: ...
def markup_to_pager(stream: Any, opts: Any) -> None: ...

0 comments on commit adea18c

Please sign in to comment.