Ejemplo n.º 1
0
    def test_issue_290a(self):
        import sys
        from srsly.ruamel_yaml.compat import StringIO
        from srsly.ruamel_yaml import YAML

        yamldoc = dedent("""\
        ---
        aliases:
          # Folded-element comment
          # for a multi-line value
          - &FoldedEntry >
            THIS IS A
            FOLDED, MULTI-LINE
            VALUE

          # Literal-element comment
          # for a multi-line value
          - &literalEntry |
            THIS IS A
            LITERAL, MULTI-LINE
            VALUE

          # Plain-element comment
          - &plainEntry Plain entry
        """)

        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.explicit_start = True
        yaml.preserve_quotes = True
        yaml.width = sys.maxsize
        data = yaml.load(yamldoc)
        buf = StringIO()
        yaml.dump(data, buf)
        assert buf.getvalue() == yamldoc
Ejemplo n.º 2
0
    def test_issue_222(self):
        import srsly.ruamel_yaml
        from srsly.ruamel_yaml.compat import StringIO

        buf = StringIO()
        srsly.ruamel_yaml.safe_dump(["012923"], buf)
        assert buf.getvalue() == "['012923']\n"
Ejemplo n.º 3
0
    def test_issue_288a(self):
        import sys
        from srsly.ruamel_yaml.compat import StringIO
        from srsly.ruamel_yaml import YAML

        yamldoc = dedent("""\
        ---
        # Reusable values
        aliases:
          # First-element comment
          - &firstEntry First entry
          # Second-element comment
          - &secondEntry Second entry

          # Third-element comment is
           # a multi-line value
          - &thirdEntry Third entry

        # EOF Comment
        """)

        yaml = YAML()
        yaml.indent(mapping=2, sequence=4, offset=2)
        yaml.explicit_start = True
        yaml.preserve_quotes = True
        yaml.width = sys.maxsize
        data = yaml.load(yamldoc)
        buf = StringIO()
        yaml.dump(data, buf)
        assert buf.getvalue() == yamldoc
Ejemplo n.º 4
0
    def round_trip(self, input, output=None, yaml_version=None):
        from srsly.ruamel_yaml.compat import StringIO

        yaml, data = self.yaml_load(input.value, yaml_version=yaml_version)
        buf = StringIO()
        yaml.dump(data, buf)
        expected = input.value if output is None else output.value
        value = buf.getvalue()
        if PY2:
            value = value.decode("utf-8")
            print("value", value)
            # print('expected', expected)
        assert value == expected
Ejemplo n.º 5
0
    def test_issue_286(self):
        from srsly.ruamel_yaml import YAML
        from srsly.ruamel_yaml.compat import StringIO

        yaml = YAML()
        inp = dedent("""\
        parent_key:
        - sub_key: sub_value

        # xxx""")
        a = yaml.load(inp)
        a["new_key"] = "new_value"
        buf = StringIO()
        yaml.dump(a, buf)
        assert buf.getvalue().endswith("xxx\nnew_key: new_value\n")
Ejemplo n.º 6
0
    def test_issue_279(self):
        from srsly.ruamel_yaml import YAML
        from srsly.ruamel_yaml.compat import StringIO

        yaml = YAML()
        yaml.indent(sequence=4, offset=2)
        inp = dedent("""\
        experiments:
          - datasets:
        # ATLAS EWK
              - {dataset: ATLASWZRAP36PB, frac: 1.0}
              - {dataset: ATLASZHIGHMASS49FB, frac: 1.0}
        """)
        a = yaml.load(inp)
        buf = StringIO()
        yaml.dump(a, buf)
        print(buf.getvalue())
        assert buf.getvalue() == inp
