Create infra scripts
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
from datetime import date, datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, Iterable, Optional, Tuple
|
||||
@@ -108,6 +109,19 @@ def _normalize_person_type(value: Optional[object]) -> str:
|
||||
return "1"
|
||||
|
||||
|
||||
def _clean_text(value: object) -> Optional[str]:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, bytes):
|
||||
text = value.decode(errors="ignore")
|
||||
else:
|
||||
text = str(value)
|
||||
text = text.replace("\u00a0", " ")
|
||||
# Drop control chars (including C1) and unicode line/paragraph separators.
|
||||
text = re.sub(r"[\x00-\x1f\x7f-\x9f\u2028\u2029]", " ", text)
|
||||
return re.sub(r"\s+", " ", text).strip()
|
||||
|
||||
|
||||
def _build_n08_payload(row: Dict[str, object]) -> Dict[str, object]:
|
||||
mobile = (row.get("mobile_phone") or "").strip()
|
||||
phone = (row.get("phone") or "").strip()
|
||||
@@ -129,9 +143,11 @@ def _build_n08_payload(row: Dict[str, object]) -> Dict[str, object]:
|
||||
if isinstance(creation_date, (date, datetime)):
|
||||
creation_date = creation_date.isoformat()
|
||||
|
||||
name = _clean_text(row.get("name"))
|
||||
|
||||
return {
|
||||
"N08_KODAS_KS": row.get("client_code"),
|
||||
"N08_PAV": row.get("name"),
|
||||
"N08_PAV": name,
|
||||
"N08_ADR": row.get("address"),
|
||||
"N08_E_MAIL": row.get("email"),
|
||||
"N08_ADD_DATE": creation_date,
|
||||
@@ -163,6 +179,17 @@ def _normalize_value(value: object) -> str:
|
||||
return str(value).strip()
|
||||
|
||||
|
||||
def _format_diff_value(value: str) -> str:
|
||||
return (
|
||||
value.replace("\r", "\\r")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\t", "\\t")
|
||||
.replace("\u2028", "\\u2028")
|
||||
.replace("\u2029", "\\u2029")
|
||||
.replace("\u0085", "\\u0085")
|
||||
)
|
||||
|
||||
|
||||
def _diff_fields(
|
||||
existing: Optional[Dict[str, object]],
|
||||
desired: Dict[str, object],
|
||||
@@ -207,7 +234,9 @@ def _upsert_client(
|
||||
return
|
||||
print(f"Updating client: {client_code} ({index}/{total})")
|
||||
for field, (old, new) in changes.items():
|
||||
print(f" {field}: '{old}' -> '{new}'")
|
||||
old_fmt = _format_diff_value(old)
|
||||
new_fmt = _format_diff_value(new)
|
||||
print(f" {field}: '{old_fmt}' -> '{new_fmt}'")
|
||||
payload = {
|
||||
"method": "EDIT_N08",
|
||||
"params": {
|
||||
@@ -221,7 +250,8 @@ def _upsert_client(
|
||||
else:
|
||||
print(f"Creating client: {client_code} ({index}/{total})")
|
||||
for field, (_, new) in changes.items():
|
||||
print(f" {field}: '' -> '{new}'")
|
||||
new_fmt = _format_diff_value(new)
|
||||
print(f" {field}: '' -> '{new_fmt}'")
|
||||
payload = {
|
||||
"method": "EDIT_N08_FULL",
|
||||
"params": {"oper": "I", "user": user},
|
||||
|
||||
Reference in New Issue
Block a user