Skip to content

Commit 019342e

Browse files
committed
utils: Add support for 'yaml' formatter
This is helpful in environments where limited horizontal space is available. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent 1dcbcb2 commit 019342e

File tree

6 files changed

+41
-7
lines changed

6 files changed

+41
-7
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ dependencies. On Fedora, run:
4747
.. code-block:: bash
4848
4949
$ sudo dnf install python3-requests python3-click python3-pbr \
50-
python3-arrow python3-tabulate
50+
python3-arrow python3-tabulate python3-yaml
5151
5252
On Ubuntu, run:
5353

5454
.. code-block:: bash
5555
5656
$ sudo apt-get install python3-requests python3-click python3-pbr \
57-
python3-arrow python3-tabulate
57+
python3-arrow python3-tabulate python3-yaml
5858
5959
Once dependencies are installed, clone this repo and run ``setup.py``:
6060

git_pw/patch.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,10 @@ def list_cmd(states, submitters, delegates, hashes, archived, limit, page,
278278
patch.get('id'),
279279
arrow.get(patch.get('date')).humanize(),
280280
utils.trim(patch.get('name')),
281-
'%s (%s)' % (patch.get('submitter').get('name'),
282-
patch.get('submitter').get('email')),
281+
'%s (%s)' % (
282+
patch.get('submitter').get('name'),
283+
patch.get('submitter').get('email'),
284+
),
283285
patch.get('state'),
284286
'yes' if patch.get('archived') else 'no',
285287
(patch.get('delegate') or {}).get('username', ''),

git_pw/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import click
1414
from tabulate import tabulate
15+
import yaml
1516

1617
LOG = logging.getLogger(__name__)
1718

@@ -92,8 +93,14 @@ def _tabulate(
9293
for item in output:
9394
writer.writerow([ensure_str(i) for i in item])
9495
return result.getvalue()
95-
96-
LOG.error('pw.format must be one of: table, simple, csv')
96+
elif fmt == 'yaml':
97+
data = [
98+
{headers[i].lower(): entry[i] for i in range(len(headers))}
99+
for entry in output
100+
]
101+
return yaml.dump(data, default_flow_style=False)
102+
103+
LOG.error('pw.format must be one of: table, simple, csv, yaml')
97104
sys.exit(1)
98105

99106

@@ -210,7 +217,7 @@ def _format_options(f):
210217
'-f',
211218
'fmt',
212219
default=None,
213-
type=click.Choice(['simple', 'table', 'csv']),
220+
type=click.Choice(['simple', 'table', 'csv', 'yaml']),
214221
help=(
215222
"Output format. Defaults to the value of "
216223
"'git config pw.format' else 'table'."
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
The ``-f`` / ``--format`` option used for many ``list`` commands now
5+
supports a new option: ``yaml``. This can be useful in environments where
6+
limited horizontal space is available.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ click>=6.0,<9.0
22
requests>2.0,<3.0
33
tabulate>=0.8
44
arrow>=0.10
5+
PyYAML>=5.1

tests/test_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88

99
import mock
10+
import yaml
1011

1112
from git_pw import utils
1213

@@ -120,6 +121,23 @@ def test_tabulate_csv(mock_tabulate):
120121
""")
121122

122123

124+
@mock.patch.object(yaml, 'dump')
125+
def test_tabulate_yaml(mock_dump):
126+
output, headers, result = _test_tabulate('yaml')
127+
128+
mock_dump.assert_called_once_with(
129+
[{
130+
'col1': b'foo',
131+
'colb': 'bar',
132+
'coliii': u'baz',
133+
'colx': '😀',
134+
'coly': None,
135+
'colz': 1,
136+
}],
137+
default_flow_style=False,
138+
)
139+
140+
123141
@mock.patch.object(utils, 'git_config', return_value='simple')
124142
@mock.patch.object(utils, 'tabulate')
125143
def test_tabulate_git_config(mock_tabulate, mock_git_config):

0 commit comments

Comments
 (0)