Skip to content

Commit

Permalink
Add TOML parsing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
br3ndonland committed Oct 24, 2020
1 parent 4f1ccf2 commit 344f3e4
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 19 deletions.
25 changes: 13 additions & 12 deletions py/algorythms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
Miscellaneous curiosities from the world of computer programming
https://github.com/br3ndonland/algorithms
"""
from importlib.metadata import version
from pathlib import Path
from typing import Any, Dict

from pydantic import BaseSettings

def package_version() -> str:
"""
Calculate version number based on pyproject.toml.
---
During `poetry install`, Poetry installs the project as if it were a package itself.
You can then pull from the package metadata.
from algorythms.examples.parse_pyproject_toml import load_pyproject

See [poetry#1036](https://github.com/python-poetry/poetry/issues/1036) and
[poetry#144](https://github.com/python-poetry/poetry/issues/144) for more info.
"""
return version(__package__)

class Settings(BaseSettings):
"""Instantiate a Pydantic Settings model."""

__version__ = package_version()
pyproject: Dict[str, Any] = load_pyproject(pyproject_path=Path("pyproject.toml"))
title: str = str(pyproject["tool"]["poetry"]["name"])
description: str = str(pyproject["tool"]["poetry"]["description"])
version: str = str(pyproject["tool"]["poetry"]["version"])


settings = Settings()
14 changes: 14 additions & 0 deletions py/algorythms/examples/parse_pyproject_toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pathlib import Path
from typing import Any, Dict

import toml


def load_pyproject(pyproject_path: Path = Path("pyproject.toml")) -> Dict[str, Any]:
"""Load pyproject.toml into a dictionary, with a default dict as a fallback."""
try:
return dict(toml.load(pyproject_path))
except Exception:
return {
"tool": {"poetry": {"name": "algorythms", "description": "", "version": ""}}
}
1 change: 1 addition & 0 deletions py/mypy.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[mypy]
disallow_untyped_defs = True
files = **/*.py
plugins = pydantic.mypy
36 changes: 34 additions & 2 deletions py/poetry.lock

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

2 changes: 2 additions & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ include = ["algorythms/py.typed"]

[tool.poetry.dependencies]
python = "^3.8"
pydantic = "^1.6"
toml = "^0.10"

[tool.poetry.dev-dependencies]
black = { version = "*", allow-prereleases = true }
Expand Down
23 changes: 23 additions & 0 deletions py/tests/examples/test_parse_pyproject_toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pathlib import Path

from algorythms import settings
from algorythms.examples.parse_pyproject_toml import load_pyproject


def test_load_pyproject() -> None:
"""Assert that pyproject.toml is successfully loaded and parsed."""
pyproject = load_pyproject(pyproject_path=Path("pyproject.toml"))
assert pyproject == settings.pyproject
assert str(pyproject["tool"]["poetry"]["name"]) == settings.title
assert str(pyproject["tool"]["poetry"]["description"]) == settings.description
assert str(pyproject["tool"]["poetry"]["version"]) == settings.version


def test_load_pyproject_error() -> None:
"""Assert that default dict is loaded when pyproject.toml is not found."""
pyproject = load_pyproject(pyproject_path=Path("pyproject.toml"))
pyproject_default = load_pyproject(pyproject_path=Path("error"))
assert pyproject != pyproject_default
assert pyproject_default == {
"tool": {"poetry": {"name": "algorythms", "description": "", "version": ""}}
}
5 changes: 0 additions & 5 deletions py/tests/test_version.py

This file was deleted.

0 comments on commit 344f3e4

Please sign in to comment.