Filter restic forget parsing to ignore summary rows
This commit is contained in:
@@ -80,6 +80,7 @@ def _beautify_restic_forget(raw: str) -> str | None:
|
||||
"""
|
||||
if "Reasons" not in raw or "Paths" not in raw:
|
||||
return None
|
||||
import re
|
||||
|
||||
lines = raw.splitlines()
|
||||
headers = []
|
||||
@@ -89,6 +90,9 @@ def _beautify_restic_forget(raw: str) -> str | None:
|
||||
if not headers:
|
||||
return None
|
||||
|
||||
def _valid_id(val: str) -> bool:
|
||||
return bool(re.fullmatch(r"[0-9a-f]{7,64}", val.strip()))
|
||||
|
||||
def parse_block(start_idx: int, end_idx: int) -> list[dict]:
|
||||
header = lines[start_idx]
|
||||
cols = ["ID", "Time", "Host", "Tags", "Reasons", "Paths", "Size"]
|
||||
@@ -109,7 +113,7 @@ def _beautify_restic_forget(raw: str) -> str | None:
|
||||
for i in range(len(cols)):
|
||||
segments.append(line[positions[i] : positions[i + 1]].strip())
|
||||
row = dict(zip(cols, segments))
|
||||
if row["ID"]:
|
||||
if row["ID"] and _valid_id(row["ID"]):
|
||||
current = {
|
||||
"id": row["ID"],
|
||||
"time": row["Time"],
|
||||
@@ -125,9 +129,9 @@ def _beautify_restic_forget(raw: str) -> str | None:
|
||||
current["paths"].append(row["Paths"])
|
||||
entries.append(current)
|
||||
elif current:
|
||||
if row["Reasons"]:
|
||||
if row["Reasons"] and not row["Reasons"].startswith("-"):
|
||||
current["reasons"].append(row["Reasons"])
|
||||
if row["Paths"]:
|
||||
if row["Paths"] and not row["Paths"].startswith("-"):
|
||||
current["paths"].append(row["Paths"])
|
||||
return entries
|
||||
|
||||
|
||||
Reference in New Issue
Block a user