コード例 #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
コード例 #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"
コード例 #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
コード例 #4
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
コード例 #5
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
コード例 #6
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")
コード例 #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
コード例 #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
コード例 #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
コード例 #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