Skip to content
This repository has been archived by the owner on Dec 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from MousaZeidBaker/tomlkit
Browse files Browse the repository at this point in the history
replace toml package with tomlkit
  • Loading branch information
MousaZeidBaker authored Sep 15, 2021
2 parents f92c261 + 026da10 commit 3f4e00c
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 46 deletions.
16 changes: 14 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poetryup"
version = "0.2.0"
version = "0.3.0"
description = "Update dependencies and bump their version in the pyproject.toml file"
authors = ["Mousa Zeid Baker"]
license = "MIT"
Expand All @@ -23,7 +23,7 @@ include = ["LICENSE"]

[tool.poetry.dependencies]
python = "^3.6"
toml = "^0.10.2"
tomlkit = "^0.7.2"

[tool.poetry.dev-dependencies]
pytest = "^6.2.5"
Expand Down
30 changes: 13 additions & 17 deletions src/poetryup/poetryup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import subprocess
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List
from typing import List

import toml
import tomlkit
from poetryup import utils
from tomlkit.toml_document import TOMLDocument


@dataclass
Expand Down Expand Up @@ -55,27 +56,27 @@ def _list_dependencies() -> List[Dependency]:
return dependencies


def _bump_versions_in_pyproject(dependencies: List[Dependency], pyproject: Dict) -> Dict:
def _bump_versions_in_pyproject(dependencies: List[Dependency], pyproject: TOMLDocument) -> TOMLDocument:
"""Bump versions in pyproject
Args:
dependencies (List[Dependency]): A list of dependencies
pyproject (Dict): The pyproject file parsed as a dictionary
pyproject (TOMLDocument): The pyproject file parsed as a TOMLDocument
Returns:
Dict: The updated pyproject dictionary
TOMLDocument: The updated pyproject
"""

