Print functions take output file parameter
Functions print_dict and print_list take outfile parameter (file where to print output). This indirection allows for testing without patching sys.stdout (patching stdout can break debugging tools). Change-Id: Id4b76c5b7d9a8509eb0c662b498855cf9b261a82
This commit is contained in:
parent
02f20fc0c7
commit
774fa137c8
@ -10,6 +10,10 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
import prettytable
|
import prettytable
|
||||||
|
|
||||||
|
|
||||||
@ -17,7 +21,8 @@ def pretty_choice_list(l):
|
|||||||
return ', '.join("'%s'" % i for i in l)
|
return ', '.join("'%s'" % i for i in l)
|
||||||
|
|
||||||
|
|
||||||
def print_list(objs, fields, formatters={}, custom_labels={}, sortby=0):
|
def print_list(objs, fields, formatters={}, custom_labels={}, sortby=0,
|
||||||
|
outfile=sys.stdout):
|
||||||
'''Prints a list of objects.
|
'''Prints a list of objects.
|
||||||
|
|
||||||
:param objs: list of objects to print
|
:param objs: list of objects to print
|
||||||
@ -53,10 +58,10 @@ def print_list(objs, fields, formatters={}, custom_labels={}, sortby=0):
|
|||||||
else:
|
else:
|
||||||
row.append(getattr(o, field, ''))
|
row.append(getattr(o, field, ''))
|
||||||
pt.add_row(row)
|
pt.add_row(row)
|
||||||
print(pt.get_string(sortby=field_labels[sortby]))
|
print(pt.get_string(sortby=field_labels[sortby]), file=outfile)
|
||||||
|
|
||||||
|
|
||||||
def print_dict(d, formatters={}, custom_labels={}):
|
def print_dict(d, formatters={}, custom_labels={}, outfile=sys.stdout):
|
||||||
'''Prints a dict.
|
'''Prints a dict.
|
||||||
|
|
||||||
:param d: dict to print
|
:param d: dict to print
|
||||||
@ -77,7 +82,7 @@ def print_dict(d, formatters={}, custom_labels={}):
|
|||||||
pt.add_row([label, formatters[field](d[field])])
|
pt.add_row([label, formatters[field](d[field])])
|
||||||
else:
|
else:
|
||||||
pt.add_row([label, d[field]])
|
pt.add_row([label, d[field]])
|
||||||
print(pt.get_string(sortby='Property'))
|
print(pt.get_string(sortby='Property'), file=outfile)
|
||||||
|
|
||||||
|
|
||||||
def attr_proxy(attr, formatter=lambda a: a, allow_undefined=True):
|
def attr_proxy(attr, formatter=lambda a: a, allow_undefined=True):
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import io
|
||||||
import mock
|
import mock
|
||||||
from six import StringIO
|
|
||||||
|
|
||||||
import tuskarclient.common.formatting as fmt
|
import tuskarclient.common.formatting as fmt
|
||||||
import tuskarclient.tests.utils as tutils
|
import tuskarclient.tests.utils as tutils
|
||||||
@ -19,13 +19,17 @@ import tuskarclient.tests.utils as tutils
|
|||||||
|
|
||||||
class PrintTest(tutils.TestCase):
|
class PrintTest(tutils.TestCase):
|
||||||
|
|
||||||
@mock.patch('sys.stdout', new_callable=StringIO)
|
def setUp(self):
|
||||||
def test_print_dict(self, mock_out):
|
super(PrintTest, self).setUp()
|
||||||
|
self.outfile = io.StringIO()
|
||||||
|
|
||||||
|
def test_print_dict(self):
|
||||||
dict_ = {'k': 'v', 'key': 'value'}
|
dict_ = {'k': 'v', 'key': 'value'}
|
||||||
formatters = {'key': lambda v: 'custom ' + v}
|
formatters = {'key': lambda v: 'custom ' + v}
|
||||||
custom_labels = {'k': 'custom_key'}
|
custom_labels = {'k': 'custom_key'}
|
||||||
|
|
||||||
fmt.print_dict(dict_, formatters, custom_labels)
|
fmt.print_dict(dict_, formatters, custom_labels,
|
||||||
|
outfile=self.outfile)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
('+------------+--------------+\n'
|
('+------------+--------------+\n'
|
||||||
'| Property | Value |\n'
|
'| Property | Value |\n'
|
||||||
@ -33,11 +37,10 @@ class PrintTest(tutils.TestCase):
|
|||||||
'| custom_key | v |\n'
|
'| custom_key | v |\n'
|
||||||
'| key | custom value |\n'
|
'| key | custom value |\n'
|
||||||
'+------------+--------------+\n'),
|
'+------------+--------------+\n'),
|
||||||
mock_out.getvalue()
|
self.outfile.getvalue()
|
||||||
)
|
)
|
||||||
|
|
||||||
@mock.patch('sys.stdout', new_callable=StringIO)
|
def test_print_list(self):
|
||||||
def test_print_list(self, mock_out):
|
|
||||||
fields = ['thing', 'color', '!artistic_name']
|
fields = ['thing', 'color', '!artistic_name']
|
||||||
formatters = {
|
formatters = {
|
||||||
'!artistic_name': lambda obj: '{0} {1}'.format(obj.color,
|
'!artistic_name': lambda obj: '{0} {1}'.format(obj.color,
|
||||||
@ -46,7 +49,8 @@ class PrintTest(tutils.TestCase):
|
|||||||
}
|
}
|
||||||
custom_labels = {'thing': 'name', '!artistic_name': 'artistic name'}
|
custom_labels = {'thing': 'name', '!artistic_name': 'artistic name'}
|
||||||
|
|
||||||
fmt.print_list(self.objects(), fields, formatters, custom_labels)
|
fmt.print_list(self.objects(), fields, formatters, custom_labels,
|
||||||
|
outfile=self.outfile)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
('+------+-------+-----------------+\n'
|
('+------+-------+-----------------+\n'
|
||||||
'| name | color | artistic name |\n'
|
'| name | color | artistic name |\n'
|
||||||
@ -54,11 +58,10 @@ class PrintTest(tutils.TestCase):
|
|||||||
'| moon | green | dark green moon |\n'
|
'| moon | green | dark green moon |\n'
|
||||||
'| sun | blue | bright blue sun |\n'
|
'| sun | blue | bright blue sun |\n'
|
||||||
'+------+-------+-----------------+\n'),
|
'+------+-------+-----------------+\n'),
|
||||||
mock_out.getvalue()
|
self.outfile.getvalue()
|
||||||
)
|
)
|
||||||
|
|
||||||
@mock.patch('sys.stdout', new_callable=StringIO)
|
def test_print_list_custom_field_without_formatter(self):
|
||||||
def test_print_list_custom_field_without_formatter(self, mock_out):
|
|
||||||
fields = ['!artistic_name']
|
fields = ['!artistic_name']
|
||||||
|
|
||||||
self.assertRaises(KeyError, fmt.print_list, self.objects(), fields)
|
self.assertRaises(KeyError, fmt.print_list, self.objects(), fields)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user