Skip to content

Commit

Permalink
removing tomlkit from poetry-core
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Sep 24, 2022
1 parent ea4c56f commit b01c5c0
Show file tree
Hide file tree
Showing 42 changed files with 362 additions and 114 deletions.
2 changes: 1 addition & 1 deletion src/poetry/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from typing import Any

from packaging.utils import canonicalize_name
from poetry.core.toml import TOMLFile

from poetry.config.dict_config_source import DictConfigSource
from poetry.config.file_config_source import FileConfigSource
from poetry.locations import CONFIG_DIR
from poetry.locations import DEFAULT_CACHE_DIR
from poetry.toml import TOMLFile


if TYPE_CHECKING:
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/config/file_config_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
if TYPE_CHECKING:
from collections.abc import Iterator

from poetry.core.toml.file import TOMLFile
from tomlkit.toml_document import TOMLDocument

from poetry.toml.file import TOMLFile


class FileConfigSource(ConfigSource):
def __init__(self, file: TOMLFile, auth_config: bool = False) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def handle(self) -> int:

# tomlkit types are awkward to work with, treat content as a mostly untyped
# dictionary.
content: dict[str, Any] = self.poetry.file.read()
content: dict[str, Any] = self.poetry.toml_file.read()
poetry_content = content["tool"]["poetry"]

if group == MAIN_GROUP:
Expand Down Expand Up @@ -253,7 +253,7 @@ def handle(self) -> int:

if status == 0 and not self.option("dry-run"):
assert isinstance(content, TOMLDocument)
self.poetry.file.write(content)
self.poetry.toml_file.write(content)

return status

Expand Down
3 changes: 1 addition & 2 deletions src/poetry/console/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ def validate_classifiers(
return errors, warnings

def handle(self) -> int:
from poetry.core.pyproject.toml import PyProjectTOML

from poetry.factory import Factory
from poetry.pyproject.toml import PyProjectTOML

# Load poetry config and display errors, if any
poetry_file = Factory.locate(Path.cwd())
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ def handle(self) -> int:
from pathlib import Path

from poetry.core.pyproject.exceptions import PyProjectException
from poetry.core.toml.file import TOMLFile

from poetry.config.config import Config
from poetry.config.file_config_source import FileConfigSource
from poetry.locations import CONFIG_DIR
from poetry.toml.file import TOMLFile

config = Config.create()
config_file = TOMLFile(CONFIG_DIR / "config.toml")
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def __init__(self) -> None:
def handle(self) -> int:
from pathlib import Path

from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.vcs.git import GitConfig

from poetry.layouts import layout
from poetry.pyproject.toml import PyProjectTOML
from poetry.utils.env import SystemEnv

pyproject = PyProjectTOML(Path.cwd() / "pyproject.toml")
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/console/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def handle(self) -> int:
else:
group = self.option("group", self.default_group)

content: dict[str, Any] = self.poetry.file.read()
content: dict[str, Any] = self.poetry.toml_file.read()
poetry_content = content["tool"]["poetry"]

if group is None:
Expand Down Expand Up @@ -117,7 +117,7 @@ def handle(self) -> int:

if not self.option("dry-run") and status == 0:
assert isinstance(content, TOMLDocument)
self.poetry.file.write(content)
self.poetry.toml_file.write(content)

return status

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/self/self_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

from poetry.core.packages.dependency import Dependency
from poetry.core.packages.project_package import ProjectPackage
from poetry.core.pyproject.toml import PyProjectTOML

from poetry.__version__ import __version__
from poetry.console.commands.installer_command import InstallerCommand
from poetry.factory import Factory
from poetry.pyproject.toml import PyProjectTOML
from poetry.utils.env import EnvManager
from poetry.utils.env import SystemEnv
from poetry.utils.helpers import directory
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/console/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def handle(self) -> int:
)

if not self.option("dry-run"):
content: dict[str, Any] = self.poetry.file.read()
content: dict[str, Any] = self.poetry.toml_file.read()
poetry_content = content["tool"]["poetry"]
poetry_content["version"] = version.text

assert isinstance(content, TOMLDocument)
self.poetry.file.write(content)
self.poetry.toml_file.write(content)
else:
if self.option("short"):
self.line(self.poetry.package.pretty_version)
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
from poetry.core.factory import Factory as BaseFactory
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.project_package import ProjectPackage
from poetry.core.toml.file import TOMLFile

from poetry.config.config import Config
from poetry.json import validate_object
from poetry.packages.locker import Locker
from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager
from poetry.poetry import Poetry
from poetry.toml.file import TOMLFile


