Exemplo n.º 1
0
    def test_duplicate_keys_02(self):
        from ruyaml import YAML
        from ruyaml.constructor import DuplicateKeyError

        yaml = YAML(typ='safe')
        with pytest.raises(DuplicateKeyError):
            yaml.load('{a: 1, a: 2}')
Exemplo n.º 2
0
    def test_multi_load(self):
        # make sure reader, scanner, parser get reset
        from ruyaml import YAML

        yaml = YAML()
        yaml.load('a: 1')
        yaml.load('a: 1')  # did not work in 0.15.4
Exemplo n.º 3
0
    def test_issue_300a(self):
        import ruyaml

        inp = dedent("""
        %YAML 1.1
        %TAG ! tag:example.com,2019/path#fragment
        ---
        null
        """)
        yaml = YAML()
        with pytest.raises(ruyaml.scanner.ScannerError,
                           match='while scanning a directive'):
            yaml.load(inp)
Exemplo n.º 4
0
    def test_parse(self):
        # ensure `parse` method is functional and can parse "unsafe" yaml
        from ruyaml import YAML
        from ruyaml.constructor import ConstructorError

        yaml = YAML(typ='safe')
        s = '- !User0 {age: 18, name: Anthon}'
        # should fail to load
        with pytest.raises(ConstructorError):
            yaml.load(s)
        # should parse fine
        yaml = YAML(typ='safe')
        for _ in yaml.parse(s):
            pass
Exemplo n.º 5
0
    def test_root_literal_scalar_no_indent_1_1_no_raise(self):
        # from ruyaml.parser import ParserError

        yaml = YAML()
        yaml.root_level_block_style_scalar_no_indent_error_1_1 = True
        s = 'testing123'
        # with pytest.raises(ParserError):
        if True:
            inp = """
            %YAML 1.1
            --- |
            {}
            """
            yaml.load(inp.format(s))
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 9
0
    def test_issue_176(self):
        # basic request by Stuart Berg
        from ruyaml import YAML

        yaml = YAML()
        seq = yaml.load('[1,2,3]')
        seq[:] = [1, 2, 3, 4]
Exemplo n.º 10
0
    def test_dup_merge_fail(self):
        from ruyaml import YAML
        from ruyaml.constructor import DuplicateKeyError

        yaml = YAML()
        yaml.allow_duplicate_keys = False
        with pytest.raises(DuplicateKeyError):
            yaml.load("""\
            foo: &f
              a: a
            foo2: &g
              b: b
            all:
              <<: *f
              <<: *g
            """)
Exemplo n.º 11
0
def get_supporting(api: str, destination: str):
    response: dict = json.load(request.urlopen(api))
    if os.path.exists(f'{destination}/metadata.yml'):
        with open(f'{destination}/metadata.yml', 'r') as fd:
            yaml = YAML(
                typ='safe')  # default, if not specfied, is 'rt' (round-trip)
            metadata = yaml.load(fd)
            version = metadata['version']
            if version == response['tag_name']:
                log(f'Latest {destination} already installed')
                return
    tar_url = response['tarball_url']

    os.system(f'rm -rf ./{destination}')

    log(f'Updating supporting {destination} v{response["tag_name"]} from {tar_url} ...'
        )
    tar_bytes = request.urlopen(tar_url).read()

    fname = 'tmp.tar.gz'
    with open(fname, 'wb') as f:
        f.write(tar_bytes)

    tar = tarfile.open(fname)
    extracted_name = tar.members[0].name

    log(f'Extracting into {os.path.expandvars(dragondir + destination)}')
    tar.extractall()
    os.rename(extracted_name, destination)
    os.remove(fname)
Exemplo n.º 12
0
def read_custom_mappings():
    custom_mappings = {}
    if not os.path.isfile(MAPPING_FILE):
        logger.info(f"[MAPPING] Custom map file not found: {MAPPING_FILE}")
    else:
        logger.info(f"[MAPPING] Custom map file found: {MAPPING_FILE}")
        file = open(MAPPING_FILE, "r", encoding="utf-8")
        yaml = YAML(typ='safe')
        file_mappings = yaml.load(file)

        for file_entry in file_mappings['entries']:
            series_title = str(file_entry['title']).lower()
            series_mappings: List[AnilistCustomMapping] = []
            for file_season in file_entry['seasons']:
                season = file_season['season']
                anilist_id = file_season['anilist-id']
                start = 1
                if 'start' in file_season:
                    start = file_season['start']

                logger.info(
                    f"[MAPPING] Adding custom mapping | title: {file_entry['title']} | season: {season} | anilist id: {anilist_id} | start: {start}"
                )
                series_mappings.append(
                    AnilistCustomMapping(season, anilist_id, start))

            custom_mappings[series_title] = series_mappings
    return custom_mappings
