From 53f46fa0d930b87e400b9906be4254d23792fc65 Mon Sep 17 00:00:00 2001 From: John Evans Date: Sun, 2 Mar 2014 11:17:44 -0500 Subject: [PATCH] Wrapping indentation for sake of 2.7. #177 Textwrap's indent method is not available on 2.7, so we have to fake it ourselves. --- glymur/jp2box.py | 34 ++++++++++++++++++++++++++++++---- glymur/test/fixtures.py | 34 ++++++++++++++++++++++++++++++---- glymur/test/test_printing.py | 4 +++- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 5a0febf..6db6c87 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -98,10 +98,36 @@ class Jp2kBox(object): for box in self.box: boxstr = str(box) # Indent the child boxes to make the association clear. - msg += '\n' + textwrap.indent(boxstr, ' ') + msg += '\n' + self._indent(boxstr) return msg + def _indent(self, textstr, indent_level=4): + """ + Indent a string. + + Textwrap's indent method only exists for 3.3 or above. In 2.7 we have + to fake it. + + Parameters + ---------- + textstring : str + String to be indented. + indent_level : str + Number of spaces of indentation to add. + + Returns + ------- + indented_string : str + Possibly multi-line string indented a certain bit. + """ + if sys.hexversion >= 0x03030000: + return textwrap.indent(textstr, ' ' * indent_level) + else: + lst = [(' ' * indent_level + x) for x in textstr.split('\n')] + return '\n'.join(lst) + + def _write_superbox(self, fptr): """Write a superbox. @@ -888,7 +914,7 @@ class ContiguousCodestreamBox(Jp2kBox): msg += '\n Main header:' for segment in self.main_header.segment: - msg += '\n' + textwrap.indent(str(segment), ' ') + msg += '\n' + self._indent(str(segment), indent_level=8) return msg @@ -2565,7 +2591,7 @@ class XMLBox(Jp2kBox): pretty_print=True).decode('utf-8') else: xmlstring = 'None' - msg += textwrap.indent(xmlstring, ' ') + msg += self._indent(xmlstring) return msg def write(self, fptr): @@ -2974,7 +3000,7 @@ class UUIDBox(Jp2kBox): encoding='utf-8', pretty_print=True).decode('utf-8') # indent it a bit - xmlstring = textwrap.indent(xmlstring.rstrip(), ' ') + xmlstring = self._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 fe0777e..666fb6b 100644 --- a/glymur/test/fixtures.py +++ b/glymur/test/fixtures.py @@ -45,6 +45,32 @@ except: raise +def _indent(textstr): + """ + Indent a string. + + Textwrap's indent method only exists for 3.3 or above. In 2.7 we have + to fake it. + + Parameters + ---------- + textstring : str + String to be indented. + indent_level : str + Number of spaces of indentation to add. + + Returns + ------- + indented_string : str + Possibly multi-line string indented a certain bit. + """ + if sys.hexversion >= 0x03030000: + return textwrap.indent(textstr, ' ') + else: + lst = [(' ' + x) for x in textstr.split('\n')] + return '\n'.join(lst) + + def opj_data_file(relative_file_name): """Compact way of forming a full filename from OpenJPEG's test suite.""" jfile = os.path.join(OPJ_DATA_ROOT, relative_file_name) @@ -270,12 +296,12 @@ nemo_xmp = """ nemo_xmp_box = """UUID Box (uuid) @ (77, 3146) UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP) UUID Data: -{0}""".format(textwrap.indent(nemo_xmp, ' ')) +{0}""".format(_indent(nemo_xmp)) nemo_xmp_box = """UUID Box (uuid) @ (77, 3146) UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP) UUID Data: -{0}""".format(textwrap.indent(nemo_xmp, ' ')) +{0}""".format(_indent(nemo_xmp)) SimpleRDF = """