diff --git a/poetry/core/pyproject/__init__.py b/poetry/core/pyproject/__init__.py index 00b410886..9f760fbe2 100644 --- a/poetry/core/pyproject/__init__.py +++ b/poetry/core/pyproject/__init__.py @@ -1,10 +1,6 @@ from poetry.core.pyproject.exceptions import PyProjectException from poetry.core.pyproject.tables import BuildSystem from poetry.core.pyproject.toml import PyProjectTOML -from poetry.core.pyproject.toml import PyProjectTOMLFile -__all__ = [ - clazz.__name__ - for clazz in {BuildSystem, PyProjectException, PyProjectTOML, PyProjectTOMLFile} -] +__all__ = [clazz.__name__ for clazz in {BuildSystem, PyProjectException, PyProjectTOML}] diff --git a/poetry/core/pyproject/toml.py b/poetry/core/pyproject/toml.py index 6be102acf..50d5fc3ba 100644 --- a/poetry/core/pyproject/toml.py +++ b/poetry/core/pyproject/toml.py @@ -2,53 +2,23 @@ from typing import Union from tomlkit.container import Container -from tomlkit.exceptions import TOMLKitError from tomlkit.toml_document import TOMLDocument -from tomlkit.toml_file import TOMLFile from poetry.core.pyproject.exceptions import PyProjectException from poetry.core.pyproject.tables import BuildSystem +from poetry.core.toml import TOMLFile from poetry.core.utils._compat import Path -class PyProjectTOMLFile(TOMLFile): - def __init__(self, path): # type: (Union[str, Path]) -> None - if isinstance(path, str): - path = Path(path) - super(PyProjectTOMLFile, self).__init__(path.as_posix()) - self.__path = path - - @property - def path(self): # type: () -> Path - return self.__path - - def exists(self): # type: () -> bool - return self.__path.exists() - - def read(self): - try: - return super(PyProjectTOMLFile, self).read() - except (ValueError, TOMLKitError) as e: - raise PyProjectException( - "Invalid TOML file {}: {}".format(self.path.as_posix(), e) - ) - - def __getattr__(self, item): - return getattr(self.__path, item) - - def __str__(self): # type: () -> str - return self.__path.as_posix() - - class PyProjectTOML: def __init__(self, path): # type: (Union[str, Path]) -> None - self._file = PyProjectTOMLFile(path=path) + self._file = TOMLFile(path=path) self._data = None # type: Optional[TOMLDocument] self._build_system = None # type: Optional[BuildSystem] self._poetry_config = None # type: Optional[TOMLDocument] @property - def file(self): # type: () -> PyProjectTOMLFile + def file(self): # type: () -> TOMLFile return self._file @property diff --git a/poetry/core/toml/__init__.py b/poetry/core/toml/__init__.py new file mode 100644 index 000000000..affd9054c --- /dev/null +++ b/poetry/core/toml/__init__.py @@ -0,0 +1,5 @@ +from poetry.core.toml.exceptions import TOMLError +from poetry.core.toml.file import TOMLFile + + +__all__ = [clazz.__name__ for clazz in {TOMLError, TOMLFile}] diff --git a/poetry/core/toml/exceptions.py b/poetry/core/toml/exceptions.py new file mode 100644 index 000000000..03959e29f --- /dev/null +++ b/poetry/core/toml/exceptions.py @@ -0,0 +1,7 @@ +from tomlkit.exceptions import TOMLKitError + +from poetry.core.exceptions import PoetryCoreException + + +class TOMLError(TOMLKitError, PoetryCoreException): + pass diff --git a/poetry/core/toml/file.py b/poetry/core/toml/file.py new file mode 100644 index 000000000..0c81007fb --- /dev/null +++ b/poetry/core/toml/file.py @@ -0,0 +1,34 @@ +from typing import Union + +from tomlkit.exceptions import TOMLKitError +from tomlkit.toml_file import TOMLFile as BaseTOMLFile + +from poetry.core.toml import TOMLError +from poetry.core.utils._compat import Path + + +class TOMLFile(BaseTOMLFile): + def __init__(self, path): # type: (Union[str, Path]) -> None + if isinstance(path, str): + path = Path(path) + super(TOMLFile, self).__init__(path.as_posix()) + self.__path = path + + @property + def path(self): # type: () -> Path + return self.__path + + def exists(self): # type: () -> bool + return self.__path.exists() + + def read(self): + try: + return super(TOMLFile, self).read() + except (ValueError, TOMLKitError) as e: + raise TOMLError("Invalid TOML file {}: {}".format(self.path.as_posix(), e)) + + def __getattr__(self, item): + return getattr(self.__path, item) + + def __str__(self): # type: () -> str + return self.__path.as_posix() diff --git a/poetry/core/utils/toml_file.py b/poetry/core/utils/toml_file.py index 0bb9ec9d2..4ac333afe 100644 --- a/poetry/core/utils/toml_file.py +++ b/poetry/core/utils/toml_file.py @@ -1,18 +1,15 @@ # -*- coding: utf-8 -*- -from poetry.core.pyproject import PyProjectTOMLFile +from poetry.core.toml import TOMLFile -class TomlFile(PyProjectTOMLFile): +class TomlFile(TOMLFile): @classmethod def __new__(cls, *args, **kwargs): import warnings warnings.warn( "Use of {}.{} has been deprecated, use {}.{} instead.".format( - cls.__module__, - cls.__name__, - PyProjectTOMLFile.__module__, - PyProjectTOMLFile.__name__, + cls.__module__, cls.__name__, TOMLFile.__module__, TOMLFile.__name__, ), category=DeprecationWarning, stacklevel=2, diff --git a/tests/pyproject/test_pyproject_toml_file.py b/tests/pyproject/test_pyproject_toml_file.py index 1a7e94d61..071dab28f 100644 --- a/tests/pyproject/test_pyproject_toml_file.py +++ b/tests/pyproject/test_pyproject_toml_file.py @@ -1,31 +1,32 @@ import pytest -from poetry.core.pyproject import PyProjectException -from poetry.core.pyproject import PyProjectTOMLFile +from poetry.core.exceptions import PoetryCoreException +from poetry.core.toml import TOMLFile from poetry.core.utils._compat import decode -from poetry.core.utils.toml_file import TomlFile def test_old_pyproject_toml_file_deprecation( pyproject_toml, build_system_section, poetry_section ): + from poetry.core.utils.toml_file import TomlFile + with pytest.warns(DeprecationWarning): file = TomlFile(pyproject_toml) data = file.read() - assert data == PyProjectTOMLFile(pyproject_toml).read() + assert data == TOMLFile(pyproject_toml).read() def test_pyproject_toml_file_invalid(pyproject_toml): with pyproject_toml.open(mode="a") as f: f.write(decode("<<<<<<<<<<<")) - with pytest.raises(PyProjectException) as excval: - _ = PyProjectTOMLFile(pyproject_toml).read() + with pytest.raises(PoetryCoreException) as excval: + _ = TOMLFile(pyproject_toml).read() assert "Invalid TOML file {}".format(pyproject_toml.as_posix()) in str(excval.value) def test_pyproject_toml_file_getattr(tmp_path, pyproject_toml): - file = PyProjectTOMLFile(pyproject_toml) + file = TOMLFile(pyproject_toml) assert file.parent == tmp_path diff --git a/tests/test_factory.py b/tests/test_factory.py index b5100862e..44693b813 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -5,7 +5,7 @@ import pytest from poetry.core.factory import Factory -from poetry.core.pyproject import PyProjectTOMLFile +from poetry.core.toml import TOMLFile from poetry.core.utils._compat import PY2 from poetry.core.utils._compat import Path @@ -157,14 +157,14 @@ def test_create_poetry_with_multi_constraints_dependency(): def test_validate(): - complete = PyProjectTOMLFile(fixtures_dir / "complete.toml") + complete = TOMLFile(fixtures_dir / "complete.toml") content = complete.read()["tool"]["poetry"] assert Factory.validate(content) == {"errors": [], "warnings": []} def test_validate_fails(): - complete = PyProjectTOMLFile(fixtures_dir / "complete.toml") + complete = TOMLFile(fixtures_dir / "complete.toml") content = complete.read()["tool"]["poetry"] content["this key is not in the schema"] = "" diff --git a/tests/testutils.py b/tests/testutils.py index 93e684cb8..8e4fc266f 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -10,7 +10,7 @@ from typing import List from typing import Optional -from poetry.core.pyproject import PyProjectTOMLFile +from poetry.core.toml import TOMLFile from poetry.core.utils._compat import Path @@ -49,7 +49,7 @@ def temporary_project_directory( with tempfile.TemporaryDirectory(prefix="poetry-core-pep517") as tmp: dst = Path(tmp) / path.name shutil.copytree(str(path), dst) - toml = PyProjectTOMLFile(str(dst / "pyproject.toml")) + toml = TOMLFile(str(dst / "pyproject.toml")) data = toml.read() data.update(toml_patch or __toml_build_backend_patch__) toml.write(data)