Beispiel #1
0
def test_constructor_types(data_filename, code_filename, verbose=False):
    _make_objects()
    native1 = None
    native2 = None
    yaml = ruyaml.YAML(typ='safe', pure=True)
    yaml.loader = MyLoader
    try:
        with open(data_filename, 'rb') as fp0:
            native1 = list(ruyaml.load_all(fp0))
        if len(native1) == 1:
            native1 = native1[0]
        with open(code_filename, 'rb') as fp0:
            native2 = _load_code(fp0.read())
        try:
            if native1 == native2:
                return
        except TypeError:
            pass
        # print('native1', native1)
        if verbose:
            print('SERIALIZED NATIVE1:')
            print(_serialize_value(native1))
            print('SERIALIZED NATIVE2:')
            print(_serialize_value(native2))
        assert _serialize_value(native1) == _serialize_value(native2), (
            native1,
            native2,
        )
    finally:
        if verbose:
            print('NATIVE1:')
            pprint.pprint(native1)
            print('NATIVE2:')
            pprint.pprint(native2)
Beispiel #2
0
    def __init__(self, path_to_graph_yml: Optional[Path], read: bool = True):
        self._yaml = ruyaml.YAML()
        self._path_to_graph_yml = path_to_graph_yml
        self._yaml.indent(mapping=2, sequence=4, offset=2)
        # read text manually instead of loading the Path directly to normalize line
        # breaks. Ruyaml opens files in binary mode (bypassing universal newline
        # support), then proceeds to behave incorrectly in the presence of \r\n, adding
        # extra line breaks in the output.
        if read:
            with self._path_to_graph_yml.open() as f:
                text = f.read()
            self._cfg = self._yaml.load(text) or {}
            # ruyaml doesn't provide a way to preserve indentation,
            # so pick a value that matches the least-indented list item we see.
            matches = (
                len(m.group(1)) for m in re.finditer(r"^( *)-", text, re.MULTILINE)
            )
            # ruyaml's indent number includes the length  of "- " for some reason
            indent = min(matches, default=2) + 2
        else:
            self._cfg = {}
            indent = 4

        self._yaml.indent(
            mapping=int(indent / 2), sequence=indent, offset=max(0, indent - 2)
        )
Beispiel #3
0
def test_recursive(recursive_filename, verbose=False):
    yaml = ruyaml.YAML(typ='safe', pure=True)
    context = globals().copy()
    with open(recursive_filename, 'rb') as fp0:
        exec(fp0.read(), context)
    value1 = context['value']
    output1 = None
    value2 = None
    output2 = None
    try:
        buf = ruyaml.compat.StringIO()
        output1 = yaml.dump(value1, buf)
        yaml.load(output1)
        value2 = buf.getvalue()
        buf = ruyaml.compat.StringIO()
        yaml.dump(value2, buf)
        output2 = buf.getvalue()
        assert output1 == output2, (output1, output2)
    finally:
        if verbose:
            print('VALUE1:', value1)
            print('VALUE2:', value2)
            print('OUTPUT1:')
            print(output1)
            print('OUTPUT2:')
            print(output2)
Beispiel #4
0
def round_trip_dump_all(
    data,
    stream=None,  # *,
    indent=None,
    block_seq_indent=None,
    default_flow_style=unset,
    top_level_colon_align=None,
    prefix_colon=None,
    explicit_start=None,
    explicit_end=None,
    version=None,
    allow_unicode=None,
):
    yaml = ruyaml.YAML()
    yaml.indent(mapping=indent, sequence=indent, offset=block_seq_indent)
    if default_flow_style is not unset:
        yaml.default_flow_style = default_flow_style
    yaml.top_level_colon_align = top_level_colon_align
    yaml.prefix_colon = prefix_colon
    yaml.explicit_start = explicit_start
    yaml.explicit_end = explicit_end
    yaml.version = version
    yaml.allow_unicode = allow_unicode
    if stream is not None:
        yaml.dump(data, stream=stream)
        return
    buf = io.StringIO()
    yaml.dump_all(data, stream=buf)
    return buf.getvalue()
Beispiel #5
0
def test_dice_constructor_with_loader():
    import ruyaml  # NOQA

    yaml = ruyaml.YAML(typ='unsafe', pure=True)
    ruyaml.add_constructor('!dice', dice_constructor, Loader=ruyaml.Loader)
    data = yaml.load('initial hit points: !dice 8d4')
    assert str(data) == "{'initial hit points': Dice(8,4)}"
