Skip to content

Commit 1a71697

Browse files
committed
Handle integers too when printing CSV
Signed-off-by: Stephen Finucane <stephen@that.guru> Fixes: 63e8ea1 ("Resolves issues with CSV printing on Python 2")
1 parent 1324b5f commit 1a71697

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

git_pw/utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
from tabulate import tabulate
1616

1717

18+
def ensure_str(s):
19+
if s is None:
20+
s = ''
21+
elif not isinstance(s, (six.text_type, six.binary_type)):
22+
s = str(s)
23+
24+
return six.ensure_str(s)
25+
26+
1827
def trim(string, length=70): # type: (str, int) -> str
1928
"""Trim a string to the given length."""
2029
return (string[:length - 1] + '...') if len(string) > length else string
@@ -63,10 +72,9 @@ def _tabulate(output, headers, fmt):
6372
result = six.StringIO()
6473
writer = csv.writer(
6574
result, quoting=csv.QUOTE_ALL, lineterminator=os.linesep)
66-
writer.writerow([six.ensure_str(h) for h in headers])
75+
writer.writerow([ensure_str(h) for h in headers])
6776
for item in output:
68-
writer.writerow([
69-
six.ensure_str(x if x is not None else '') for x in item])
77+
writer.writerow([ensure_str(i) for i in item])
7078
return result.getvalue()
7179

7280
print('pw.format must be one of: table, simple, csv')

tests/test_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def test_echo_via_pager_env_default(mock_inner, mock_tabulate, mock_config):
8484

8585

8686
def _test_tabulate(fmt):
87-
output = [(b'foo', 'bar', u'baz', '😀', None)]
88-
headers = ('col1', 'colb', 'colIII', 'colX', 'colY')
87+
output = [(b'foo', 'bar', u'baz', '😀', None, 1)]
88+
headers = ('col1', 'colb', 'colIII', 'colX', 'colY', 'colZ')
8989

9090
result = utils._tabulate(output, headers, fmt)
9191

@@ -114,8 +114,8 @@ def test_tabulate_csv(mock_tabulate):
114114

115115
mock_tabulate.assert_not_called()
116116
assert result == textwrap.dedent("""\
117-
"col1","colb","colIII","colX","colY"
118-
"foo","bar","baz","😀",""
117+
"col1","colb","colIII","colX","colY","colZ"
118+
"foo","bar","baz","😀","","1"
119119
""")
120120

121121

0 commit comments

Comments
 (0)