Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Handle pyproject.toml as --configuration argument #759

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/_includes/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Options:
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration PATH Path to global configuration file in
.djlintrc format
--configuration PATH Path to global configuration file.
.djlintrc OR pyproject.toml formats are valid
--statistics Count the number of occurrences of each
error/warning code.
--include TEXT Codes to include. ex: "H014,H017"
Expand Down
34 changes: 23 additions & 11 deletions src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,29 @@ def find_djlint_rules(root: Path) -> Optional[Path]:
return None


def load_pyproject_config(filepath: Path) -> Dict:
"""Load djlint config from pyproject.toml"""
data = tomllib.loads(filepath.resolve().read_text(encoding="utf-8"))
return data.get("tool", {}).get("djlint", {})


def load_djlintrc_config(filepath: Path) -> Dict:
"""Load djlint config from .djlintrc"""
return json.loads(filepath.resolve().read_text(encoding="utf-8"))


def load_project_settings(src: Path, config: Optional[str]) -> Dict:
"""Load djlint config from pyproject.toml."""

djlint_content: Dict = {}

if config:
try:
djlint_content = json.loads(
Path(config).resolve().read_text(encoding="utf8")
)
path = Path(config)
if path.name == "pyproject.toml":
djlint_content.update(load_pyproject_config(path))
else:
djlint_content.update(load_djlintrc_config(path))

# pylint: disable=broad-except
except BaseException as error:
Expand All @@ -118,20 +131,19 @@ def load_project_settings(src: Path, config: Optional[str]) -> Dict:
pyproject_file = find_pyproject(src)

if pyproject_file:
content = tomllib.loads(pyproject_file.read_text(encoding="utf8"))
try:
return {**djlint_content, **content["tool"]["djlint"]} # type: ignore
except KeyError:
content = load_pyproject_config(pyproject_file)
if content != {}:
djlint_content.update(content)
return content
else:
logger.info("No pyproject.toml found.")

djlintrc_file = find_djlintrc(src)

if djlintrc_file:
try:
return {
**djlint_content,
**json.loads(djlintrc_file.read_text(encoding="utf8")),
}
djlint_content.update(load_djlintrc_config(djlintrc_file))
return content
# pylint: disable=broad-except
except BaseException as error:
logger.error("%sFailed to load .djlintrc file. %s", Fore.RED, error)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tool.djlint]
profile = "django"
indent = 2
ignore = ""
files = [
"./tests/test_config/test_config_pyproject/test.html",
"./tests/test_config/test_config_pyproject/test_two.html"
]
3 changes: 3 additions & 0 deletions tests/test_config/test_config_pyproject/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<p></p>
</div>
27 changes: 27 additions & 0 deletions tests/test_config/test_config_pyproject/test_config_pyproject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Djlint tests specific to custom file path.

run::

pytest tests/test_config/test_config_pyproject/test.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing

pytest tests/test_config/test_config_pyproject/test.py::test_check_pyproject_as_config

"""

from click.testing import CliRunner

from src.djlint import main as djlint


def test_check_pyproject_as_config(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
[
"-",
"--check",
"--configuration",
"tests/test_config/test_config_pyproject/subfolder/pyproject.toml",
],
)
assert """Checking 2/2 files""" in result.output
1 change: 1 addition & 0 deletions tests/test_config/test_config_pyproject/test_two.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>