示例#1
0
def test_file_output(unicode_filename, verbose=False):
    with open(unicode_filename, 'rb') as fp:
        data = fp.read().decode('utf-8')
    handle, filename = tempfile.mkstemp()
    os.close(handle)
    try:
        stream = StringIO()
        yaml.dump(data, stream, allow_unicode=True)
        data1 = stream.getvalue()
        stream = BytesIO()
        yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True)
        data2 = stream.getvalue().decode('utf-16-le')[1:]
        with open(filename, 'w', encoding='utf-16-le') as stream:
            yaml.dump(data, stream, allow_unicode=True)
        with open(filename, 'r', encoding='utf-16-le') as fp0:
            data3 = fp0.read()
        with open(filename, 'wb') as stream:
            yaml.dump(data, stream, encoding='utf-8', allow_unicode=True)
        with open(filename, 'r', encoding='utf-8') as fp0:
            data4 = fp0.read()
        assert data1 == data2, (data1, data2)
        assert data1 == data3, (data1, data3)
        assert data1 == data4, (data1, data4)
    finally:
        if os.path.exists(filename):
            os.unlink(filename)
示例#2
0
def test_unicode_output(unicode_filename, verbose=False):
    yaml = YAML(typ='safe', pure=True)
    with open(unicode_filename, 'rb') as fp:
        data = fp.read().decode('utf-8')
    value = ' '.join(data.split())
    for allow_unicode in [False, True]:
        data1 = yaml.dump(value, allow_unicode=allow_unicode)
        for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']:
            stream = StringIO()
            yaml.dump(value,
                      stream,
                      encoding=encoding,
                      allow_unicode=allow_unicode)
            data2 = stream.getvalue()
            data3 = yaml.dump(value,
                              encoding=encoding,
                              allow_unicode=allow_unicode)
            if encoding is not None:
                assert isinstance(data3, bytes)
                data3 = data3.decode(encoding)
            stream = BytesIO()
            if encoding is None:
                try:
                    yaml.dump(value,
                              stream,
                              encoding=encoding,
                              allow_unicode=allow_unicode)
                except TypeError as exc:
                    if verbose:
                        print(exc)
                    data4 = None
                else:
                    raise AssertionError('expected an exception')
            else:
                yaml.dump(value,
                          stream,
                          encoding=encoding,
                          allow_unicode=allow_unicode)
                data4 = stream.getvalue()
                if verbose:
                    print('BYTES:', data4[:50])
                data4 = data4.decode(encoding)
            for copy in [data1, data2, data3, data4]:
                if copy is None:
                    continue
                assert isinstance(copy, str)
                if allow_unicode:
                    try:
                        copy[4:].encode('ascii')
                    except UnicodeEncodeError as exc:
                        if verbose:
                            print(exc)
                    else:
                        raise AssertionError('expected an exception')
                else:
                    copy[4:].encode('ascii')
            assert isinstance(data1, str), (type(data1), encoding)
            assert isinstance(data2, str), (type(data2), encoding)
示例#3
0
def test_dice_representer():
    import ruyaml  # NOQA

    ruyaml.add_representer(Dice, dice_representer)
    # ruyaml 0.15.8+ no longer forces quotes tagged scalars
    assert (ruyaml.dump(dict(gold=Dice(10, 6)),
                        default_flow_style=False) == 'gold: !dice 10d6\n')
示例#4
0
def test_roundtrip_data(code_filename, roundtrip_filename, verbose=False):
    _make_objects()
    with open(code_filename, 'rb') as fp0:
        value1 = fp0.read()
    native2 = list(ruyaml.load_all(value1, Loader=MyLoader))
    if len(native2) == 1:
        native2 = native2[0]
    try:
        value2 = ruyaml.dump(
            native2,
            Dumper=MyDumper,
            default_flow_style=False,
            allow_unicode=True,
            encoding='utf-8',
        )
        # value2 += x
        if verbose:
            print('SERIALIZED NATIVE1:')
            print(value1)
            print('SERIALIZED NATIVE2:')
            print(value2)
        assert value1 == value2, (value1, value2)
    finally:
        if verbose:
            print('NATIVE2:')
            pprint.pprint(native2)
