def get_role_spec(data: str, schema: str) -> dict: """ Read and parse roles specification from role_spec.yaml file. Specification file structure is checked against role_schema.yaml file. (see https://github.com/arenadata/yspec for details about schema syntaxis) """ try: with open(data, encoding='utf_8') as fd: data = ruyaml.round_trip_load(fd) except FileNotFoundError: err('INVALID_ROLE_SPEC', f'Can not open role file "{data}"') except (ruyaml.parser.ParserError, ruyaml.scanner.ScannerError, NotImplementedError) as e: err('INVALID_ROLE_SPEC', f'YAML decode "{data}" error: {e}') with open(schema, encoding='utf_8') as fd: rules = ruyaml.round_trip_load(fd) try: cm.checker.check(data, rules) except cm.checker.FormatError as e: args = '' if e.errors: for ee in e.errors: if 'Input data for' in ee.message: continue args += f'line {ee.line}: {ee}\n' err('INVALID_ROLE_SPEC', f'line {e.line} error: {e}', args) return data
def test_duplicate_key_00(self): from ruyaml import round_trip_load, safe_load from ruyaml.constructor import DuplicateKeyError s = dedent("""\ &anchor foo: foo: bar *anchor : duplicate key baz: bat *anchor : duplicate key """) with pytest.raises(DuplicateKeyError): safe_load(s) with pytest.raises(DuplicateKeyError): round_trip_load(s)
def check_adcm_config(conf_file): warnings.simplefilter('error', ruyaml.error.ReusedAnchorWarning) schema_file = os.path.join(config.CODE_DIR, 'cm', 'adcm_schema.yaml') with open(schema_file, encoding='utf_8') as fd: rules = ruyaml.round_trip_load(fd) try: with open(conf_file, encoding='utf_8') as fd: data = cm.checker.round_trip_load(fd, version="1.1", allow_duplicate_keys=True) except (ruyaml.parser.ParserError, ruyaml.scanner.ScannerError, NotImplementedError) as e: err('STACK_LOAD_ERROR', f'YAML decode "{conf_file}" error: {e}') except ruyaml.error.ReusedAnchorWarning as e: err('STACK_LOAD_ERROR', f'YAML decode "{conf_file}" error: {e}') except ruyaml.constructor.DuplicateKeyError as e: msg = f'{e.context}\n{e.context_mark}\n{e.problem}\n{e.problem_mark}' err('STACK_LOAD_ERROR', f'Duplicate Keys error: {msg}') except ruyaml.composer.ComposerError as e: err('STACK_LOAD_ERROR', f'YAML Composer error: {e}') try: cm.checker.check(data, rules) return data except cm.checker.FormatError as e: args = '' if e.errors: for ee in e.errors: if 'Input data for' in ee.message: continue args += f'line {ee.line}: {ee}\n' err('INVALID_OBJECT_DEFINITION', f'"{conf_file}" line {e.line} error: {e}', args) return {}
def test_dump00(self): import ruyaml # NOQA data = None s = ruyaml.round_trip_dump(data) assert s == 'null\n...\n' d = ruyaml.round_trip_load(s) assert d == data
def test_dump03(self): import ruyaml # NOQA data = None s = ruyaml.round_trip_dump(data, explicit_start=True) assert s == '---\n...\n' d = ruyaml.round_trip_load(s) assert d == data
def test_dump02(self): import ruyaml # NOQA data = None s = ruyaml.round_trip_dump(data, explicit_end=False) assert s == 'null\n...\n' d = ruyaml.round_trip_load(s) assert d == data
def load_file(filename: str, version="1.2"): if pathlib.Path(filename).suffix not in ('.yml', '.yaml', '.json'): print(f'Unknown extension of file "{filename}"') sys.exit(1) with open(filename, 'r') as stream: try: return ruyaml.round_trip_load(stream, version=version) except ruyaml.parser.ParserError as e: print(e) sys.exit(1)
def test_merge_items(self): from ruyaml import safe_load d = safe_load(self.yaml_str) data = round_trip_load(self.yaml_str) count = 0 for x in data[2].items(): count += 1 print(count, x) assert count == len(d[2])
def check_config(data_file, schema_file, print_ok=True): rules = ruyaml.round_trip_load(open(schema_file, encoding='utf_8')) try: # ruyaml.version_info=(0, 15, 0) # switch off duplicate key error data = ruyaml.round_trip_load(open(data_file, encoding='utf_8'), version="1.1") except FileNotFoundError as e: print(e) return 1 except ruyaml.constructor.DuplicateKeyError as e: print(f'Config file "{data_file}" Duplicate Keys Error:') print(f'{e.context}\n{e.context_mark}\n{e.problem}\n{e.problem_mark}') return 1 except (ruyaml.parser.ParserError, ruyaml.scanner.ScannerError, NotImplementedError) as e: print(f'Config file "{data_file}" YAML Parser Error:') print(f'{e}') return 1 try: cm.checker.check(data, rules) if print_ok: print(f'Config file "{data_file}" is OK') return 0 except cm.checker.DataError as e: print(f'File "{data_file}", error: {e}') return 1 except cm.checker.SchemaError as e: print(f'File "{schema_file}" error: {e}') return 1 except cm.checker.FormatError as e: print(f'Data File "{data_file}" Errors:') print(f'\tline {e.line}: {e.message}') if e.errors: for ee in e.errors: if 'Input data for' in ee.message: continue print(f'\tline {ee.line}: {ee.message}') print( f'Schema File "{schema_file}" line {rules[e.rule].lc.line}, Rule: "{e.rule}"' ) return 1
def test_issue_61(self): import ruyaml s = dedent(""" def1: &ANCHOR1 key1: value1 def: &ANCHOR <<: *ANCHOR1 key: value comb: <<: *ANCHOR """) data = ruyaml.round_trip_load(s) assert str(data['comb']) == str(data['def']) assert (str(data['comb']) == "ordereddict([('key', 'value'), ('key1', 'value1')])")
def test_scalar_with_comments(self): import ruyaml # NOQA for x in [ "", '\n', '\n# Another comment\n', '\n\n', '\n\n# abc\n#xyz\n', '\n\n# abc\n#xyz\n', '# abc\n\n#xyz\n', '\n\n # abc\n #xyz\n', ]: commented_line = test_block_scalar_commented_line_template.format(x) data = ruyaml.round_trip_load(commented_line) assert ruyaml.round_trip_dump(data) == commented_line
def test_len_items_delete(self): from ruyaml import safe_load d = safe_load(self.yaml_str) data = round_trip_load(self.yaml_str) x = data[2].items() print('d2 items', d[2].items(), len(d[2].items()), x, len(x)) ref = len(d[2].items()) print('ref', ref) assert len(x) == ref del data[2]['m'] ref -= 1 assert len(x) == ref del data[2]['d'] ref -= 1 assert len(x) == ref del data[2]['a'] ref -= 1 assert len(x) == ref
def test_issue_127(): import ruyaml # NOQA class Ref(ruyaml.YAMLObject): yaml_constructor = ruyaml.RoundTripConstructor yaml_representer = ruyaml.RoundTripRepresenter yaml_tag = u'!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' """) data = ruyaml.round_trip_load(document, preserve_quotes=True) assert ruyaml.round_trip_dump(data, indent=4, block_seq_indent=2) == document.replace( '\n Two and', ' Two and')
def load(s): return round_trip_load(dedent(s))
def load(s, version=None): import ruyaml # NOQA return ruyaml.round_trip_load(dedent(s), version)