From 3f3749480310e776057e2918820ff82d4e9a27f1 Mon Sep 17 00:00:00 2001 From: John Evans Date: Sat, 1 Mar 2014 12:16:42 -0500 Subject: [PATCH] Fixing indentation issues with textwrap. #176 XML had been flush left since introducing lxml, but the textwrap module fixes that easily. --- glymur/jp2box.py | 27 +++-- glymur/test/fixtures.py | 198 +++-------------------------------- glymur/test/test_printing.py | 1 + 3 files changed, 30 insertions(+), 196 deletions(-) diff --git a/glymur/jp2box.py b/glymur/jp2box.py index b4cc803..5a0febf 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -20,6 +20,7 @@ import os import pprint import struct import sys +import textwrap import uuid import warnings @@ -96,10 +97,8 @@ class Jp2kBox(object): msg = Jp2kBox.__str__(self) for box in self.box: boxstr = str(box) - - # Add indentation. - strs = [('\n ' + x) for x in boxstr.split('\n')] - msg += ''.join(strs) + # Indent the child boxes to make the association clear. + msg += '\n' + textwrap.indent(boxstr, ' ') return msg @@ -889,10 +888,7 @@ class ContiguousCodestreamBox(Jp2kBox): msg += '\n Main header:' for segment in self.main_header.segment: - segstr = str(segment) - # Add indentation. - strs = [('\n ' + x) for x in segstr.split('\n')] - msg += ''.join(strs) + msg += '\n' + textwrap.indent(str(segment), ' ') return msg @@ -2562,14 +2558,14 @@ class XMLBox(Jp2kBox): if _printoptions['xml'] == False: return msg - xml = self.xml + msg += '\n' if self.xml is not None: - bstr = ET.tostring(self.xml, - encoding='utf-8', - pretty_print=True).decode('utf-8') - msg += '\n {0}'.format(bstr) + xmlstring = ET.tostring(self.xml, + encoding='utf-8', + pretty_print=True).decode('utf-8') else: - msg += '\n {0}'.format(xml) + xmlstring = 'None' + msg += textwrap.indent(xmlstring, ' ') return msg def write(self, fptr): @@ -2977,7 +2973,8 @@ class UUIDBox(Jp2kBox): xmlstring = ET.tostring(self.data, encoding='utf-8', pretty_print=True).decode('utf-8') - xmlstring = xmlstring.rstrip() + # indent it a bit + xmlstring = textwrap.indent(xmlstring.rstrip(), ' ') msg += line.format(xmlstring) elif self.uuid.bytes == b'JpgTiffExif->JP2': msg += '\n UUID Data: {0}'.format(str(self.data)) diff --git a/glymur/test/fixtures.py b/glymur/test/fixtures.py index e7c81a3..b20331c 100644 --- a/glymur/test/fixtures.py +++ b/glymur/test/fixtures.py @@ -4,6 +4,7 @@ Test fixtures common to more than one test point. import os import re import sys +import textwrap import warnings import numpy as np @@ -181,10 +182,7 @@ def read_pgx_header(pgx_file): header = header.rstrip() return header, pos -nemo_xmp_box = """UUID Box (uuid) @ (77, 3146) - UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP) - UUID Data: - +nemo_xmp = """ @@ -269,6 +267,10 @@ nemo_xmp_box = """UUID Box (uuid) @ (77, 3146) """ +nemo_xmp_box = """UUID Box (uuid) @ (77, 3146) + UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP) + UUID Data: +{0}""".format(textwrap.indent(nemo_xmp, ' ')) SimpleRDF = """ - - - - Google - 2013-02-09T14:47:53 - - - 1 - 72/1 - 72/1 - 2 - HTC - HTC Glacier - 2592 - 1456 - - - 8 - 8 - 8 - - - 2 - 3 - - - 1343036288/4294967295 - 1413044224/4294967295 - - - - - 2748779008/4294967295 - 1417339264/4294967295 - 1288490240/4294967295 - 2576980480/4294967295 - 644245120/4294967295 - 257698032/4294967295 - - - - - 1 - 2528 - 1424 - 353/100 - 0 - 0/1 - WGS-84 - 2013-02-09T14:47:53 - - - 76 - - - 0220 - 0100 - - - 1 - 2 - 3 - 0 - - - 42,20.56N - 71,5.29W - 2013-02-09T19:47:53Z - NETWORK - - - 2013-02-09T14:47:53 - - - - - Glymur - Python XMP Toolkit - - - - - - +{0} Contiguous Codestream Box (jp2c) @ (3223, 1132296) Main header: SOC marker segment @ (3231, 0) @@ -496,6 +414,7 @@ Contiguous Codestream Box (jp2c) @ (3223, 1132296) Step size: [(0, 8), (0, 9), (0, 9), (0, 10)] CME marker segment @ (3305, 37) "Created by OpenJPEG version 2.0.0"''' +nemo_dump_full = dump.format(textwrap.indent(nemo_xmp, ' ')) nemo_dump_short = r"""JPEG 2000 Signature Box (jP ) @ (0, 12) File Type Box (ftyp) @ (12, 20) @@ -561,7 +480,7 @@ Contiguous Codestream Box (jp2c) @ (3223, 1132296) CME marker segment @ (3305, 37) "Created by OpenJPEG version 2.0.0"''' -nemo_dump_no_codestream = r"""JPEG 2000 Signature Box (jP ) @ (0, 12) +dump = r"""JPEG 2000 Signature Box (jP ) @ (0, 12) Signature: 0d0a870a File Type Box (ftyp) @ (12, 20) Brand: jp2 @@ -580,92 +499,9 @@ JP2 Header Box (jp2h) @ (32, 45) UUID Box (uuid) @ (77, 3146) UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP) UUID Data: - - - - - Google - 2013-02-09T14:47:53 - - - 1 - 72/1 - 72/1 - 2 - HTC - HTC Glacier - 2592 - 1456 - - - 8 - 8 - 8 - - - 2 - 3 - - - 1343036288/4294967295 - 1413044224/4294967295 - - - - - 2748779008/4294967295 - 1417339264/4294967295 - 1288490240/4294967295 - 2576980480/4294967295 - 644245120/4294967295 - 257698032/4294967295 - - - - - 1 - 2528 - 1424 - 353/100 - 0 - 0/1 - WGS-84 - 2013-02-09T14:47:53 - - - 76 - - - 0220 - 0100 - - - 1 - 2 - 3 - 0 - - - 42,20.56N - 71,5.29W - 2013-02-09T19:47:53Z - NETWORK - - - 2013-02-09T14:47:53 - - - - - Glymur - Python XMP Toolkit - - - - - - +{0} Contiguous Codestream Box (jp2c) @ (3223, 1132296)""" +nemo_dump_no_codestream = dump.format(textwrap.indent(nemo_xmp, ' ')) nemo_dump_no_codestream_no_xml = r"""JPEG 2000 Signature Box (jP ) @ (0, 12) Signature: 0d0a870a @@ -699,8 +535,8 @@ file7_rreq = r"""Reader Requirements Box (rreq) @ (44, 24) file1_xml = """XML Box (xml ) @ (36, 439) -\t -\t\t2001-11-01T13:45:00.000-06:00 -\t\tProfessional 120 Image -\t -""" + \t + \t\t2001-11-01T13:45:00.000-06:00 + \t\tProfessional 120 Image + \t + """ diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index 45b21df..923420b 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -631,6 +631,7 @@ class TestPrinting(unittest.TestCase): with patch('sys.stdout', new=StringIO()) as fake_out: print(j.box[2]) actual = fake_out.getvalue().strip() + self.maxDiff = None self.assertEqual(actual, fixtures.file1_xml) @unittest.skipIf(sys.hexversion < 0x03000000,