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
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
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
def test_issue_135_temporary_workaround(self): # never raised error from ruyaml import YAML data = {'a': 1, 'b': 2} yaml = YAML(typ='safe', pure=True) yaml.dump(data, sys.stdout)
def test_issue_135(self): # reported by Andrzej Ostrowski from ruyaml import YAML data = {'a': 1, 'b': 2} yaml = YAML(typ='safe') # originally on 2.7: with pytest.raises(TypeError): yaml.dump(data, sys.stdout)
def test_write_unicode(self, tmpdir): from ruyaml import YAML yaml = YAML() text_dict = {'text': 'HELLO_WORLD©'} file_name = str(tmpdir) + '/tstFile.yaml' yaml.dump(text_dict, open(file_name, 'w')) assert open(file_name, 'rb').read().decode('utf-8') == 'text: HELLO_WORLD©\n'
def test_dump_missing_stream(self): from ruyaml import YAML yaml = YAML() data = yaml.map() data['a'] = 1 data['b'] = 2 with pytest.raises(TypeError): yaml.dump(data)
def test_print(self, capsys): from ruyaml import YAML yaml = YAML() data = yaml.map() data['a'] = 1 data['b'] = 2 yaml.dump(data, sys.stdout) out, err = capsys.readouterr() assert out == 'a: 1\nb: 2\n'
def test_dump_path(self, tmpdir): from ruyaml import YAML fn = Path(str(tmpdir)) / 'test.yaml' yaml = YAML() data = yaml.map() data['a'] = 1 data['b'] = 2 yaml.dump(data, fn) assert fn.read_text() == 'a: 1\nb: 2\n'
def test_dump_too_many_args(self, tmpdir): from ruyaml import YAML fn = Path(str(tmpdir)) / 'test.yaml' yaml = YAML() data = yaml.map() data['a'] = 1 data['b'] = 2 with pytest.raises(TypeError): yaml.dump(data, fn, True)
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)
def test_dump_file(self, tmpdir): from ruyaml import YAML fn = Path(str(tmpdir)) / 'test.yaml' yaml = YAML() data = yaml.map() data['a'] = 1 data['b'] = 2 with open(str(fn), 'w') as fp: yaml.dump(data, fp) assert fn.read_text() == 'a: 1\nb: 2\n'
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)
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)
def test_flow_style(self, capsys): # https://stackoverflow.com/questions/45791712/ from ruyaml import YAML yaml = YAML() yaml.default_flow_style = None data = yaml.map() data['b'] = 1 data['a'] = [[1, 2], [3, 4]] yaml.dump(data, sys.stdout) out, err = capsys.readouterr() assert out == 'b: 1\na:\n- [1, 2]\n- [3, 4]\n'
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)
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)
def test_transform(self, tmpdir): from ruyaml import YAML def tr(s): return s.replace(' ', ' ') fn = Path(str(tmpdir)) / 'test.yaml' yaml = YAML() data = yaml.map() data['a'] = 1 data['b'] = 2 yaml.dump(data, fn, transform=tr) assert fn.read_text() == 'a: 1\nb: 2\n'
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)
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
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')
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
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
def save(self) -> None: """Save the configuration in the configuration YAML file.""" with open(os.path.expanduser(self.config_path), "w+") as file_cursor: yaml = YAML() yaml.default_flow_style = False yaml.dump(self.data, file_cursor)