From d08aff8dd1dc7d1ac6cc36c5f63e985b4be03759 Mon Sep 17 00:00:00 2001 From: truePhilipp Date: Thu, 2 May 2024 21:33:46 +0200 Subject: [PATCH] renamed files; implemented command_entry; wrote real test and added test file structure --- .gitignore | 3 ++ html_to_django/converter.py | 35 +++++++++++++++++++ html_to_django/main.py | 18 ---------- pyproject.toml | 2 +- tests/test_main.py | 8 ----- tests/{ => test_project}/test.html | 0 .../test_project_expected_result/test.n.html | 22 ++++++++++++ tests/tests_converter.py | 21 +++++++++++ 8 files changed, 82 insertions(+), 27 deletions(-) create mode 100644 html_to_django/converter.py delete mode 100644 html_to_django/main.py delete mode 100644 tests/test_main.py rename tests/{ => test_project}/test.html (100%) create mode 100644 tests/test_project_expected_result/test.n.html create mode 100644 tests/tests_converter.py diff --git a/.gitignore b/.gitignore index 0b01067..ab816e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Created by https://www.toptal.com/developers/gitignore/api/python,pycharm+all,macos # Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm+all,macos +### Custom ### +tests/test_project_test_result + ### macOS ### # General .DS_Store diff --git a/html_to_django/converter.py b/html_to_django/converter.py new file mode 100644 index 0000000..65a4c28 --- /dev/null +++ b/html_to_django/converter.py @@ -0,0 +1,35 @@ +import bs4 +import sys +import os +from glob import glob +from .formatters import formatters + + +def convert_file(path: str, overwrite: bool = False) -> None: + with open(path, "r") as file: + soup = bs4.BeautifulSoup(file, "html.parser") + + for formatter in formatters: + formatter.format(soup) + + if overwrite: + out_path = path + else: + out_path = path.replace(".html", ".n.html") + with open(out_path, "w") as file: + file.write("{% load static %}\n") + # indent=4 will be re added when https://github.com/python/typeshed/pull/11855 is accepted + file.write(soup.prettify(formatter=bs4.formatter.HTMLFormatter())) + + +def convert_dir(path: str, overwrite: bool = False) -> None: + for filepath in glob(f"{path}/**/*.html", recursive=True): + convert_file(filepath, overwrite) + + +def command_entry() -> None: + path = os.path.abspath(sys.argv[1]) + if os.path.isdir(path): + convert_dir(path) + elif os.path.isfile(path): + convert_file(path) diff --git a/html_to_django/main.py b/html_to_django/main.py deleted file mode 100644 index 8c29664..0000000 --- a/html_to_django/main.py +++ /dev/null @@ -1,18 +0,0 @@ -import bs4 -from .formatters import formatters - - -def main() -> None: - with open("test.html", "r") as file: - soup = bs4.BeautifulSoup(file, "html.parser") - - for formatter in formatters: - formatter.format(soup) - - with open("out.html", "w") as file: - file.write("{% load static %}\n") - file.write(soup.prettify(formatter=bs4.formatter.HTMLFormatter(indent=4))) - - -def command_entry() -> None: - print("ok") diff --git a/pyproject.toml b/pyproject.toml index f8e525f..4a9aaca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,7 +37,7 @@ Homepage = "" Issues = "" [project.scripts] -html_to_django = "html_to_django.main:command_entry" +html_to_django = "html_to_django.converter:command_entry" [tool.hatch.version] path = "html_to_django/__about__.py" \ No newline at end of file diff --git a/tests/test_main.py b/tests/test_main.py deleted file mode 100644 index f27e5dd..0000000 --- a/tests/test_main.py +++ /dev/null @@ -1,8 +0,0 @@ -from html_to_django import main -from unittest import TestCase - - -class TestTocHTMLParser(TestCase): - def test_command_entry(self): - main.command_entry() - self.assertEqual(0, 0) diff --git a/tests/test.html b/tests/test_project/test.html similarity index 100% rename from tests/test.html rename to tests/test_project/test.html diff --git a/tests/test_project_expected_result/test.n.html b/tests/test_project_expected_result/test.n.html new file mode 100644 index 0000000..29a862c --- /dev/null +++ b/tests/test_project_expected_result/test.n.html @@ -0,0 +1,22 @@ +{% load static %} +{% block content %} +{% for i in items %} +

+ {{ i.text }} +

+{% if i.selectable %} +

+ Select Me +

+{% elif i.other %} +

+ Else if +

+{% else %} +

+ Else +

+ +{% endif %} +{% endfor %} +{% endblock %} diff --git a/tests/tests_converter.py b/tests/tests_converter.py new file mode 100644 index 0000000..a18a505 --- /dev/null +++ b/tests/tests_converter.py @@ -0,0 +1,21 @@ +import os +import filecmp +import shutil +from unittest import TestCase +from html_to_django import converter + + +class ConverterTests(TestCase): + def test_convert_file(self): + shutil.copytree( + os.path.abspath("tests/test_project"), + os.path.abspath("tests/test_project_test_result"), + dirs_exist_ok=True + ) + converter.convert_file( + os.path.abspath("tests/test_project_test_result/test.html") + ) + self.assertTrue(filecmp.cmp( + os.path.abspath("tests/test_project_test_result/test.n.html"), + os.path.abspath("tests/test_project_expected_result/test.n.html") + ))