Beispiel #6
0
    def test_issue_130a(self):
        # issue 130 reported by Devid Fee
        import ruyaml

        ys = dedent("""\
        components:
          server: &server_component
            type: spark.server:ServerComponent
            host: 0.0.0.0
            port: 8000
          shell: &shell_component
            type: spark.shell:ShellComponent

        services:
          server: &server_service
            <<: *server_component
            port: 4000
          shell: &shell_service
            <<: *shell_component
            components:
              server: {<<: *server_service}
        """)
        yaml = ruyaml.YAML(typ='safe', pure=True)
        data = yaml.load(ys)
        assert data['services']['shell']['components']['server'][
            'port'] == 4000
Beispiel #7
0
def round_trip_load_all(inp, preserve_quotes=None, version=None):
    import ruyaml  # NOQA

    dinp = dedent(inp)
    yaml = ruyaml.YAML()
    yaml.preserve_quotes = preserve_quotes
    yaml.version = version
    return yaml.load_all(dinp)
Beispiel #8
0
def yload(stream, multi=False):
    """
    Load one or more YAML objects from a file.
    """
    y = yaml.YAML(typ="safe")
    if multi:
        return y.load_all(stream)
    else:
        return y.load(stream)
Beispiel #9
0
    def test_issue_232(self):
        import ruyaml

        yaml = ruyaml.YAML(typ='safe', pure=True)

        with pytest.raises(ruyaml.parser.ParserError):
            yaml.load(']')
        with pytest.raises(ruyaml.parser.ParserError):
            yaml.load('{]')
Beispiel #10
0
    def test_issue_222(self):
        from io import StringIO

        import ruyaml

        yaml = ruyaml.YAML(typ='safe')
        buf = StringIO()
        yaml.dump(['012923'], buf)
        assert buf.getvalue() == "['012923']\n"
Beispiel #11
0
def _compare_scanners(py_data, c_data, verbose):
    yaml = ruyaml.YAML(typ='unsafe', pure=True)
    py_tokens = list(yaml.scan(py_data, Loader=ruyaml.PyLoader))
    c_tokens = []
    try:
        yaml = ruyaml.YAML(typ='unsafe', pure=False)
        for token in yaml.scan(c_data, Loader=ruyaml.CLoader):
            c_tokens.append(token)
        assert len(py_tokens) == len(c_tokens), (len(py_tokens), len(c_tokens))
        for py_token, c_token in zip(py_tokens, c_tokens):
            assert py_token.__class__ == c_token.__class__, (py_token, c_token)
            if hasattr(py_token, 'value'):
                assert py_token.value == c_token.value, (py_token, c_token)
            if isinstance(py_token, ruyaml.StreamEndToken):
                continue
            py_start = (
                py_token.start_mark.index,
                py_token.start_mark.line,
                py_token.start_mark.column,
            )
            py_end = (
                py_token.end_mark.index,
                py_token.end_mark.line,
                py_token.end_mark.column,
            )
            c_start = (
                c_token.start_mark.index,
                c_token.start_mark.line,
                c_token.start_mark.column,
            )
            c_end = (
                c_token.end_mark.index,
                c_token.end_mark.line,
                c_token.end_mark.column,
            )
            assert py_start == c_start, (py_start, c_start)
            assert py_end == c_end, (py_end, c_end)
    finally:
        if verbose:
            print('PY_TOKENS:')
            pprint.pprint(py_tokens)
            print('C_TOKENS:')
            pprint.pprint(c_tokens)
Beispiel #12
0
def test_load_cyaml():
    print("???????????????????????", platform.python_implementation())
    import ruyaml

    if sys.version_info >= NO_CLIB_VER:
        return
    yaml = ruyaml.YAML(typ='safe', pure=False)
    assert ruyaml.__with_libyaml__

    yaml.load('abc: 1')
Beispiel #13
0
    def test_issue_284(self):
        import ruyaml

        inp = dedent("""\
        plain key: in-line value
        : # Both empty
        "quoted key":
        - entry
        """)
        yaml = ruyaml.YAML(typ='rt')
        yaml.version = (1, 2)
        d = yaml.load(inp)
        assert d[None] is None

        yaml = ruyaml.YAML(typ='rt')
        yaml.version = (1, 1)
        with pytest.raises(ruyaml.parser.ParserError,
                           match='expected <block end>'):
            d = yaml.load(inp)
Beispiel #14
0
def test_dice_representer():
    import ruyaml  # NOQA

    yaml = ruyaml.YAML(typ='unsafe', pure=True)
    yaml.default_flow_style = False
    ruyaml.add_representer(Dice, dice_representer)
    # ruyaml 0.15.8+ no longer forces quotes tagged scalars
    buf = ruyaml.compat.StringIO()
    yaml.dump(dict(gold=Dice(10, 6)), buf)
    assert buf.getvalue() == 'gold: !dice 10d6\n'