for dependency in dependencies:
value = utils.lookup_nested_dict(
dictionary=pyproject["tool"]["poetry"],
value = utils.lookup_tomlkit_table(
table=pyproject["tool"]["poetry"],
key=dependency.name
)

if value.startswith(("^", "~")):
new_version = value[0] + dependency.version
utils.update_nested_dict(
dictionary=pyproject["tool"]["poetry"],
utils.update_tomlkit_table(
table=pyproject["tool"]["poetry"],
key=dependency.name,
new_value=new_version
)
Expand All @@ -94,15 +95,10 @@ def poetryup(pyproject_str: str) -> str:

_run_poetry_update()
dependencies = _list_dependencies()
pyproject_dict = toml.loads(pyproject_str)
updated_pyproject_dict = _bump_versions_in_pyproject(dependencies, pyproject_dict)
pyproject = tomlkit.loads(pyproject_str)
updated_pyproject = _bump_versions_in_pyproject(dependencies, pyproject)

# in order to preserve the order of the pyproject file, append build-system to the end
build_system = {"build-system": updated_pyproject_dict.pop("build-system")}
updated_pyproject_str = toml.dumps(updated_pyproject_dict)
updated_pyproject_str += "\n" + toml.dumps(build_system)

return updated_pyproject_str
return tomlkit.dumps(updated_pyproject)


def main():
Expand Down
36 changes: 19 additions & 17 deletions src/poetryup/utils.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
#!/usr/bin/env python

from typing import Any, Dict
from typing import Any

from tomlkit.container import Table

def lookup_nested_dict(dictionary: Dict, key: str) -> Any:
"""Lookup value recursively in nested dictionary

def lookup_tomlkit_table(table: Table, key: str) -> Any:
"""Lookup value recursively in a tomlkit Table
Args:
dictionary (Dict): The dictionary to search in
table (Table): The table to search in
key (str): The key to search for
Returns:
Any: The value of the key if found, None otherwise
"""

if key in dictionary:
return dictionary[key]
if key in table:
return table[key]

for value in dictionary.values():
if type(value) is dict:
lookup = lookup_nested_dict(value, key)
for value in table.values():
if type(value) is Table:
lookup = lookup_tomlkit_table(table=value, key=key)
if lookup is not None:
return lookup

return None


def update_nested_dict(dictionary: Dict, key: str, new_value: Any) -> bool:
"""Update value in nested dictionary
def update_tomlkit_table(table: Table, key: str, new_value: Any) -> bool:
"""Update value in a tomlkit Table
Args:
dictionary (Dict): The dictionary to search in
table (Table): The table to search in
key (str): The key to search for
new_value (Any): The new value
Returns:
Any: True if key found and value updated, False otherwise
"""

if key in dictionary:
dictionary[key] = new_value
if key in table:
table[key] = new_value
return True

for value in dictionary.values():
if type(value) is dict:
updated = update_nested_dict(dictionary=value, key=key, new_value=new_value)
for value in table.values():
if type(value) is Table:
updated = update_tomlkit_table(table=value, key=key, new_value=new_value)
if updated is True:
return True

Expand Down
21 changes: 19 additions & 2 deletions tests/unit/fixtures/expected_pyproject/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,31 @@
name = "test-poetryup"
version = "0.1.0"
description = "Test PoetryUp"
authors = [ "Mousa Zeid Baker",]
authors = ["Mousa Zeid Baker"]
packages = [
{ include = "test_poetryup", from = "src" }
]
license = "MIT"
readme = "README.md"
homepage = "https://github.com/MousaZeidBaker/poetryup"
repository = "https://github.com/MousaZeidBaker/poetryup"
keywords=[
"packaging",
"dependency",
"poetry",
"poetryup",
]
include = ["LICENSE"]

[tool.poetry.dependencies]
python = "^3.6"
poetryup = "^0.2.0"

[tool.poetry.dev-dependencies]

[tool.poetry.scripts]
poetryup = "src.test_poetryup.test_poetryup:main"

[build-system]
requires = [ "poetry-core>=1.0.0",]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@
name = "test-poetryup"
version = "0.1.0"
description = "Test PoetryUp"
authors = [ "Mousa Zeid Baker",]
authors = ["Mousa Zeid Baker"]
packages = [
{ include = "test_poetryup", from = "src" }
]
license = "MIT"
readme = "README.md"
homepage = "https://github.com/MousaZeidBaker/poetryup"
repository = "https://github.com/MousaZeidBaker/poetryup"
keywords=[
"packaging",
"dependency",
"poetry",
"poetryup",
]
include = ["LICENSE"]

[tool.poetry.dependencies]
python = "^3.6"
Expand All @@ -12,6 +26,9 @@ poetryup = "^0.2.0"

[tool.poetry.group.dev.dependencies]

[tool.poetry.scripts]
poetryup = "src.test_poetryup.test_poetryup:main"

[build-system]
requires = [ "poetry-core>=1.0.0",]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
21 changes: 19 additions & 2 deletions tests/unit/fixtures/input_pyproject/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,31 @@
name = "test-poetryup"
version = "0.1.0"
description = "Test PoetryUp"
authors = [ "Mousa Zeid Baker",]
authors = ["Mousa Zeid Baker"]
packages = [
{ include = "test_poetryup", from = "src" }
]
license = "MIT"
readme = "README.md"
homepage = "https://github.com/MousaZeidBaker/poetryup"
repository = "https://github.com/MousaZeidBaker/poetryup"
keywords=[
"packaging",
"dependency",
"poetry",
"poetryup",
]
include = ["LICENSE"]

[tool.poetry.dependencies]
python = "^3.6"
poetryup = "^0.1.0"

[tool.poetry.dev-dependencies]

[tool.poetry.scripts]
poetryup = "src.test_poetryup.test_poetryup:main"

[build-system]
requires = [ "poetry-core>=1.0.0",]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@
name = "test-poetryup"
version = "0.1.0"
description = "Test PoetryUp"
authors = [ "Mousa Zeid Baker",]
authors = ["Mousa Zeid Baker"]
packages = [
{ include = "test_poetryup", from = "src" }
]
license = "MIT"
readme = "README.md"
homepage = "https://github.com/MousaZeidBaker/poetryup"
repository = "https://github.com/MousaZeidBaker/poetryup"
keywords=[
"packaging",
"dependency",
"poetry",
"poetryup",
]
include = ["LICENSE"]

[tool.poetry.dependencies]
python = "^3.6"
Expand All @@ -12,6 +26,9 @@ poetryup = "^0.1.0"

[tool.poetry.group.dev.dependencies]

[tool.poetry.scripts]
poetryup = "src.test_poetryup.test_poetryup:main"

[build-system]
requires = [ "poetry-core>=1.0.0",]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

0 comments on commit 3f4e00c

Please sign in to comment.