initial commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
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 "elements = { }\n"
|
||||
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
|
||||
Reference in New Issue
Block a user