def validate_yaml_import(yaml_file): """Validate clowder.yaml with an import""" parsed_yaml = parse_yaml(yaml_file) _validate_type_dict(parsed_yaml, fmt.yaml_file('clowder.yaml'), yaml_file) if 'import' not in parsed_yaml: error = fmt.missing_entry_error('import', fmt.yaml_file('clowder.yaml'), yaml_file) raise ClowderError(error) _validate_type_str(parsed_yaml['import'], 'import', yaml_file) del parsed_yaml['import'] if not parsed_yaml: error = fmt.empty_yaml_error(yaml_file) raise ClowderError(error) if 'defaults' in parsed_yaml: _validate_yaml_import_defaults(parsed_yaml['defaults'], yaml_file) del parsed_yaml['defaults'] if 'sources' in parsed_yaml: _validate_yaml_sources(parsed_yaml['sources'], yaml_file) del parsed_yaml['sources'] if 'groups' in parsed_yaml: _validate_yaml_import_groups(parsed_yaml['groups'], yaml_file) del parsed_yaml['groups'] if parsed_yaml: error = fmt.invalid_entries_error(fmt.yaml_file('clowder.yaml'), parsed_yaml, yaml_file) raise ClowderError(error)
def _validate_yaml(yaml_file): """Validate clowder.yaml with no import :param str yaml_file: Path to yaml file :raise ClowderYAMLError: """ parsed_yaml = parse_yaml(yaml_file) validate_type(parsed_yaml, fmt.yaml_file('clowder.yaml'), dict, 'dict', yaml_file) if not parsed_yaml: raise ClowderYAMLError(fmt.empty_yaml_error(yaml_file)) validate_required_dict(parsed_yaml, 'defaults', validate_yaml_defaults, yaml_file) validate_required_dict(parsed_yaml, 'sources', validate_yaml_sources, yaml_file) validate_required_dict(parsed_yaml, 'groups', validate_yaml_groups, yaml_file) if parsed_yaml: raise ClowderYAMLError(fmt.unknown_entry_error(fmt.yaml_file('clowder.yaml'), parsed_yaml, yaml_file))
def parse_yaml(yaml_file): """Parse yaml file""" if os.path.isfile(yaml_file): try: with open(yaml_file) as raw_file: parsed_yaml = yaml.safe_load(raw_file) if parsed_yaml is None: print(fmt.invalid_yaml_error()) print(fmt.empty_yaml_error(yaml_file) + '\n') sys.exit(1) return parsed_yaml except yaml.YAMLError: fmt.open_file_error(yaml_file) sys.exit(1) except (KeyboardInterrupt, SystemExit): sys.exit(1) else: print() print(fmt.missing_yaml_error()) print() sys.exit(1)
def _validate_yaml_import(yaml_file): """Validate clowder.yaml with an import :param str yaml_file: Path to yaml file :raise ClowderYAMLError: """ parsed_yaml = parse_yaml(yaml_file) validate_type(parsed_yaml, fmt.yaml_file('clowder.yaml'), dict, 'dict', yaml_file) validate_clowder_yaml_contains_value(parsed_yaml, 'import', yaml_file) validate_type(parsed_yaml['import'], 'import', str, 'str', yaml_file) del parsed_yaml['import'] if not parsed_yaml: raise ClowderYAMLError(fmt.empty_yaml_error(yaml_file)) validate_optional_dict(parsed_yaml, 'defaults', validate_yaml_defaults_import, yaml_file) validate_optional_dict(parsed_yaml, 'sources', validate_yaml_sources, yaml_file) validate_optional_dict(parsed_yaml, 'groups', validate_yaml_groups_import, yaml_file) if parsed_yaml: raise ClowderYAMLError(fmt.unknown_entry_error(fmt.yaml_file('clowder.yaml'), parsed_yaml, yaml_file))
def parse_yaml(yaml_file): """Parse yaml file :param str yaml_file: Path to yaml file :return: YAML python object :rtype: dict Raises: ClowderExit ClowderYAMLError """ if not os.path.isfile(yaml_file): raise ClowderYAMLError(fmt.missing_yaml_error()) try: with open(yaml_file) as raw_file: parsed_yaml = yaml.safe_load(raw_file) if parsed_yaml is None: raise ClowderYAMLError(fmt.empty_yaml_error(yaml_file)) return parsed_yaml except yaml.YAMLError: raise ClowderYAMLError(fmt.open_file_error(yaml_file)) except (KeyboardInterrupt, SystemExit): raise ClowderExit(1)