file_config_source.py (poetry-1.1.15) | : | file_config_source.py (poetry-1.2.0) | ||
---|---|---|---|---|
from __future__ import annotations | ||||
from contextlib import contextmanager | from contextlib import contextmanager | |||
from typing import TYPE_CHECKING | from typing import TYPE_CHECKING | |||
from typing import Any | from typing import Any | |||
from tomlkit import document | from tomlkit import document | |||
from tomlkit import table | from tomlkit import table | |||
from .config_source import ConfigSource | from poetry.config.config_source import ConfigSource | |||
if TYPE_CHECKING: | if TYPE_CHECKING: | |||
from poetry.core.toml.file import TOMLFile # noqa | from collections.abc import Iterator | |||
from poetry.core.toml.file import TOMLFile | ||||
from tomlkit.toml_document import TOMLDocument | ||||
class FileConfigSource(ConfigSource): | class FileConfigSource(ConfigSource): | |||
def __init__(self, file, auth_config=False): # type: ("TOMLFile", bool) -> None | def __init__(self, file: TOMLFile, auth_config: bool = False) -> None: | |||
self._file = file | self._file = file | |||
self._auth_config = auth_config | self._auth_config = auth_config | |||
@property | @property | |||
def name(self): # type: () -> str | def name(self) -> str: | |||
return str(self._file.path) | return str(self._file.path) | |||
@property | @property | |||
def file(self): # type: () -> "TOMLFile" | def file(self) -> TOMLFile: | |||
return self._file | return self._file | |||
def add_property(self, key, value): # type: (str, Any) -> None | def add_property(self, key: str, value: Any) -> None: | |||
with self.secure() as config: | with self.secure() as toml: | |||
config: dict[str, Any] = toml | ||||
keys = key.split(".") | keys = key.split(".") | |||
for i, key in enumerate(keys): | for i, key in enumerate(keys): | |||
if key not in config and i < len(keys) - 1: | if key not in config and i < len(keys) - 1: | |||
config[key] = table() | config[key] = table() | |||
if i == len(keys) - 1: | if i == len(keys) - 1: | |||
config[key] = value | config[key] = value | |||
break | break | |||
config = config[key] | config = config[key] | |||
def remove_property(self, key): # type: (str) -> None | def remove_property(self, key: str) -> None: | |||
with self.secure() as config: | with self.secure() as toml: | |||
config: dict[str, Any] = toml | ||||
keys = key.split(".") | keys = key.split(".") | |||
current_config = config | current_config = config | |||
for i, key in enumerate(keys): | for i, key in enumerate(keys): | |||
if key not in current_config: | if key not in current_config: | |||
return | return | |||
if i == len(keys) - 1: | if i == len(keys) - 1: | |||
del current_config[key] | del current_config[key] | |||
break | break | |||
current_config = current_config[key] | current_config = current_config[key] | |||
@contextmanager | @contextmanager | |||
def secure(self): | def secure(self) -> Iterator[TOMLDocument]: | |||
if self.file.exists(): | if self.file.exists(): | |||
initial_config = self.file.read() | initial_config = self.file.read() | |||
config = self.file.read() | config = self.file.read() | |||
else: | else: | |||
initial_config = document() | initial_config = document() | |||
config = document() | config = document() | |||
new_file = not self.file.exists() | new_file = not self.file.exists() | |||
yield config | yield config | |||
End of changes. 9 change blocks. | ||||
10 lines changed or deleted | 17 lines changed or added |