djLint/src/djlint/__init__.py

154 lines
4.1 KiB
Python
Raw Normal View History

2021-07-12 18:26:46 +00:00
#!/usr/bin/python
"""Check Django template syntax.
usage::
djlint INPUT -e <extension>
"""
import os
import re
import sys
from concurrent.futures import ProcessPoolExecutor
from pathlib import Path
import click
import yaml
from click import echo
from colorama import Fore, Style, deinit, init
rules = yaml.load(
(Path(__file__).parent / "rules.yaml").read_text(encoding="utf8"),
Loader=yaml.SafeLoader,
)
def lint_file(this_file: Path):
"""Check file for formatting errors."""
file_name = str(this_file)
errors: dict = {file_name: []}
html = this_file.read_text(encoding="utf8")
# build list of line ends for file
2021-07-13 15:45:57 +00:00
line_ends = [m.end() for m in re.finditer(r"(?:.*\n)|(?:[^\n]+$)", html)]
2021-07-12 18:26:46 +00:00
def get_line(start):
"""Get the line number and index of match."""
for index, value in enumerate(line_ends):
2021-07-13 15:45:57 +00:00
if value > start:
2021-07-12 18:26:46 +00:00
line_start_index = (line_ends[index - 1] if index > 0 else 0) - 1
return "%d:%d" % (index + 1, start - line_start_index)
return "error"
for rule in rules:
rule = rule["rule"]
for pattern in rule["patterns"]:
for match in re.finditer(pattern, html, re.DOTALL):
errors[file_name].append(
{
"code": rule["name"],
"line": get_line(match.start()),
2021-07-12 22:07:52 +00:00
"match": match.group()[:20].strip(),
2021-07-12 18:26:46 +00:00
"message": rule["message"],
}
)
return errors
def get_src(src: Path, extension=None):
"""Get source files."""
if Path.is_file(src):
return [src]
# remove leading . from extension
extension = extension[1:] if extension.startswith(".") else extension
paths = list(src.glob(r"**/*.%s" % extension))
if len(paths) == 0:
2021-07-13 15:45:57 +00:00
echo(Fore.BLUE + "No files to lint! 😢")
2021-07-12 18:26:46 +00:00
return []
return paths
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@click.argument(
"src",
type=click.Path(
exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
),
nargs=1,
metavar="SRC ...",
)
@click.option(
"-e",
"--extension",
type=str,
default="html",
help="File extension to lint",
show_default=True,
)
def main(src: str, extension: str):
"""Djlint django template files."""
file_list = get_src(Path(src), extension)
if len(file_list) == 0:
return
2021-07-12 19:40:08 +00:00
file_quantity = "%d file%s" % (len(file_list), ("s" if len(file_list) > 1 else ""))
echo("\nChecking %s!" % file_quantity)
2021-07-12 18:26:46 +00:00
worker_count = os.cpu_count()
if sys.platform == "win32":
# Work around https://bugs.python.org/issue26903
worker_count = min(worker_count, 60)
with ProcessPoolExecutor(max_workers=worker_count) as exe:
file_errors = exe.map(lint_file, file_list)
# format errors
2021-07-12 19:40:08 +00:00
error_count = 0
2021-07-12 18:26:46 +00:00
for error in file_errors:
for this_file, errors in error.items():
if errors:
echo(
"{}\n{}\n{}===============================".format(
2021-07-12 18:31:44 +00:00
Fore.GREEN + Style.BRIGHT, this_file, Style.DIM
2021-07-12 18:26:46 +00:00
)
+ Style.RESET_ALL
)
2021-07-12 19:40:08 +00:00
error_count += len(errors)
2021-07-13 15:45:57 +00:00
for message in sorted(
errors, key=lambda x: int(x["line"].split(":")[0])
):
2021-07-12 18:26:46 +00:00
error = bool(message["code"][:1] == "E")
echo(
"{} {} {} {} {}".format(
(Fore.RED if error else Fore.YELLOW),
message["code"] + Style.RESET_ALL,
Fore.BLUE + message["line"] + Style.RESET_ALL,
message["message"],
Fore.BLUE + message["match"],
),
2021-07-12 20:26:14 +00:00
err=False,
2021-07-12 18:26:46 +00:00
)
2021-07-12 19:40:08 +00:00
success_message = "Checked %s, found %d errors." % (
file_quantity,
error_count,
)
2021-07-12 18:26:46 +00:00
2021-07-12 19:40:08 +00:00
echo("\n%s\n" % (success_message))
2021-07-12 18:26:46 +00:00
if __name__ == "__main__":
init(autoreset=True)
main()
deinit()