-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from villainy/toml
Add optional TOML configuration support
- Loading branch information
Showing
8 changed files
with
218 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Pylama TOML configuration.""" | ||
|
||
import toml | ||
|
||
from pylama.libs.inirama import Namespace as _Namespace | ||
|
||
|
||
class Namespace(_Namespace): | ||
"""Inirama-style wrapper for TOML config.""" | ||
|
||
def parse(self, source: str, update: bool = True, **params): | ||
"""Parse TOML source as string.""" | ||
content = toml.loads(source) | ||
tool = content.get("tool", {}) | ||
pylama = tool.get("pylama", {}) | ||
linters = pylama.pop("linter", {}) | ||
files = pylama.pop("files", []) | ||
|
||
for name, value in pylama.items(): | ||
self["pylama"][name] = value | ||
|
||
for linter, options in linters.items(): | ||
for name, value in options.items(): | ||
self[f"pylama:{linter}"][name] = value | ||
|
||
for file in files: | ||
path = file.pop("path", None) | ||
if path is None: | ||
continue | ||
for name, value in file.items(): | ||
self[f"pylama:{path}"][name] = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ radon >= 5.1.0 | |
mypy | ||
pylint >= 2.11.1 | ||
pylama-quotes | ||
toml | ||
vulture | ||
|
||
types-setuptools | ||
types-toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""Test TOML config handling.""" | ||
|
||
from unittest import mock | ||
|
||
from pylama import config_toml | ||
from pylama.config import DEFAULT_SECTION | ||
from pylama.libs import inirama | ||
|
||
CONFIG_TOML = """ | ||
[tool.pylama] | ||
async = 1 | ||
ignore = "D203,D213,F0401,C0111,E731,I0011" | ||
linters = "pycodestyle,pyflakes,mccabe,pydocstyle,pylint,mypy" | ||
skip = "pylama/inirama.py,pylama/libs/*" | ||
verbose = 0 | ||
max_line_length = 100 | ||
[tool.pylama.linter.pyflakes] | ||
builtins = "_" | ||
[tool.pylama.linter.pylint] | ||
ignore = "R,E1002,W0511,C0103,C0204" | ||
[[tool.pylama.files]] | ||
path = "pylama/core.py" | ||
ignore = "C901,R0914" | ||
[[tool.pylama.files]] | ||
path = "pylama/main.py" | ||
ignore = "R0914,W0212,C901,E1103" | ||
[[tool.pylama.files]] | ||
path = "tests/*" | ||
ignore = "D,C,W,E1103" | ||
""" | ||
|
||
CONFIG_INI=""" | ||
[pylama] | ||
async = 1 | ||
ignore = D203,D213,F0401,C0111,E731,I0011 | ||
linters = pycodestyle,pyflakes,mccabe,pydocstyle,pylint,mypy | ||
skip = pylama/inirama.py,pylama/libs/* | ||
verbose = 0 | ||
max_line_length = 100 | ||
[pylama:pyflakes] | ||
builtins = _ | ||
[pylama:pylint] | ||
ignore=R,E1002,W0511,C0103,C0204 | ||
[pylama:pylama/core.py] | ||
ignore = C901,R0914 | ||
[pylama:pylama/main.py] | ||
ignore = R0914,W0212,C901,E1103 | ||
[pylama:tests/*] | ||
ignore = D,C,W,E1103 | ||
""" | ||
|
||
def test_toml_parsing_matches_ini(): | ||
"""Ensure the parsed TOML namepsace matches INI parsing.""" | ||
with mock.patch("pylama.libs.inirama.io.open", mock.mock_open(read_data=CONFIG_INI)): | ||
ini = inirama.Namespace() | ||
ini.default_section = DEFAULT_SECTION | ||
ini.read("ini") | ||
|
||
with mock.patch("pylama.libs.inirama.io.open", mock.mock_open(read_data=CONFIG_TOML)): | ||
toml = config_toml.Namespace() | ||
toml.default_section = DEFAULT_SECTION | ||
toml.read("toml") | ||
|
||
assert ini.sections == toml.sections |