示例#5
0
def rt(s):
    import ruyaml

    res = ruyaml.dump(
        ruyaml.load(s, Loader=ruyaml.RoundTripLoader),
        Dumper=ruyaml.RoundTripDumper,
    )
    return res.strip() + '\n'
示例#6
0
def test_yaml_obj():
    import ruyaml  # NOQA

    ruyaml.add_representer(Obj1, YAMLObj1.to_yaml)
    ruyaml.add_multi_constructor(YAMLObj1.yaml_tag, YAMLObj1.from_yaml)
    x = ruyaml.load('!obj:x.2\na: 1', Loader=ruyaml.Loader)
    print(x)
    assert ruyaml.dump(x) == """!obj:x.2 "{'a': 1}"\n"""
示例#7
0
def test_dice_implicit_resolver():
    import ruyaml  # NOQA

    pattern = re.compile(r'^\d+d\d+$')
    ruyaml.add_implicit_resolver(u'!dice', pattern)
    assert (ruyaml.dump(dict(treasure=Dice(10, 20)),
                        default_flow_style=False) == 'treasure: 10d20\n')
    assert ruyaml.load('damage: 5d10',
                       Loader=ruyaml.Loader) == dict(damage=Dice(5, 10))
示例#8
0
    def test_roundtrip_flow_mapping(self):
        import ruyaml

        s = dedent("""\
        - {a: 1, b: hallo}
        - {j: fka, k: 42}
        """)
        data = ruyaml.load(s, Loader=ruyaml.RoundTripLoader)
        output = ruyaml.dump(data, Dumper=ruyaml.RoundTripDumper)
        assert s == output
示例#9
0
    def test_set_out(self):
        # preferable would be the shorter format without the ': null'
        import ruyaml  # NOQA

        x = set(['a', 'b', 'c'])
        res = ruyaml.dump(x, default_flow_style=False)
        assert res == dedent("""
        !!set
        a: null
        b: null
        c: null
        """)
示例#10
0
    def test_omap_out(self):
        # ordereddict mapped to !!omap
        import ruyaml  # NOQA
        from ruyaml.compat import ordereddict

        x = ordereddict([('a', 1), ('b', 2)])
        res = ruyaml.dump(x, default_flow_style=False)
        assert res == dedent("""
        !!omap
        - a: 1
        - b: 2
        """)
示例#11
0
def test_recursive(recursive_filename, verbose=False):
    context = globals().copy()
    with open(recursive_filename, 'rb') as fp0:
        exec(fp0.read(), context)
    value1 = context['value']
    output1 = None
    value2 = None
    output2 = None
    try:
        output1 = yaml.dump(value1)
        value2 = yaml.load(output1)
        output2 = yaml.dump(value2)
        assert output1 == output2, (output1, output2)
    finally:
        if verbose:
            print('VALUE1:', value1)
            print('VALUE2:', value2)
            print('OUTPUT1:')
            print(output1)
            print('OUTPUT2:')
            print(output2)
示例#12
0
def test_dump_cyaml():
    import ruyaml

    if sys.version_info >= (3, 8):
        return
    data = {'a': 1, 'b': 2}
    res = ruyaml.dump(
        data,
        Dumper=ruyaml.cyaml.CSafeDumper,
        default_flow_style=False,
        allow_unicode=True,
    )
    assert res == 'a: 1\nb: 2\n'
示例#13
0
def Xtest_numpy():
    import ruyaml

    if numpy is None:
        return
    data = numpy.arange(10)
    print('data', type(data), data)

    yaml_str = ruyaml.dump(data)
    datb = ruyaml.load(yaml_str)
    print('datb', type(datb), datb)

    print('\nYAML', yaml_str)
    assert data == datb
示例#14
0
    def test_dump_ruamel_ordereddict(self):
        from ruamel.ordereddict import ordereddict

        import ruyaml  # NOQA

        # OrderedDict mapped to !!omap
        x = ordereddict([('a', 1), ('b', 2)])
        res = ruyaml.dump(x,
                          Dumper=ruyaml.RoundTripDumper,
                          default_flow_style=False)
        assert res == dedent("""
        !!omap
        - a: 1
        - b: 2
        """)
