32 lines
810 B
Python
32 lines
810 B
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
log = logging.getLogger("xray-lists.render")
|
|
|
|
|
|
def render_elements_file(elements: list[str]) -> str:
|
|
if not elements:
|
|
return ""
|
|
joined = ",\n ".join(elements)
|
|
return f"elements = {{\n {joined}\n}}\n"
|
|
|
|
|
|
def write_if_changed(path: Path, content: str, dry_run: bool = False) -> bool:
|
|
old = path.read_text() if path.is_file() else None
|
|
if old == content:
|
|
log.debug("%s: unchanged", path)
|
|
return False
|
|
|
|
if dry_run:
|
|
log.info("[dry-run] would write %s", path)
|
|
return True
|
|
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
tmp = path.with_suffix(path.suffix + ".tmp")
|
|
tmp.write_text(content)
|
|
tmp.replace(path)
|
|
log.info("wrote %s", path)
|
|
return True
|