Exemplo n.º 13
0
    def test_issue_233a(self):
        import json

        from ruyaml import YAML

        yaml = YAML()
        data = yaml.load('[]')
        json_str = json.dumps(data)  # NOQA
Exemplo n.º 14
0
    def test_duplicate_key_01(self):
        # so issue https://stackoverflow.com/a/52852106/1307905
        from ruyaml.constructor import DuplicateKeyError

        s = dedent("""\
        - &name-name
          a: 1
        - &help-name
          b: 2
        - <<: *name-name
          <<: *help-name
        """)
        with pytest.raises(DuplicateKeyError):
            yaml = YAML(typ='safe')
            yaml.load(s)
        with pytest.raises(DuplicateKeyError):
            yaml = YAML()
            yaml.load(s)
Exemplo n.º 15
0
    def test_read_unicode(self, tmpdir):
        from ruyaml import YAML

        yaml = YAML()
        file_name = str(tmpdir) + '/tstFile.yaml'
        with open(file_name, 'wb') as fp:
            fp.write('text: HELLO_WORLD©\n'.encode('utf-8'))
        text_dict = yaml.load(open(file_name, 'r'))
        assert text_dict['text'] == 'HELLO_WORLD©'
Exemplo n.º 16
0
    def test_dupl_set_00(self):
        # round-trip-loader should except
        from ruyaml import YAML
        from ruyaml.constructor import DuplicateKeyError

        yaml = YAML()
        with pytest.raises(DuplicateKeyError):
            yaml.load(
                textwrap.dedent(
                    """\
                !!set
                ? a
                ? b
                ? c
                ? a
                """
                )
            )
Exemplo n.º 17
0
 def test_root_folding_scalar_no_indent(self):
     yaml = YAML()
     s = 'testing123'
     inp = """
     --- >
     {}
     """
     d = yaml.load(inp.format(s))
     print(d)
     assert d == s + '\n'
Exemplo n.º 18
0
 def test_root_folding_scalar_no_indent_special(self):
     yaml = YAML()
     s = '%!PS-Adobe-2.0'
     inp = """
     --- >
     {}
     """
     d = yaml.load(inp.format(s))
     print(d)
     assert d == s + '\n'
Exemplo n.º 19
0
 def test_root_literal_scalar_indent_offset_one(self):
     yaml = YAML()
     s = 'testing123'
     inp = """
     --- |1
      {}
     """
     d = yaml.load(inp.format(s))
     print(d)
     assert d == s + '\n'
Exemplo n.º 20
0
 def test_root_literal_scalar_indent_example_9_5(self):
     yaml = YAML()
     s = '%!PS-Adobe-2.0'
     inp = """
     --- |
       {}
     """
     d = yaml.load(inp.format(s))
     print(d)
     assert d == s + '\n'
Exemplo n.º 21
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)
Exemplo n.º 22
0
 def test_root_literal_scalar_indent_offset_two_leading_space(self):
     yaml = YAML()
     s = ' testing123'
     inp = """
     --- |4
         {s}
         {s}
     """
     d = yaml.load(inp.format(s=s))
     print(d)
     assert d == (s + '\n') * 2
Exemplo n.º 23
0
 def test_root_literal_scalar_no_indent_1_1(self):
     yaml = YAML()
     s = 'testing123'
     inp = """
     %YAML 1.1
     --- |
     {}
     """
     d = yaml.load(inp.format(s))
     print(d)
     assert d == s + '\n'
Exemplo n.º 24
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)
Exemplo n.º 25
0
 def test_root_literal_doc_indent_marker(self):
     yaml = YAML()
     yaml.explicit_start = True
     inp = """
     --- |2
        some more
       text
     """
     d = yaml.load(inp)
     print(type(d), repr(d))
     yaml.round_trip(inp)
Exemplo n.º 26
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)
Exemplo n.º 27
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)
Exemplo n.º 28
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)
Exemplo n.º 29
0
    def test_root_literal_scalar_no_indent_1_1_old_style(self):
        from textwrap import dedent

        from ruyaml import YAML

        yaml = YAML(typ='safe', pure=True)
        s = 'testing123'
        inp = """
        %YAML 1.1
        --- |
          {}
        """
        d = yaml.load(dedent(inp.format(s)))
        print(d)
        assert d == s + '\n'
Exemplo n.º 30
0
def test_qualified_name01(tmpdir):
    """issue 214"""
    from io import StringIO

    import ruyaml.comments
    from ruyaml import YAML

    yaml = YAML(typ='unsafe', pure=True)
    yaml.explicit_end = True
    buf = StringIO()
    yaml.dump(ruyaml.comments.CommentedBase.yaml_anchor, buf)
    res = buf.getvalue()
    assert res == "!!python/name:ruyaml.comments.CommentedBase.yaml_anchor ''\n...\n"
    x = yaml.load(res)
    assert x == ruyaml.comments.CommentedBase.yaml_anchor