Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions poetry/core/pyproject/__init__.py
Original file line number Diff line number Diff line change
@@ -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}]
36 changes: 3 additions & 33 deletions poetry/core/pyproject/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions poetry/core/toml/__init__.py
Original file line number Diff line number Diff line change
@@ -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}]
7 changes: 7 additions & 0 deletions poetry/core/toml/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from tomlkit.exceptions import TOMLKitError

from poetry.core.exceptions import PoetryCoreException


class TOMLError(TOMLKitError, PoetryCoreException):
pass
34 changes: 34 additions & 0 deletions poetry/core/toml/file.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 3 additions & 6 deletions poetry/core/utils/toml_file.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
15 changes: 8 additions & 7 deletions tests/pyproject/test_pyproject_toml_file.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"] = ""

Expand Down
4 changes: 2 additions & 2 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down