From 47d8e8470bd251bbc149ee21f79e5d84fba0dfee Mon Sep 17 00:00:00 2001 From: John Evans Date: Mon, 10 Feb 2014 06:58:11 -0500 Subject: [PATCH] Print options remaining persistent across invocations. #162 --- glymur/jp2box.py | 9 +++++---- glymur/test/test_printing.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/glymur/jp2box.py b/glymur/jp2box.py index d0b6c07..12613fa 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -2829,7 +2829,7 @@ _BOX_WITH_ID = { _printoptions = {'short': False, 'xml': True, 'codestream': True} -def set_printoptions(short=False, xml=True, codestream=True): +def set_printoptions(**kwargs): """Set printing options. These options determine the way JPEG 2000 boxes are displayed. @@ -2856,9 +2856,10 @@ def set_printoptions(short=False, xml=True, codestream=True): >>> import glymur >>> glymur.set_printoptions(short=False, xml=True, codestream=True) """ - _printoptions['short'] = short - _printoptions['xml'] = xml - _printoptions['codestream'] = codestream + for key, value in kwargs.items(): + if key not in ['short', 'xml', 'codestream']: + raise TypeError('"{0}" not a valid keyword parameter.'.format(key)) + _printoptions[key] = value def get_printoptions(): """Return the current print options. diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index fa9ab0c..56fa4f0 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -48,6 +48,26 @@ class TestPrinting(unittest.TestCase): def tearDown(self): pass + def test_printoptions_bad_argument(self): + """Verify error when bad parameter to set_printoptions""" + with self.assertRaises(TypeError): + glymur.set_printoptions(hi='low') + + def test_printopt_no_codestr_then_no_xml(self): + """Verify printed output when codestream=False and xml=False, #162""" + # The print options should be persistent across invocations. + glymur.set_printoptions(codestream=False) + glymur.set_printoptions(xml=False) + with patch('sys.stdout', new=StringIO()) as fake_out: + glymur.jp2dump(self.jp2file) + actual = fake_out.getvalue().strip() + + # Get rid of the filename line, as it is not set in stone. + lst = actual.split('\n') + lst = lst[1:] + actual = '\n'.join(lst) + self.assertEqual(actual, fixtures.nemo_dump_no_codestream_no_xml) + def test_printopt_no_codestr_or_xml(self): """Verify printed output when codestream=False and xml=False""" glymur.set_printoptions(codestream=False, xml=False)