Skip to content

Commit

Permalink
Move source_to_table to Source class
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus committed Sep 2, 2024
1 parent f843cee commit 867bc74
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
13 changes: 13 additions & 0 deletions src/poetry/config/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import dataclasses
import warnings
from typing import TYPE_CHECKING

from poetry.repositories.repository_pool import Priority

if TYPE_CHECKING:
from tomlkit.items import Table

@dataclasses.dataclass(order=True, eq=True)
class Source:
Expand Down Expand Up @@ -41,3 +44,13 @@ def to_dict(self) -> dict[str, str | bool]:
if v
},
)

def to_toml_table(self) -> Table:
from tomlkit import nl
from tomlkit import table

source_table: Table = table()
for key, value in self.to_dict().items():
source_table.add(key, value)
source_table.add(nl())
return source_table
5 changes: 2 additions & 3 deletions src/poetry/console/commands/source/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class SourceAddCommand(Command):

def handle(self) -> int:
from poetry.factory import Factory
from poetry.utils.source import source_to_table

name: str = self.argument("name")
lower_name = name.lower()
Expand Down Expand Up @@ -147,11 +146,11 @@ def handle(self) -> int:
source = new_source
is_new_source = False

sources.append(source_to_table(source))
sources.append(source.to_toml_table())

if is_new_source:
self.line(f"Adding source with name <c1>{name}</c1>.")
sources.append(source_to_table(new_source))
sources.append(new_source.to_toml_table())
else:
self.line(f"Source with name <c1>{name}</c1> already exists. Updating.")

Expand Down
4 changes: 1 addition & 3 deletions src/poetry/console/commands/source/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class SourceRemoveCommand(Command):
]

def handle(self) -> int:
from poetry.utils.source import source_to_table

name = self.argument("name")
lower_name = name.lower()

Expand All @@ -38,7 +36,7 @@ def handle(self) -> int:
self.line(f"Removing source with name <c1>{source.name}</c1>.")
removed = True
continue
sources.append(source_to_table(source))
sources.append(source.to_toml_table())

if not removed:
self.line_error(
Expand Down
Empty file removed src/poetry/utils/setup_reader.py
Empty file.
20 changes: 0 additions & 20 deletions src/poetry/utils/source.py

This file was deleted.

3 changes: 1 addition & 2 deletions tests/utils/test_source.py → tests/config/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from poetry.config.source import Source
from poetry.repositories.repository_pool import Priority
from poetry.utils.source import source_to_table


@pytest.mark.parametrize(
Expand Down Expand Up @@ -36,7 +35,7 @@ def test_source_to_table(source: Source, table_body: dict[str, str | bool]) -> N
table = Table(Container(), Trivia(), False)
table._value = table_body # type: ignore[assignment]

assert source_to_table(source) == table
assert source.to_toml_table() == table


def test_source_default_is_primary() -> None:
Expand Down

0 comments on commit 867bc74

Please sign in to comment.