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_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 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)
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
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))
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_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_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 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 """)
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 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_233a(self): import json from ruyaml import YAML yaml = YAML() data = yaml.load('[]') json_str = json.dumps(data) # NOQA
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)
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_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 """ ) )
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_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_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_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_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_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_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_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_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)
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_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_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_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'
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