示例#15
0
    def test_dump_unicode_utf8(self):
        import ruyaml  # NOQA

        x = dedent(u"""\
        ab:
        - x  # comment
        - y  # more comment
        """)
        data = round_trip_load(x)
        dumper = ruyaml.RoundTripDumper
        for utf in [True, False]:
            y = ruyaml.dump(data,
                            default_flow_style=False,
                            Dumper=dumper,
                            allow_unicode=utf)
            assert y == x
示例#16
0
    def test_added_inline_list(self):
        import ruyaml

        s1 = dedent("""
        a:
        - b
        - c
        - d
        """)
        s = 'a: [b, c, d]\n'
        data = ruyaml.load(s1, Loader=ruyaml.RoundTripLoader)
        val = data['a']
        val.fa.set_flow_style()
        # print(type(val), '_yaml_format' in dir(val))
        output = ruyaml.dump(data, Dumper=ruyaml.RoundTripDumper)
        assert s == output
示例#17
0
def test_representer_types(code_filename, verbose=False):
    yaml = YAML(typ='safe', pure=True)
    test_constructor._make_objects()
    for allow_unicode in [False, True]:
        for encoding in ['utf-8', 'utf-16-be', 'utf-16-le']:
            with open(code_filename, 'rb') as fp0:
                native1 = test_constructor._load_code(fp0.read())
            native2 = None
            try:
                output = yaml.dump(
                    native1,
                    Dumper=test_constructor.MyDumper,
                    allow_unicode=allow_unicode,
                    encoding=encoding,
                )
                native2 = yaml.load(output, Loader=test_constructor.MyLoader)
                try:
                    if native1 == native2:
                        continue
                except TypeError:
                    pass
                value1 = test_constructor._serialize_value(native1)
                value2 = test_constructor._serialize_value(native2)
                if verbose:
                    print('SERIALIZED NATIVE1:')
                    print(value1)
                    print('SERIALIZED NATIVE2:')
                    print(value2)
                assert value1 == value2, (native1, native2)
            finally:
                if verbose:
                    print('NATIVE1:')
                    pprint.pprint(native1)
                    print('NATIVE2:')
                    pprint.pprint(native2)
                    print('OUTPUT:')
                    print(output)
示例#18
0
def main():
    editor = ProjectEditor()
    editor.create_new_module()
    with open('DragonMake', 'w') as f:
        f.write(yaml.dump(editor.config, Dumper=yaml.RoundTripDumper))
示例#19
0
文件: sigmacover.py 项目: yt0ng/sigma
                       help="Output target format")
cmdargs = argparser.parse_args()

if cmdargs.target == None:
    print("No outpout use -h to see help")
    exit()

#init dict of all rules
default_key_test = {key: "NO TEST" for key in backend_dict.keys()}
the_dico = {}
rules = pathlib.Path("../rules").glob("**/*.yml")
for rule in rules:
    the_dico[rule.name] = copy.deepcopy(default_key_test)

#Check all the backend
for name, opt in backend_dict.items():
    print(f"check backend : {name}")
    result = get_sigmac(name, opt)
    update_dict(the_dico, result, name)

#Save
if cmdargs.target.lower() == "yaml":
    cover = pathlib.Path("sigmacover.yml")
    with cover.open("w") as file:
        ruyaml.dump(the_dico, file, Dumper=ruyaml.RoundTripDumper)
else:
    cover = pathlib.Path("sigmacover.json")
    with cover.open("w") as file:
        json_dumps_str = json.dumps(the_dico, indent=4)
        file.write(json_dumps_str)
示例#20
0
    def test_ordereddict(self):
        from collections import OrderedDict

        import ruyaml  # NOQA

        assert ruyaml.dump(OrderedDict()) == '!!omap []\n'
示例#21
0
def to_yaml_file(obj, f):
    """
    Convert Python objects (including rpcq messages) to yaml and write it to `f`.
    """
    yaml.dump(rapidjson.loads(to_json(obj)), f)