Skip to content

Commit

Permalink
refactor: move duplicated source_to_table to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner authored and branchvincent committed Jun 15, 2022
1 parent bed427d commit 993519f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
21 changes: 3 additions & 18 deletions src/poetry/console/commands/source/add.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cleo.helpers import argument
from cleo.helpers import option
from cleo.io.null_io import NullIO
from tomlkit import nl
from tomlkit import table
from tomlkit.items import AoT

from poetry.config.source import Source
from poetry.console.commands.command import Command


if TYPE_CHECKING:
from tomlkit.items import Table


class SourceAddCommand(Command):

name = "source add"
Expand All @@ -41,17 +33,10 @@ class SourceAddCommand(Command):
option("secondary", "s", "Set this source as secondary."),
]

@staticmethod
def source_to_table(source: Source) -> Table:
source_table: Table = table()
for key, value in source.to_dict().items():
source_table.add(key, value)
source_table.add(nl())
return source_table

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

name = self.argument("name")
url = self.argument("url")
Expand Down Expand Up @@ -92,11 +77,11 @@ def handle(self) -> int:
source = new_source
new_source = None

sources.append(self.source_to_table(source))
sources.append(source_to_table(source))

if new_source is not None:
self.line(f"Adding source with name <c1>{name}</c1>.")
sources.append(self.source_to_table(new_source))
sources.append(source_to_table(new_source))

# ensure new source is valid. eg: invalid name etc.
self.poetry._pool = Pool()
Expand Down
22 changes: 3 additions & 19 deletions src/poetry/console/commands/source/remove.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cleo.helpers import argument
from tomlkit import nl
from tomlkit import table
from tomlkit.items import AoT

from poetry.console.commands.command import Command


if TYPE_CHECKING:
from tomlkit.items import Table

from poetry.config.source import Source


class SourceRemoveCommand(Command):

name = "source remove"
Expand All @@ -28,15 +18,9 @@ class SourceRemoveCommand(Command):
),
]

@staticmethod
def source_to_table(source: Source) -> Table:
source_table: Table = table()
for key, value in source.to_dict().items():
source_table.add(key, value)
source_table.add(nl())
return source_table

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

name = self.argument("name")

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

if not removed:
self.line_error(
Expand Down
20 changes: 20 additions & 0 deletions src/poetry/utils/source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import annotations

from typing import TYPE_CHECKING


if TYPE_CHECKING:
from tomlkit.items import Table

from poetry.config.source import Source


def source_to_table(source: Source) -> Table:
from tomlkit import nl
from tomlkit import table

source_table: Table = table()
for key, value in source.to_dict().items():
source_table.add(key, value)
source_table.add(nl())
return source_table
40 changes: 40 additions & 0 deletions tests/utils/test_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import annotations

import pytest

from tomlkit.container import Container
from tomlkit.items import Table
from tomlkit.items import Trivia

from poetry.config.source import Source
from poetry.utils.source import source_to_table


@pytest.mark.parametrize(
"source,table_body",
[
(
Source("foo", "https://example.com"),
{
"default": False,
"name": "foo",
"secondary": False,
"url": "https://example.com",
},
),
(
Source("bar", "https://example.com/bar", True, True),
{
"default": True,
"name": "bar",
"secondary": True,
"url": "https://example.com/bar",
},
),
],
)
def test_source_to_table(source: Source, table_body: dict[str, str | bool]):
table = Table(Container(), Trivia(), False)
table._value = table_body

assert source_to_table(source) == table

0 comments on commit 993519f

Please sign in to comment.