Coverage for /usr/lib/python3.10/site-packages/hyd/backend/util/injection.py: 21%

29 statements  

« prev     ^ index     » next       coverage.py v7.0.3, created at 2023-02-05 02:26 +0000

1import os 

2from pathlib import Path 

3 

4from hyd.backend.util.const import LOADER_HTML_INJECTION 

5 

6 

7def inject_js_loader_to_html(*, dir_path: Path) -> None: 

8 html_files: list[Path] = [] 

9 _recursive_html_file_search(dir_path=dir_path, html_files=html_files) 

10 

11 for file in html_files: 

12 with open(file, "a") as handle: 

13 handle.write("\n" + LOADER_HTML_INJECTION) 

14 

15 

16def reinject_js_loader_to_html(*, dir_path: Path) -> None: 

17 html_files: list[Path] = [] 

18 _recursive_html_file_search(dir_path=dir_path, html_files=html_files) 

19 

20 for file in html_files: 

21 with open(file, "r+", encoding="utf-8") as handle: 

22 handle.seek(0, os.SEEK_END) 

23 pos = handle.tell() 

24 while pos > 0 and handle.read(1) != "\n": 

25 pos -= 1 

26 handle.seek(pos, os.SEEK_SET) 

27 if pos > 0: 

28 handle.seek(pos, os.SEEK_SET) 

29 handle.truncate() 

30 handle.write("\n" + LOADER_HTML_INJECTION) 

31 

32 

33def _recursive_html_file_search(*, dir_path: Path, html_files: list[Path]) -> None: 

34 for entry in dir_path.iterdir(): 

35 if entry.is_dir(): 

36 _recursive_html_file_search(dir_path=entry, html_files=html_files) 

37 elif entry.is_file() and entry.suffix == ".html": 

38 html_files.append(entry)