Beispiel #15
0
def test_yaml_obj():
    import ruyaml  # NOQA

    yaml = ruyaml.YAML(typ='unsafe', pure=True)
    ruyaml.add_representer(Obj1, YAMLObj1.to_yaml)
    ruyaml.add_multi_constructor(YAMLObj1.yaml_tag, YAMLObj1.from_yaml)
    x = yaml.load('!obj:x.2\na: 1')
    print(x)
    buf = ruyaml.compat.StringIO()
    yaml.dump(x, buf)
    assert buf.getvalue() == """!obj:x.2 "{'a': 1}"\n"""
Beispiel #16
0
def test_dice_implicit_resolver():
    import ruyaml  # NOQA

    yaml = ruyaml.YAML(typ='unsafe', pure=True)
    yaml.default_flow_style = False
    pattern = re.compile(r'^\d+d\d+$')
    ruyaml.add_implicit_resolver('!dice', pattern)
    buf = ruyaml.compat.StringIO()
    yaml.dump(dict(treasure=Dice(10, 20)), buf)
    assert buf.getvalue() == 'treasure: 10d20\n'
    assert yaml.load('damage: 5d10') == dict(damage=Dice(5, 10))
Beispiel #17
0
def test_scanner(data_filename, canonical_filename, verbose=False):
    for filename in [data_filename, canonical_filename]:
        tokens = []
        try:
            yaml = ruyaml.YAML(typ='unsafe', pure=False)
            with open(filename, 'rb') as fp:
                for token in yaml.scan(fp):
                    tokens.append(token.__class__.__name__)
        finally:
            if verbose:
                pprint.pprint(tokens)
Beispiel #18
0
def load(s, typ=float):
    import ruyaml

    yaml = ruyaml.YAML()
    x = '{"low": %s }' % (s)
    print('input: [%s]' % (s), repr(x))
    # just to check it is loadable json
    res = json.loads(x)
    assert isinstance(res['low'], typ)
    ret_val = yaml.load(x)
    print(ret_val)
    return ret_val['low']
Beispiel #19
0
def test_dump_cyaml():
    import ruyaml

    if sys.version_info >= NO_CLIB_VER:
        return
    data = {'a': 1, 'b': 2}
    yaml = ruyaml.YAML(typ='safe', pure=False)
    yaml.default_flow_style = False
    yaml.allow_unicode = True
    buf = ruyaml.compat.StringIO()
    yaml.dump(data, buf)
    assert buf.getvalue() == 'a: 1\nb: 2\n'
Beispiel #20
0
def test_load_cyaml_1_2():
    # issue 155
    import ruyaml

    if sys.version_info >= NO_CLIB_VER:
        return
    assert ruyaml.__with_libyaml__
    inp = dedent("""\
    %YAML 1.2
    ---
    num_epochs: 70000
    """)
    yaml = ruyaml.YAML(typ='safe')
    yaml.load(inp)
Beispiel #21
0
    def test_set_out(self):
        # preferable would be the shorter format without the ': null'
        import ruyaml  # NOQA

        x = set(['a', 'b', 'c'])
        # cannot use round_trip_dump, it doesn't show null in block style
        buf = io.StringIO()
        yaml = ruyaml.YAML(typ='unsafe', pure=True)
        yaml.default_flow_style = False
        yaml.dump(x, buf)
        assert buf.getvalue() == dedent("""
        !!set
        a: null
        b: null
        c: null
        """)
Beispiel #22
0
def yprint(data, stream=sys.stdout, compact=False):
    """
    Write a YAML record.

    :param data: The data to write.
    :param stream: the file to write to, defaults to stdout.
    :param compact: Write single lines if possible, default False.
    """
    if isinstance(data, (int, float)):
        print(data, file=stream)
    elif isinstance(data, (str, bytes)):
        print(repr(data), file=stream)
    #   elif isinstance(data, bytes):
    #       os.write(sys.stdout.fileno(), data)
    else:
        y = yaml.YAML(typ="safe")
        y.default_flow_style = compact
        y.dump(data, stream=stream)
