Beispiel #1
0
    def test_issue_288a(self):
        import sys
        from io import StringIO

        from ruyaml 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
Beispiel #2
0
    def test_decorator_explicit(self):
        from ruamel.yaml import yaml_object

        yml = YAML()

        @yaml_object(yml)
        class User3(object):
            yaml_tag = u'!USER'

            def __init__(self, name, age):
                self.name = name
                self.age = age

            @classmethod
            def to_yaml(cls, representer, node):
                return representer.represent_scalar(
                    cls.yaml_tag, u'{.name}-{.age}'.format(node, node))

            @classmethod
            def from_yaml(cls, constructor, node):
                return cls(*node.value.split('-'))

        ys = """
        - !USER Anthon-18
        """
        d = yml.load(ys)
        yml.dump(d, compare=ys)
Beispiel #3
0
    def test_issue_221_sort_key_reverse(self):
        from io import StringIO

        from ruyaml import YAML

        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
Beispiel #4
0
    def test_issue_221_sort_reverse(self):
        from io import StringIO

        from ruyaml import YAML

        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
Beispiel #5
0
 def test_register_1_rt(self):
     yaml = YAML()
     yaml.register_class(User1)
     ys = """
     - !user Anthon-18
     """
     d = yaml.load(ys)
     yaml.dump(d, compare=ys)
Beispiel #6
0
 def test_register_1_unsafe(self):
     yaml = YAML(typ='unsafe')
     yaml.register_class(User1)
     ys = """
     [!user Anthon-18]
     """
     d = yaml.load(ys)
     yaml.dump(d, compare=ys)
Beispiel #7
0
 def test_register_0_unsafe(self):
     # default_flow_style = None
     yaml = YAML(typ='unsafe')
     yaml.register_class(User0)
     ys = """
     - !User0 {age: 18, name: Anthon}
     """
     d = yaml.load(ys)
     yaml.dump(d, compare=ys)
Beispiel #8
0
 def test_rt_non_root_literal_scalar(self):
     yaml = YAML()
     s = 'testing123'
     ys = """
     - |
       {}
     """
     ys = ys.format(s)
     d = yaml.load(ys)
     yaml.dump(d, compare=ys)
Beispiel #9
0
 def test_register_0_rt(self):
     yaml = YAML()
     yaml.register_class(User0)
     ys = """
     - !User0
       name: Anthon
       age: 18
     """
     d = yaml.load(ys)
     yaml.dump(d, compare=ys, unordered_lines=True)
Beispiel #10
0
 def test_rt_root_literal_scalar_no_indent_no_eol(self):
     yaml = YAML()
     yaml.explicit_start = True
     s = 'testing123'
     ys = """
     --- |-
     {}
     """
     ys = ys.format(s)
     d = yaml.load(ys)
     yaml.dump(d, compare=ys)
Beispiel #11
0
 def test_rt_root_plain_scalar_no_indent(self):
     yaml = YAML()
     yaml.explicit_start = True
     yaml.indent = 0
     s = 'testing123'
     ys = """
     ---
     {}
     """
     ys = ys.format(s)
     d = yaml.load(ys)
     yaml.dump(d, compare=ys)
Beispiel #12
0
    def test_issue_280(self):
        from collections import namedtuple
        from sys import stdout

        from ruyaml import YAML
        from ruyaml.representer import RepresenterError

        T = namedtuple('T', ('a', 'b'))
        t = T(1, 2)
        yaml = YAML()
        with pytest.raises(RepresenterError, match='cannot represent'):
            yaml.dump({'t': t}, stdout)
Beispiel #13
0
 def test_rt_root_sq_scalar_expl_indent(self):
     yaml = YAML()
     yaml.explicit_start = True
     yaml.indent = 4
     s = "'testing: 123'"
     ys = """
     ---
         {}
     """
     ys = ys.format(s)
     d = yaml.load(ys)
     yaml.dump(d, compare=ys)
Beispiel #14
0
 def test_rt_root_dq_scalar_expl_indent(self):
     # if yaml.indent is the default (None)
     # then write after the directive indicator
     yaml = YAML()
     yaml.explicit_start = True
     yaml.indent = 0
     s = '"\'testing123"'
     ys = """
     ---
     {}
     """
     ys = ys.format(s)
     d = yaml.load(ys)
     yaml.dump(d, compare=ys)
Beispiel #15
0
    def test_issue_286(self):
        from io import StringIO

        from ruyaml import YAML

        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')
Beispiel #16
0
    def test_decorator_implicit(self):
        from ruamel.yaml import yaml_object

        yml = YAML()

        @yaml_object(yml)
        class User2(object):
            def __init__(self, name, age):
                self.name = name
                self.age = age

        ys = """
        - !User2
          name: Anthon
          age: 18
        """
        d = yml.load(ys)
        yml.dump(d, compare=ys, unordered_lines=True)
Beispiel #17
0
    def test_issue_279(self):
        from io import StringIO

        from ruyaml import YAML

        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
Beispiel #18
0
    def test_issue_290a(self):
        import sys
        from io import StringIO

        from ruyaml 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
Beispiel #19
0
    def test_issue_221_sort(self):
        from ruamel.yaml import YAML
        from 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()
        buf = StringIO()
        yaml.dump(a, buf)
        exp = dedent("""\
        - a  # 1
        - b  # 2
        - c  # 3
        - d
        - e  # 5
        """)
        assert buf.getvalue() == exp
Beispiel #20
0
    def test_issue_221_sort_key(self):
        from ruamel.yaml import YAML
        from 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)
        buf = StringIO()
        yaml.dump(a, buf)
        exp = dedent("""\
        - five   # 5
        - four
        - One    # 1
        - Three  # 3
        - two    # 2
        """)
        assert buf.getvalue() == exp