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_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_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}')
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_176(self): # basic request by Stuart Berg from ruyaml import YAML yaml = YAML() seq = yaml.load('[1,2,3]') seq[:] = [1, 2, 3, 4]
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
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_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
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)
def test_issue_233a(self): import json from ruyaml import YAML yaml = YAML() data = yaml.load('[]') json_str = json.dumps(data) # NOQA
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 yaml(self, yaml_version=None): from ruyaml import YAML y = YAML() y.preserve_quotes = True if yaml_version: y.version = yaml_version return y
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_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©'
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_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'
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_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_root_literal_doc_indent_document_end(self): yaml = YAML() yaml.explicit_start = True inp = """ --- |- some more ... text """ yaml.round_trip(inp)
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'
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'
def test_root_literal_doc_indent_directives_end(self): yaml = YAML() yaml.explicit_start = True inp = """ --- |- %YAML 1.3 --- this: is a test """ yaml.round_trip(inp)
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'
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'
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
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_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 docs(self, path): from ruyaml import YAML tyaml = YAML(typ='safe', pure=True) tyaml.register_class(YAMLData) tyaml.register_class(Python) tyaml.register_class(Output) tyaml.register_class(Assert) return list(tyaml.load_all(path))