Beispiel #23
0
def test_constructor(data_filename, canonical_filename, verbose=False):
    _make_loader()
    _make_canonical_loader()
    native1 = None
    native2 = None
    yaml = ruyaml.YAML(typ='safe')
    try:
        with open(data_filename, 'rb') as fp0:
            native1 = list(yaml.load(fp0, Loader=MyLoader))
        with open(canonical_filename, 'rb') as fp0:
            native2 = list(yaml.load(fp0, Loader=MyCanonicalLoader))
        assert native1 == native2, (native1, native2)
    finally:
        if verbose:
            print('NATIVE1:')
            pprint.pprint(native1)
            print('NATIVE2:')
            pprint.pprint(native2)
Beispiel #24
0
def test_issue_127():
    import ruyaml  # NOQA

    class Ref(ruyaml.YAMLObject):
        yaml_constructor = ruyaml.RoundTripConstructor
        yaml_representer = ruyaml.RoundTripRepresenter
        yaml_tag = '!Ref'

        def __init__(self, logical_id):
            self.logical_id = logical_id

        @classmethod
        def from_yaml(cls, loader, node):
            return cls(loader.construct_scalar(node))

        @classmethod
        def to_yaml(cls, dumper, data):
            if isinstance(data.logical_id, ruyaml.scalarstring.ScalarString):
                style = data.logical_id.style  # ruyaml>0.15.8
            else:
                style = None
            return dumper.represent_scalar(cls.yaml_tag,
                                           data.logical_id,
                                           style=style)

    document = dedent("""\
    AList:
      - !Ref One
      - !Ref 'Two'
      - !Ref
        Two and a half
    BList: [!Ref Three, !Ref "Four"]
    CList:
      - Five Six
      - 'Seven Eight'
    """)
    yaml = ruyaml.YAML()
    yaml.preserve_quotes = True
    yaml.default_flow_style = None
    yaml.indent(sequence=4, offset=2)
    data = yaml.load(document)
    buf = ruyaml.compat.StringIO()
    yaml.dump(data, buf)
    assert buf.getvalue() == document.replace('\n    Two and', ' Two and')
Beispiel #25
0
def test_tokens(data_filename, tokens_filename, verbose=False):
    tokens1 = []
    with open(tokens_filename, 'r') as fp:
        tokens2 = fp.read().split()
    try:
        yaml = ruyaml.YAML(typ='unsafe', pure=True)
        with open(data_filename, 'rb') as fp1:
            for token in yaml.scan(fp1):
                if not isinstance(
                        token,
                    (ruyaml.StreamStartToken, ruyaml.StreamEndToken)):
                    tokens1.append(_replaces[token.__class__])
    finally:
        if verbose:
            print('TOKENS1:', ' '.join(tokens1))
            print('TOKENS2:', ' '.join(tokens2))
    assert len(tokens1) == len(tokens2), (tokens1, tokens2)
    for token1, token2 in zip(tokens1, tokens2):
        assert token1 == token2, (token1, token2)
Beispiel #26
0
    def test_load_all_perserve_quotes(self):
        import ruyaml  # NOQA

        yaml = ruyaml.YAML()
        yaml.preserve_quotes = True
        s = dedent("""\
        a: 'hello'
        ---
        b: "goodbye"
        """)
        data = []
        for x in yaml.load_all(s):
            data.append(x)
        buf = ruyaml.compat.StringIO()
        yaml.dump_all(data, buf)
        out = buf.getvalue()
        print(type(data[0]['a']), data[0]['a'])
        # out = ruyaml.round_trip_dump_all(data)
        print(out)
        assert out == s
Beispiel #27
0
def test_dump_cyaml_1_2():
    # issue 155
    from io import StringIO

    import ruyaml

    if sys.version_info >= NO_CLIB_VER:
        return
    assert ruyaml.__with_libyaml__
    yaml = ruyaml.YAML(typ='safe')
    yaml.version = (1, 2)
    yaml.default_flow_style = False
    data = {'a': 1, 'b': 2}
    exp = dedent("""\
    %YAML 1.2
    ---
    a: 1
    b: 2
    """)
    buf = StringIO()
    yaml.dump(data, buf)
    assert buf.getvalue() == exp
Beispiel #28
0
def spec_data():
    """Read role_spec.yaml and parse it to structure"""
    with open(os.path.join(os.path.dirname(__file__), "role_spec.yaml"),
              encoding="utf-8") as f:
        return ruyaml.YAML().load(f)
Beispiel #29
0
    def test_issue_223(self):
        import ruyaml

        yaml = ruyaml.YAML(typ='safe')
        yaml.load('phone: 0123456789')
Beispiel #30
0
def load(s, version=None):
    import ruyaml  # NOQA

    yaml = ruyaml.YAML()
    yaml.version = version
    return yaml.load(dedent(s))