refactor: fix review issues — reuse entity_to_row, public parse_row, validate dates
- scan.py: use entity_to_row from csv_utils instead of duplicated _fmt - csv_utils: rename _parse_row to parse_row (public API) - backup.py: use public parse_row - cli.py: validate --start < --end in backup command Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@ from collections import defaultdict
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from order_bak.csv_utils import CSV_COLUMNS, _parse_row
|
from order_bak.csv_utils import CSV_COLUMNS, parse_row
|
||||||
|
|
||||||
|
|
||||||
def backup_orders(
|
def backup_orders(
|
||||||
@@ -19,7 +19,7 @@ def backup_orders(
|
|||||||
with open(scan_path, newline="") as f:
|
with open(scan_path, newline="") as f:
|
||||||
reader = csv.DictReader(f)
|
reader = csv.DictReader(f)
|
||||||
for raw_row in reader:
|
for raw_row in reader:
|
||||||
entity = _parse_row(raw_row)
|
entity = parse_row(raw_row)
|
||||||
if entity["state"] != 1:
|
if entity["state"] != 1:
|
||||||
continue
|
continue
|
||||||
order_time = entity["OrderTime"]
|
order_time = entity["OrderTime"]
|
||||||
|
|||||||
@@ -49,6 +49,10 @@ def backup(ctx, start, end, scan_path):
|
|||||||
start_utc = start.replace(tzinfo=timezone.utc)
|
start_utc = start.replace(tzinfo=timezone.utc)
|
||||||
end_utc = end.replace(tzinfo=timezone.utc)
|
end_utc = end.replace(tzinfo=timezone.utc)
|
||||||
|
|
||||||
|
if start_utc >= end_utc:
|
||||||
|
click.echo("Error: --start must be before --end")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
backup_orders(
|
backup_orders(
|
||||||
scan_path=scan_file,
|
scan_path=scan_file,
|
||||||
backup_dir=Path("./backups"),
|
backup_dir=Path("./backups"),
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ def entity_to_row(entity: dict[str, Any]) -> dict[str, str]:
|
|||||||
return {col: _format_value(entity.get(col)) for col in CSV_COLUMNS}
|
return {col: _format_value(entity.get(col)) for col in CSV_COLUMNS}
|
||||||
|
|
||||||
|
|
||||||
def _parse_row(raw: dict[str, str]) -> dict[str, Any]:
|
def parse_row(raw: dict[str, str]) -> dict[str, Any]:
|
||||||
result: dict[str, Any] = {}
|
result: dict[str, Any] = {}
|
||||||
for col in CSV_COLUMNS:
|
for col in CSV_COLUMNS:
|
||||||
val = raw.get(col, "")
|
val = raw.get(col, "")
|
||||||
@@ -51,7 +51,7 @@ def _parse_row(raw: dict[str, str]) -> dict[str, Any]:
|
|||||||
|
|
||||||
|
|
||||||
def row_to_entity(raw: dict[str, str]) -> dict[str, Any]:
|
def row_to_entity(raw: dict[str, str]) -> dict[str, Any]:
|
||||||
return _parse_row(raw)
|
return parse_row(raw)
|
||||||
|
|
||||||
|
|
||||||
def write_csv(path: Path, entities: list[dict[str, Any]]) -> None:
|
def write_csv(path: Path, entities: list[dict[str, Any]]) -> None:
|
||||||
@@ -67,4 +67,4 @@ def read_csv(path: Path):
|
|||||||
with open(path, newline="") as f:
|
with open(path, newline="") as f:
|
||||||
reader = csv.DictReader(f)
|
reader = csv.DictReader(f)
|
||||||
for row in reader:
|
for row in reader:
|
||||||
yield _parse_row(row)
|
yield parse_row(row)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
from azure.data.tables import TableClient
|
from azure.data.tables import TableClient
|
||||||
|
|
||||||
from order_bak.csv_utils import CSV_COLUMNS
|
from order_bak.csv_utils import CSV_COLUMNS, entity_to_row
|
||||||
|
|
||||||
|
|
||||||
def scan_orders(
|
def scan_orders(
|
||||||
@@ -30,7 +30,7 @@ def scan_orders(
|
|||||||
|
|
||||||
for page in paged.by_page():
|
for page in paged.by_page():
|
||||||
for entity in page:
|
for entity in page:
|
||||||
writer.writerow({col: _fmt(entity.get(col)) for col in CSV_COLUMNS})
|
writer.writerow(entity_to_row(entity))
|
||||||
total_scanned += 1
|
total_scanned += 1
|
||||||
|
|
||||||
if delay_ms > 0:
|
if delay_ms > 0:
|
||||||
@@ -38,11 +38,3 @@ def scan_orders(
|
|||||||
|
|
||||||
print(f"Scan complete: {total_scanned} orders saved to {csv_path}")
|
print(f"Scan complete: {total_scanned} orders saved to {csv_path}")
|
||||||
return csv_path
|
return csv_path
|
||||||
|
|
||||||
|
|
||||||
def _fmt(val):
|
|
||||||
if val is None:
|
|
||||||
return ""
|
|
||||||
if isinstance(val, datetime):
|
|
||||||
return val.isoformat()
|
|
||||||
return str(val)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user