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
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 get_sources(self) -> List[Source]: return [ Source(**source) for source in self.pyproject.poetry_config.get("source", []) ]
def source_secondary() -> Source: return Source(name="secondary", url="https://secondary.com", secondary=True)
def source_default() -> Source: return Source(name="default", url="https://default.com", default=True)
def source_two() -> Source: return Source(name="two", url="https://two.com")
def source_one() -> Source: return Source(name="one", url="https://one.com")
@pytest.fixture def source_two() -> Source: return Source(name="two", url="https://two.com") @pytest.fixture def source_default() -> Source: return Source(name="default", url="https://default.com", default=True) @pytest.fixture def source_secondary() -> Source: return Source(name="secondary", url="https://secondary.com", secondary=True) _existing_source = Source(name="existing", url="https://existing.com") @pytest.fixture def source_existing() -> Source: return _existing_source PYPROJECT_WITH_SOURCES = f""" [tool.poetry] name = "source-command-test" version = "0.1.0" description = "" authors = ["Poetry Tester <*****@*****.**>"] [tool.poetry.dependencies]
def handle(self) -> Optional[int]: from poetry.factory import Factory from poetry.repositories import Pool name = self.argument("name") url = self.argument("url") is_default = self.option("default") is_secondary = self.option("secondary") if is_default and is_secondary: self.line_error( "Cannot configure a source as both <c1>default</c1> and <c1>secondary</c1>." ) return 1 new_source = Source(name=name, url=url, default=is_default, secondary=is_secondary) existing_sources = self.poetry.get_sources() sources = AoT([]) for source in existing_sources: if source == new_source: self.line( f"Source with name <c1>{name}</c1> already exits. Skipping addition." ) return 0 elif source.default and is_default: self.line_error( f"<error>Source with name <c1>{source.name}</c1> is already set to default. " f"Only one default source can be configured at a time.</error>" ) return 1 if source.name == name: self.line( f"Source with name <c1>{name}</c1> already exits. Updating." ) source = new_source new_source = None sources.append(self.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)) # ensure new source is valid. eg: invalid name etc. self.poetry._pool = Pool() try: Factory.configure_sources(self.poetry, sources, self.poetry.config, NullIO()) self.poetry.pool.repository(name) except ValueError as e: self.line_error( f"<error>Failed to validate addition of <c1>{name}</c1>: {e}</error>" ) return 1 self.poetry.pyproject.poetry_config["source"] = sources self.poetry.pyproject.save() return 0
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", },