Spaces:
Sleeping
Sleeping
Update admin_patterns.py
Browse files- admin_patterns.py +36 -22
admin_patterns.py
CHANGED
|
@@ -31,33 +31,47 @@ def _make_backup_of_patterns():
|
|
| 31 |
|
| 32 |
def load_pattern_config(debug: bool = False):
|
| 33 |
"""
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
"""
|
| 36 |
global pattern_config
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
-
with open(
|
| 40 |
cfg = json.load(f)
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if debug:
|
| 43 |
-
print("
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# create empty file
|
| 53 |
-
try:
|
| 54 |
-
with open(PATTERN_FILE, "w", encoding="utf-8") as f:
|
| 55 |
-
json.dump(DEFAULT_PATTERN_CONFIG, f, indent=2, ensure_ascii=False)
|
| 56 |
-
print("DEBUG: Created empty patterns.json at", PATTERN_FILE)
|
| 57 |
-
except Exception as e:
|
| 58 |
-
print("ERROR: Could not create patterns.json:", e)
|
| 59 |
-
pattern_config = DEFAULT_PATTERN_CONFIG.copy()
|
| 60 |
-
return pattern_config
|
| 61 |
|
| 62 |
|
| 63 |
def save_pattern_config(cfg: dict):
|
|
|
|
| 31 |
|
| 32 |
def load_pattern_config(debug: bool = False):
|
| 33 |
"""
|
| 34 |
+
Simplified config loader for Hugging Face Spaces:
|
| 35 |
+
- Checks common locations in order
|
| 36 |
+
- Returns first valid config found
|
| 37 |
+
- Falls back to default if none found
|
| 38 |
"""
|
| 39 |
global pattern_config
|
| 40 |
+
|
| 41 |
+
# Search paths in priority order
|
| 42 |
+
search_paths = [
|
| 43 |
+
PATTERN_FILE, # Same dir as module
|
| 44 |
+
"/app/patterns.json", # HF Spaces mount point
|
| 45 |
+
os.path.join(os.getcwd(), "patterns.json") # Current directory
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
# Try each path
|
| 49 |
+
for path in search_paths:
|
| 50 |
+
if not os.path.exists(path):
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
try:
|
| 54 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 55 |
cfg = json.load(f)
|
| 56 |
+
|
| 57 |
+
# Validate it has expected structure
|
| 58 |
+
if isinstance(cfg, dict) and ("global" in cfg or "states" in cfg):
|
| 59 |
+
pattern_config = cfg
|
| 60 |
+
if debug:
|
| 61 |
+
print(f"Loaded config from: {path}")
|
| 62 |
+
return cfg
|
| 63 |
+
|
| 64 |
+
except (json.JSONDecodeError, IOError) as e:
|
| 65 |
if debug:
|
| 66 |
+
print(f"Failed to load {path}: {e}")
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
# No valid config found - use default
|
| 70 |
+
pattern_config = DEFAULT_PATTERN_CONFIG.copy()
|
| 71 |
+
if debug:
|
| 72 |
+
print("No valid config found, using defaults")
|
| 73 |
+
|
| 74 |
+
return pattern_config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
def save_pattern_config(cfg: dict):
|