if TYPE_CHECKING:
Expand Down Expand Up @@ -81,7 +81,7 @@ def create_poetry(
config.merge({"repositories": repositories})

poetry = Poetry(
base_poetry.file.path,
base_poetry.file,
base_poetry.local_config,
base_poetry.package,
locker,
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/inspection/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
from poetry.core.factory import Factory
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.utils.helpers import parse_requires
from poetry.core.utils.helpers import temporary_directory
from poetry.core.version.markers import InvalidMarker

from poetry.pyproject.toml import PyProjectTOML
from poetry.utils.env import EnvCommandError
from poetry.utils.env import ephemeral_environment
from poetry.utils.setup_reader import SetupReader
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
from cleo.io.null_io import NullIO
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.utils.link import Link
from poetry.core.pyproject.toml import PyProjectTOML

from poetry.installation.chef import Chef
from poetry.installation.chooser import Chooser
from poetry.installation.operations import Install
from poetry.installation.operations import Uninstall
from poetry.installation.operations import Update
from poetry.pyproject.toml import PyProjectTOML
from poetry.utils._compat import decode
from poetry.utils.authenticator import Authenticator
from poetry.utils.env import EnvCommandError
Expand Down Expand Up @@ -558,7 +558,7 @@ def _install_directory(self, operation: Install | Update) -> int:
package_poetry = None
if pyproject.is_poetry_project():
with contextlib.suppress(RuntimeError):
package_poetry = Factory().create_poetry(pyproject.file.path.parent)
package_poetry = Factory().create_poetry(pyproject.file.parent)

if package_poetry is not None:
# Even if there is a build system specified
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/installation/pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from typing import TYPE_CHECKING
from typing import Any

from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.semver.version import Version

from poetry.installation.base_installer import BaseInstaller
from poetry.pyproject.toml import PyProjectTOML
from poetry.repositories.http import HTTPRepository
from poetry.utils._compat import encode
from poetry.utils.helpers import remove_directory
Expand Down Expand Up @@ -226,7 +226,7 @@ def install_directory(self, package: Package) -> str | int:
package_poetry = None
if pyproject.is_poetry_project():
with contextlib.suppress(RuntimeError):
package_poetry = Factory().create_poetry(pyproject.file.path.parent)
package_poetry = Factory().create_poetry(pyproject.file.parent)

if package_poetry is not None:
# Even if there is a build system specified
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from typing import Any

from packaging.utils import canonicalize_name
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.utils.helpers import module_name
from tomlkit import inline_table
from tomlkit import loads
from tomlkit import table
from tomlkit.toml_document import TOMLDocument

from poetry.pyproject.toml import PyProjectTOML


if TYPE_CHECKING:
from typing import Mapping
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/masonry/builders/editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def _add_dist_info(self, added_files: list[Path]) -> None:
json.dumps(
{
"dir_info": {"editable": True},
"url": self._poetry.file.path.parent.as_uri(),
"url": self._poetry.file.parent.as_uri(),
}
)
)
Expand Down
2 changes: 2 additions & 0 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from poetry.core.version.markers import parse_marker
from poetry.core.version.requirements import InvalidRequirement

from poetry.toml.file import TOMLFile


if TYPE_CHECKING:
from poetry.core.packages.directory_dependency import DirectoryDependency
Expand Down
14 changes: 13 additions & 1 deletion src/poetry/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

from typing import TYPE_CHECKING
from typing import Any
from typing import cast

from poetry.core.poetry import Poetry as BasePoetry

from poetry.__version__ import __version__
from poetry.config.source import Source
from poetry.pyproject.toml import PyProjectTOML


if TYPE_CHECKING:
Expand All @@ -18,6 +20,7 @@
from poetry.packages.locker import Locker
from poetry.plugins.plugin_manager import PluginManager
from poetry.repositories.pool import Pool
from poetry.toml import TOMLFile


class Poetry(BasePoetry):
Expand All @@ -34,14 +37,23 @@ def __init__(
) -> None:
from poetry.repositories.pool import Pool

super().__init__(file, local_config, package)
super().__init__(file, local_config, package, pyproject_type=PyProjectTOML)

self._locker = locker
self._config = config
self._pool = Pool()
self._plugin_manager: PluginManager | None = None
self._disable_cache = disable_cache

@property
def pyproject(self) -> PyProjectTOML:
pyproject = super().pyproject
return cast(PyProjectTOML, pyproject)

@property
def toml_file(self) -> TOMLFile:
return self.pyproject.toml_file

@property
def locker(self) -> Locker:
return self._locker
Expand Down
59 changes: 59 additions & 0 deletions src/poetry/pyproject/toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from poetry.core.pyproject.toml import PyProjectTOML as BasePyProjectTOML
from tomlkit.api import table
from tomlkit.items import Table
from tomlkit.toml_document import TOMLDocument

from poetry.toml import TOMLFile


if TYPE_CHECKING:
from pathlib import Path


# Enhanced version of poetry-core's PyProjectTOML which is capable of writing
# pyproject.toml
#
# The poetry-core class uses tomli to read the file, here we use tomlkit so as to
# preserve comments and formatting when writing.
class PyProjectTOML(BasePyProjectTOML):
def __init__(self, path: str | Path) -> None:
super().__init__(path)
self._toml_file = TOMLFile(path=path)
self._toml_document: TOMLDocument | None = None

@property
def toml_file(self) -> TOMLFile:
return self._toml_file

@property
def data(self) -> TOMLDocument:
if self._toml_document is None:
if not self._file.exists():
self._toml_document = TOMLDocument()
else:
self._toml_document = self.toml_file.read()

return self._toml_document

def save(self) -> None:
data = self.data

if self._build_system is not None:
if "build-system" not in data:
data["build-system"] = table()

build_system = data["build-system"]
assert isinstance(build_system, Table)

build_system["requires"] = self._build_system.requires
build_system["build-backend"] = self._build_system.build_backend

self.toml_file.write(data=data)

def reload(self) -> None:
self._toml_document = None
self._build_system = None
7 changes: 7 additions & 0 deletions src/poetry/toml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import annotations

from poetry.toml.exceptions import TOMLError
from poetry.toml.file import TOMLFile


__all__ = ["TOMLError", "TOMLFile"]
8 changes: 8 additions & 0 deletions src/poetry/toml/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from poetry.core.exceptions import PoetryCoreException
from tomlkit.exceptions import TOMLKitError


class TOMLError(TOMLKitError, PoetryCoreException):
pass
Loading

0 comments on commit b01c5c0

Please sign in to comment.