gkdivya commited on
Commit
93c996d
·
verified ·
1 Parent(s): 2d8d3dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -75,26 +75,39 @@ master_df: pd.DataFrame | None = None
75
  pattern_config: dict | None = None
76
 
77
 
78
- # ====================================================
79
- # PATTERN CONFIG LOAD / SAVE (JSON)
80
- # ====================================================
81
-
82
- DEFAULT_PATTERN_CONFIG = {
83
- "global": [], # start empty; managed via Admin tab
84
- "states": {} # e.g. "KARNATAKA": [{pattern: "...", replacement: "..."}]
85
- }
86
-
87
-
88
- def load_pattern_config() -> dict:
89
- """Load patterns from JSON file, or create empty file."""
90
  if os.path.exists(PATTERN_FILE):
91
- with open(PATTERN_FILE, "r", encoding="utf-8") as f:
92
- return json.load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
- # If file not present, seed with empty structure
95
- with open(PATTERN_FILE, "w", encoding="utf-8") as f:
96
- json.dump(DEFAULT_PATTERN_CONFIG, f, indent=2, ensure_ascii=False)
97
- return DEFAULT_PATTERN_CONFIG.copy()
98
 
99
 
100
  def save_pattern_config(config: dict) -> None:
 
75
  pattern_config: dict | None = None
76
 
77
 
78
+ def load_pattern_config(debug=True) -> dict:
79
+ """
80
+ Load patterns.json from PATTERN_FILE.
81
+ If missing, create empty structure.
82
+ If debug=True, also return verbose info via prints.
83
+ """
 
 
 
 
 
 
84
  if os.path.exists(PATTERN_FILE):
85
+ try:
86
+ with open(PATTERN_FILE, "r", encoding="utf-8") as f:
87
+ cfg = json.load(f)
88
+ if debug:
89
+ print("DEBUG: Successfully loaded patterns.json")
90
+ print("DEBUG: top-level keys:", list(cfg.keys()))
91
+ # show counts safely
92
+ gcount = len(cfg.get("global", []))
93
+ skeys = list(cfg.get("states", {}).keys())
94
+ print(f"DEBUG: global patterns={gcount}, state keys={skeys}")
95
+ return cfg
96
+ except Exception as e:
97
+ print("ERROR: Failed to parse patterns.json:", e)
98
+ # fallback to empty skeleton
99
+ return {"global": [], "states": {}}
100
+ else:
101
+ # Not present — create it with empty structure and log
102
+ skeleton = {"global": [], "states": {}}
103
+ try:
104
+ with open(PATTERN_FILE, "w", encoding="utf-8") as f:
105
+ json.dump(skeleton, f, indent=2, ensure_ascii=False)
106
+ print("DEBUG: patterns.json not found — created empty patterns.json at", PATTERN_FILE)
107
+ except Exception as e:
108
+ print("ERROR: Could not create patterns.json:", e)
109
+ return skeleton
110
 
 
 
 
 
111
 
112
 
113
  def save_pattern_config(config: dict) -> None: