Skip to content

Commit

Permalink
Add TOML configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
villainy committed Jul 1, 2022
1 parent 10e55b1 commit 11f7c9c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
41 changes: 33 additions & 8 deletions pylama/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
from pylama.libs import inirama
from pylama.lint import LINTERS

try:
from pylama import config_toml
CONFIG_FILES = ["pylama.ini", "pyproject.toml", "setup.cfg", "tox.ini", "pytest.ini"]
except ImportError:
CONFIG_FILES = ["pylama.ini", "setup.cfg", "tox.ini", "pytest.ini"]


#: A default checkers
DEFAULT_LINTERS = "pycodestyle", "pyflakes", "mccabe"

CURDIR = Path.cwd()
HOMECFG = Path.home() / ".pylama.ini"
CONFIG_FILES = "pylama.ini", "setup.cfg", "tox.ini", "pytest.ini"
DEFAULT_SECTION = "pylama"

# Setup a logger
LOGGER.propagate = False
Expand Down Expand Up @@ -253,18 +260,36 @@ def process_value(actions: Dict, name: str, value: Any) -> Any:
return value


def get_config(ini_path: str = None, rootdir: Path = None) -> inirama.Namespace:
"""Load configuration from INI."""
config = inirama.Namespace()
config.default_section = "pylama"

cfg_path = ini_path or get_default_config_file(rootdir)
def get_config(user_path: str = None, rootdir: Path = None) -> inirama.Namespace:
"""Load configuration from files."""
cfg_path = user_path or get_default_config_file(rootdir)
if not cfg_path and HOMECFG.exists():
cfg_path = HOMECFG.as_posix()

if cfg_path:
LOGGER.info("Read config: %s", cfg_path)
config.read(cfg_path)
if cfg_path.endswith(".toml"):
return get_config_toml(cfg_path)
else:
return get_config_ini(cfg_path)

return inirama.Namespace()


def get_config_ini(ini_path: str) -> inirama.Namespace:
"""Load configuration from INI."""
config = inirama.Namespace()
config.default_section = DEFAULT_SECTION
config.read(ini_path)

return config


def get_config_toml(toml_path: str) -> inirama.Namespace:
"""Load configuration from TOML."""
config = config_toml.Namespace()
config.default_section = DEFAULT_SECTION
config.read(toml_path)

return config

Expand Down
31 changes: 31 additions & 0 deletions pylama/config_toml.py
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
2 changes: 1 addition & 1 deletion pylama/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def run(
sorter = default_sorter
if options and options.sort:
sort = options.sort
sorter = lambda err: (sort.get(err.etype, 999), err.lnum)
sorter = lambda err: (sort.get(err.etype, 999), err.lnum) # pylint: disable=C3001

return sorted(errors, key=sorter)

Expand Down
2 changes: 2 additions & 0 deletions requirements/requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ radon >= 5.1.0
mypy
pylint >= 2.11.1
pylama-quotes
toml
vulture

types-setuptools
types-toml
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def parse_requirements(path: str) -> "list[str]":
install_requires=parse_requirements("requirements/requirements.txt"),
extras_require=dict(
tests=parse_requirements("requirements/requirements-tests.txt"),
all=OPTIONAL_LINTERS, **{linter: [linter] for linter in OPTIONAL_LINTERS}
all=OPTIONAL_LINTERS, **{linter: [linter] for linter in OPTIONAL_LINTERS},
toml="toml>=0.10.2",
),
)

0 comments on commit 11f7c9c

Please sign in to comment.