Exemple #1
0
 def test_writelines(self):
     OUT1 = StringIO()
     OUT2 = StreamIndenter(OUT1)
     OUT2.writelines(['Hello?\n', '\n', 'Text\n', '\n', 'Hello, world!'])
     self.assertEqual('    Hello?\n\n    Text\n\n    Hello, world!',
                      OUT2.getvalue())
Exemple #2
0
 def test_blank_lines(self):
     OUT1 = StringIO()
     OUT2 = StreamIndenter(OUT1)
     OUT2.write('Hello?\n\nText\n\nHello, world!')
     self.assertEqual('    Hello?\n\n    Text\n\n    Hello, world!',
                      OUT2.getvalue())
Exemple #3
0
 def test_noprefix(self):
     OUT1 = StringIO()
     OUT2 = StreamIndenter(OUT1)
     OUT2.write('Hello?\nHello, world!')
     self.assertEqual('    Hello?\n    Hello, world!', OUT2.getvalue())
Exemple #4
0
 def test_prefix(self):
     prefix = 'foo:'
     OUT1 = StringIO()
     OUT2 = StreamIndenter(OUT1, prefix)
     OUT2.write('Hello?\nHello, world!')
     self.assertEqual('foo:Hello?\nfoo:Hello, world!', OUT2.getvalue())
Exemple #5
0
    def _pprint_base_impl(self, ostream, verbose, prefix, _name, _doc,
                          _constructed, _attr, _data, _header, _fcn):
        if ostream is None:
            ostream = sys.stdout
        if prefix:
            ostream = StreamIndenter(ostream, prefix)

        # FIXME: HACK for backwards compatability with suppressing the
        # header for the top block
        if not _attr and self.parent_block() is None:
            _name = ''

        # We only indent everything if we printed the header
        if _attr or _name or _doc:
            ostream = StreamIndenter(ostream, self._PPRINT_INDENT)
            # The first line should be a hanging indent (i.e., not indented)
            ostream.newline = False

        if self.is_reference():
            _attr = list(_attr) if _attr else []
            _attr.append(('ReferenceTo', self.referent))

        if _name:
            ostream.write(_name + " : ")
        if _doc:
            ostream.write(_doc + '\n')
        if _attr:
            ostream.write(", ".join("%s=%s" % (k, v) for k, v in _attr))
        if _attr or _name or _doc:
            ostream.write("\n")

        if not _constructed:
            # HACK: for backwards compatability, Abstract blocks will
            # still print their assigned components.  Should we instead
            # always pprint unconstructed components (possibly
            # suppressing the table header if the table is empty)?
            if self.parent_block() is not None:
                ostream.write("Not constructed\n")
                return

        if type(_fcn) is tuple:
            _fcn, _fcn2 = _fcn
        else:
            _fcn2 = None

        if _header is not None:
            if _fcn2 is not None:
                _data_dict = dict(_data)
                _data = _data_dict.items()
            tabular_writer(ostream, '', _data, _header, _fcn)
            if _fcn2 is not None:
                for _key in sorted_robust(_data_dict):
                    _fcn2(ostream, _key, _data_dict[_key])
        elif _fcn is not None:
            _data_dict = dict(_data)
            for _key in sorted_robust(_data_dict):
                _fcn(ostream, _key, _data_dict[_key])
        elif _data is not None:
            ostream.write(_data)