-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathhelpers.py
More file actions
59 lines (39 loc) · 1.46 KB
/
Copy pathhelpers.py
File metadata and controls
59 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import hashlib
from pathlib import Path
from typing import Iterable, Tuple
from lxml import etree
from rich.console import Console
FILE_DIR = Path(__file__).resolve().parent
SAVE_DIR = FILE_DIR / "keys"
if not SAVE_DIR.exists():
SAVE_DIR.mkdir(exist_ok=True)
CACHE_FILE = FILE_DIR / "cache.txt"
if not CACHE_FILE.exists():
CACHE_FILE.touch()
console = Console()
INFO_STYLE = "bold cyan"
WARN_STYLE = "bold yellow"
ERROR_STYLE = "bold red"
SECTION_STYLE = "bold blue"
def log_info(message: str) -> None:
_log("INFO", message, INFO_STYLE)
def log_warning(message: str) -> None:
_log("WARN", message, WARN_STYLE)
def log_error(message: str) -> None:
_log("ERROR", message, ERROR_STYLE)
def _log(label: str, message: str, style: str) -> None:
console.print(f"[{style}]{label:<5}[/] {message}")
def print_section(title: str) -> None:
console.rule(f"[{SECTION_STYLE}]{title}[/]", style=SECTION_STYLE)
def print_summary(title: str, rows: Iterable[Tuple[str, str]]) -> None:
formatted = " ".join(
f"[bold white]{label}[/]: [cyan]{value}[/]" for label, value in rows
)
console.print(f"[{SECTION_STYLE}]{title}[/] {formatted}")
def hash_xml_file(file_content: bytes) -> str:
"""Hashes the contents of an XML file."""
root = etree.fromstring(file_content)
canonical_xml = etree.tostring(root, method="c14n")
hash_value = hashlib.sha256(canonical_xml).hexdigest()
file_name = hash_value + ".xml"
return file_name