def test_parse_yaml_duplicate_key_error(): text = """\ mykey: - foo mykey: - bar """ with pytest.raises(YAMLError, match='found duplicate key "mykey"'): parse_yaml(text, "mypath")
def test_parse_yaml_duplicate_key_error(): text = """\ mykey: - foo mykey: - bar """ with pytest.raises(YAMLFileCorruptedError): parse_yaml(text, "mypath")
def load( path: str, schema: Callable[[_T], _T] = None, fs: "FileSystem" = None, encoding: str = "utf-8", round_trip: bool = False, ) -> Any: open_fn = fs.open if fs else open rev = getattr(fs, "rev", None) try: with open_fn(path, encoding=encoding) as fd: # type: ignore text = fd.read() data = parse_yaml(text, path, typ="rt" if round_trip else "safe") except UnicodeDecodeError as exc: raise EncodingError(path, encoding) from exc except YAMLFileCorruptedError as exc: cause = exc.__cause__ raise YAMLSyntaxError(path, text, exc, rev=rev) from cause if schema: # not returning validated data, as it may remove # details from CommentedMap that we get from roundtrip parser validate(data, schema, text=text, path=path, rev=rev) return data, text
def _load(self): # it raises the proper exceptions by priority: # 1. when the file doesn't exists # 2. filename is not a DVC-file # 3. path doesn't represent a regular file if not self.exists(): raise StageFileDoesNotExistError(self.path) if self.verify: check_dvc_filename(self.path) if not self.repo.tree.isfile(self.path): raise StageFileIsNotDvcFileError(self.path) with self.repo.tree.open(self.path) as fd: stage_text = fd.read() d = parse_yaml(stage_text, self.path) return self.validate(d, self.relpath), stage_text
def _load(self): # it raises the proper exceptions by priority: # 1. when the file doesn't exists # 2. filename is not a DVC file # 3. path doesn't represent a regular file # 4. when the file is git ignored if not self.exists(): is_ignored = self.repo.fs.exists(self.path, use_dvcignore=False) raise StageFileDoesNotExistError(self.path, dvc_ignored=is_ignored) self._verify_filename() if not self.repo.fs.isfile(self.path): raise StageFileIsNotDvcFileError(self.path) self._check_gitignored() with self.repo.fs.open(self.path, encoding="utf-8") as fd: stage_text = fd.read() d = parse_yaml(stage_text, self.path) return self.validate(d, self.relpath), stage_text
def raw(self, **kwargs): return parse_yaml(self.content, self.filename, typ="rt")