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:
|
if "Reasons" not in raw or "Paths" not in raw:
|
||||||
return None
|
return None
|
||||||
|
import re
|
||||||
|
|
||||||
lines = raw.splitlines()
|
lines = raw.splitlines()
|
||||||
headers = []
|
headers = []
|
||||||
@@ -89,6 +90,9 @@ def _beautify_restic_forget(raw: str) -> str | None:
|
|||||||
if not headers:
|
if not headers:
|
||||||
return None
|
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]:
|
def parse_block(start_idx: int, end_idx: int) -> list[dict]:
|
||||||
header = lines[start_idx]
|
header = lines[start_idx]
|
||||||
cols = ["ID", "Time", "Host", "Tags", "Reasons", "Paths", "Size"]
|
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)):
|
for i in range(len(cols)):
|
||||||
segments.append(line[positions[i] : positions[i + 1]].strip())
|
segments.append(line[positions[i] : positions[i + 1]].strip())
|
||||||
row = dict(zip(cols, segments))
|
row = dict(zip(cols, segments))
|
||||||
if row["ID"]:
|
if row["ID"] and _valid_id(row["ID"]):
|
||||||
current = {
|
current = {
|
||||||
"id": row["ID"],
|
"id": row["ID"],
|
||||||
"time": row["Time"],
|
"time": row["Time"],
|
||||||
@@ -125,9 +129,9 @@ def _beautify_restic_forget(raw: str) -> str | None:
|
|||||||
current["paths"].append(row["Paths"])
|
current["paths"].append(row["Paths"])
|
||||||
entries.append(current)
|
entries.append(current)
|
||||||
elif current:
|
elif current:
|
||||||
if row["Reasons"]:
|
if row["Reasons"] and not row["Reasons"].startswith("-"):
|
||||||
current["reasons"].append(row["Reasons"])
|
current["reasons"].append(row["Reasons"])
|
||||||
if row["Paths"]:
|
if row["Paths"] and not row["Paths"].startswith("-"):
|
||||||
current["paths"].append(row["Paths"])
|
current["paths"].append(row["Paths"])
|
||||||
return entries
|
return entries
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user