rairo commited on
Commit
a70c9da
·
verified ·
1 Parent(s): 5149f53

Update sozo_gen.py

Browse files
Files changed (1) hide show
  1. sozo_gen.py +42 -56
sozo_gen.py CHANGED
@@ -258,31 +258,17 @@ def concat_media(file_paths: List[str], output_path: Path):
258
  finally:
259
  list_file.unlink(missing_ok=True)
260
 
261
- # Safe, backward-compatible fix that only patches the specific problematic calls
262
  import json as _original_json
263
- import builtins
264
-
265
- # Store original functions
266
- _original_str = builtins.str
267
- _original_dumps = _original_json.dumps
268
-
269
- def safe_str(obj):
270
- """Safe str() that handles Firebase compatibility."""
271
- try:
272
- result = _original_str(obj)
273
- # Clean for Firebase - remove problematic characters
274
- clean_result = ''.join(char for char in result if ord(char) >= 32 or char in '\n\r\t')
275
- # Limit length
276
- if len(clean_result) > 1000:
277
- clean_result = clean_result[:1000] + "... (truncated)"
278
- return clean_result
279
- except Exception:
280
- return "Error occurred during processing"
281
 
282
  def safe_json_dumps(obj, **kwargs):
283
  """Safe JSON dumps that handles non-serializable types."""
284
  def make_serializable(item):
285
- if isinstance(item, (str, int, float, type(None), bool)):
 
 
 
 
286
  return item
287
  elif isinstance(item, (list, tuple)):
288
  return [make_serializable(x) for x in item]
@@ -292,42 +278,42 @@ def safe_json_dumps(obj, **kwargs):
292
  return str(item)
293
 
294
  try:
295
- return _original_dumps(make_serializable(obj), **kwargs)
296
- except Exception as e:
297
- logging.warning(f"JSON serialization failed: {e}")
298
- return _original_str(obj)
299
-
300
- # Only apply patches in specific contexts to avoid breaking Firebase
301
- import sys
302
- import traceback
303
-
304
- def contextual_json_dumps(obj, **kwargs):
305
- """Only apply safe JSON dumps when called from problematic contexts."""
306
- # Check if we're in the ChartGenerator context
307
- frame = sys._getframe(1)
308
- if frame and hasattr(frame, 'f_code'):
309
- # Check if this is called from generate_chart_spec or similar
310
- if 'generate_chart_spec' in frame.f_code.co_name or 'ChartGenerator' in str(frame.f_locals):
311
- return safe_json_dumps(obj, **kwargs)
312
-
313
- # Use original for everything else (including Firebase)
314
- return _original_dumps(obj, **kwargs)
315
-
316
- def contextual_str(obj):
317
- """Only apply safe str when called from Firebase error contexts."""
318
- # Check if we're in an error handling context
319
- frame = sys._getframe(1)
320
- if frame and hasattr(frame, 'f_code'):
321
- # Check if this is called from Firebase update error handling
322
- if 'update' in frame.f_code.co_name and any('firebase' in str(v).lower() for v in frame.f_locals.values() if v):
323
- return safe_str(obj)
324
-
325
- # Use original for everything else
326
- return _original_str(obj)
327
-
328
- # Apply contextual patches
329
- _original_json.dumps = contextual_json_dumps
330
- builtins.str = contextual_str
331
 
332
  # --- Main Business Logic Functions for Flask ---
333
 
 
258
  finally:
259
  list_file.unlink(missing_ok=True)
260
 
261
+ # DROP-IN FIX - requires zero code changes
262
  import json as _original_json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
 
264
  def safe_json_dumps(obj, **kwargs):
265
  """Safe JSON dumps that handles non-serializable types."""
266
  def make_serializable(item):
267
+ if item is None:
268
+ return None
269
+ elif isinstance(item, bool): # Check bool before int since bool is subclass of int
270
+ return item
271
+ elif isinstance(item, (int, float, str)):
272
  return item
273
  elif isinstance(item, (list, tuple)):
274
  return [make_serializable(x) for x in item]
 
278
  return str(item)
279
 
280
  try:
281
+ serializable_obj = make_serializable(obj)
282
+ return _original_json.dumps(serializable_obj, **kwargs)
283
+ except Exception:
284
+ return f'"{str(obj)}"'
285
+
286
+ # Patch json.dumps
287
+ _original_json.dumps = safe_json_dumps
288
+
289
+ # Patch Firebase Reference.update to handle error strings safely
290
+ try:
291
+ from firebase_admin import db
292
+ _original_update = db.Reference.update
293
+
294
+ def safe_update(self, value):
295
+ """Safe wrapper for Firebase update that cleans error strings."""
296
+ if isinstance(value, dict) and 'error' in value:
297
+ try:
298
+ # Clean the error string for Firebase
299
+ error_str = str(value['error'])
300
+ clean_error = ''.join(char for char in error_str if 32 <= ord(char) <= 126 or char in '\n\r\t ')
301
+ if len(clean_error) > 1000:
302
+ clean_error = clean_error[:1000] + "... (truncated)"
303
+ value = dict(value) # Create a copy
304
+ value['error'] = clean_error
305
+ except Exception:
306
+ # If cleaning fails, use a generic error message
307
+ value = dict(value) # Create a copy
308
+ value['error'] = 'An error occurred during processing'
309
+
310
+ return _original_update(self, value)
311
+
312
+ db.Reference.update = safe_update
313
+
314
+ except ImportError:
315
+ # firebase_admin not available, skip patching
316
+ pass
317
 
318
  # --- Main Business Logic Functions for Flask ---
319