Ejemplo n.º 7
0
def test_qualified_name01(tmpdir):
    """issue 214"""
    from srsly.ruamel_yaml import YAML
    import srsly.ruamel_yaml.comments
    from srsly.ruamel_yaml.compat import StringIO

    with pytest.raises(ValueError):
        yaml = YAML(typ="unsafe", pure=True)
        yaml.explicit_end = True
        buf = StringIO()
        yaml.dump(srsly.ruamel_yaml.comments.CommentedBase.yaml_anchor, buf)
        res = buf.getvalue()
        assert (
            res ==
            "!!python/name:srsly.ruamel_yaml.comments.CommentedBase.yaml_anchor ''\n...\n"
        )
        x = yaml.load(res)
        assert x == srsly.ruamel_yaml.comments.CommentedBase.yaml_anchor
Ejemplo n.º 8
0
    def test_issue_221_sort_key_reverse(self):
        from srsly.ruamel_yaml import YAML
        from srsly.ruamel_yaml.compat import StringIO

        yaml = YAML()
        inp = dedent("""\
        - four
        - One    # 1
        - Three  # 3
        - five   # 5
        - two    # 2
        """)
        a = yaml.load(dedent(inp))
        a.sort(key=str.lower, reverse=True)
        buf = StringIO()
        yaml.dump(a, buf)
        exp = dedent("""\
        - two    # 2
        - Three  # 3
        - One    # 1
        - four
        - five   # 5
        """)
        assert buf.getvalue() == exp
Ejemplo n.º 9
0
    def test_issue_221_sort_reverse(self):
        from srsly.ruamel_yaml import YAML
        from srsly.ruamel_yaml.compat import StringIO

        yaml = YAML()
        inp = dedent("""\
        - d
        - a  # 1
        - c  # 3
        - e  # 5
        - b  # 2
        """)
        a = yaml.load(dedent(inp))
        a.sort(reverse=True)
        buf = StringIO()
        yaml.dump(a, buf)
        exp = dedent("""\
        - e  # 5
        - d
        - c  # 3
        - b  # 2
        - a  # 1
        """)
        assert buf.getvalue() == exp
Ejemplo n.º 10
0
    def test_issue_160(self):
        from srsly.ruamel_yaml.compat import StringIO

        s = dedent("""\
        root:
            # a comment
            - {some_key: "value"}

        foo: 32
        bar: 32
        """)
        a = round_trip_load(s)
        del a["root"][0]["some_key"]
        buf = StringIO()
        round_trip_dump(a, buf, block_seq_indent=4)
        exp = dedent("""\
        root:
            # a comment
            - {}

        foo: 32
        bar: 32
        """)
        assert buf.getvalue() == exp
Ejemplo n.º 11
0
        def round_trip_all(self, stream, **kw):
            from srsly.ruamel_yaml.compat import StringIO, BytesIO  # NOQA

            assert isinstance(stream,
                              (srsly.ruamel_yaml.compat.text_type, str))
            lkw = kw.copy()
            if stream and stream[0] == "\n":
                stream = stream[1:]
            stream = textwrap.dedent(stream)
            data = list(srsly.ruamel_yaml.YAML.load_all(self, stream))
            outp = lkw.pop("outp", stream)
            lkw["stream"] = st = StringIO()
            srsly.ruamel_yaml.YAML.dump_all(self, data, **lkw)
            res = st.getvalue()
            if res != outp:
                diff(outp, res, "input string")
            assert res == outp
Ejemplo n.º 12
0
        def dump(self, data, **kw):
            from srsly.ruamel_yaml.compat import StringIO, BytesIO  # NOQA

            assert ("stream" in kw) ^ ("compare" in kw)
            if "stream" in kw:
                return srsly.ruamel_yaml.YAML.dump(data, **kw)
            lkw = kw.copy()
            expected = textwrap.dedent(lkw.pop("compare"))
            unordered_lines = lkw.pop("unordered_lines", False)
            if expected and expected[0] == "\n":
                expected = expected[1:]
            lkw["stream"] = st = StringIO()
            srsly.ruamel_yaml.YAML.dump(self, data, **lkw)
            res = st.getvalue()
            print(res)
            if unordered_lines:
                res = sorted(res.splitlines())
                expected = sorted(expected.splitlines())
            assert res == expected