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

Update sozo_gen.py

Browse files
Files changed (1) hide show
  1. sozo_gen.py +62 -85
sozo_gen.py CHANGED
@@ -258,99 +258,76 @@ def concat_media(file_paths: List[str], output_path: Path):
258
  finally:
259
  list_file.unlink(missing_ok=True)
260
 
261
- # Backward-compatible fix: Override json.dumps to handle non-serializable types
262
- import json as _json
 
263
 
264
- def safe_json_dumps(obj, indent=2, **kwargs):
265
- """Safely serialize object to JSON, handling non-serializable types."""
266
- def json_serializer(obj):
267
- if isinstance(obj, (bool, int, float, str, type(None))):
268
- return obj
269
- elif isinstance(obj, (list, tuple)):
270
- return [json_serializer(item) for item in obj]
271
- elif isinstance(obj, dict):
272
- return {key: json_serializer(value) for key, value in obj.items()}
273
- else:
274
- # Convert non-serializable objects to string representation
275
- return str(obj)
276
-
277
- try:
278
- return _json.dumps(json_serializer(obj), indent=indent, **kwargs)
279
- except Exception as e:
280
- logging.warning(f"JSON serialization failed: {e}")
281
- return str(obj)
282
-
283
- # Monkey patch json.dumps to use our safe version
284
- json.dumps = safe_json_dumps
285
-
286
- def safe_firebase_data(obj):
287
- """
288
- Safely prepare data for Firebase by ensuring all values are JSON-serializable
289
- and Firebase-compatible.
290
- """
291
- def clean_for_firebase(obj):
292
- if obj is None:
293
- return None
294
- elif isinstance(obj, bool):
295
- return obj
296
- elif isinstance(obj, (int, float)):
297
- return obj
298
- elif isinstance(obj, str):
299
- # Clean string for Firebase - remove null bytes and control characters
300
- return ''.join(char for char in obj if ord(char) >= 32 or char in '\n\r\t')
301
- elif isinstance(obj, (list, tuple)):
302
- return [clean_for_firebase(item) for item in obj]
303
- elif isinstance(obj, dict):
304
- cleaned = {}
305
- for key, value in obj.items():
306
- # Firebase keys must be strings and can't contain certain characters
307
- clean_key = str(key).replace('.', '_').replace('$', '_').replace('#', '_').replace('[', '_').replace(']', '_').replace('/', '_')
308
- cleaned[clean_key] = clean_for_firebase(value)
309
- return cleaned
310
- else:
311
- # Convert to string and clean
312
- str_repr = str(obj)
313
- return ''.join(char for char in str_repr if ord(char) >= 32 or char in '\n\r\t')
314
-
315
- return clean_for_firebase(obj)
316
 
317
- def concat_media(file_paths: List[str], output_path: Path):
318
- valid_paths = [p for p in file_paths if Path(p).exists() and Path(p).stat().st_size > 100]
319
- if not valid_paths: raise ValueError("No valid media files to concatenate.")
320
- if len(valid_paths) == 1: import shutil; shutil.copy2(valid_paths[0], str(output_path)); return
321
- list_file = output_path.with_suffix(".txt")
322
- with open(list_file, 'w') as f:
323
- for path in valid_paths: f.write(f"file '{Path(path).resolve()}'\n")
324
- cmd = ["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", str(list_file), "-c", "copy", str(output_path)]
325
  try:
326
- subprocess.run(cmd, check=True, capture_output=True, text=True)
327
- finally:
328
- list_file.unlink(missing_ok=True)
329
-
330
- # Backward-compatible fix: Override json.dumps to handle non-serializable types
331
- import json as _json
332
-
333
- def safe_json_dumps(obj, indent=2, **kwargs):
334
- """Safely serialize object to JSON, handling non-serializable types."""
335
- def json_serializer(obj):
336
- if isinstance(obj, (bool, int, float, str, type(None))):
337
- return obj
338
- elif isinstance(obj, (list, tuple)):
339
- return [json_serializer(item) for item in obj]
340
- elif isinstance(obj, dict):
341
- return {key: json_serializer(value) for key, value in obj.items()}
 
 
 
342
  else:
343
- # Convert non-serializable objects to string representation
344
- return str(obj)
345
 
346
  try:
347
- return _json.dumps(json_serializer(obj), indent=indent, **kwargs)
348
  except Exception as e:
349
  logging.warning(f"JSON serialization failed: {e}")
350
- return str(obj)
351
-
352
- # Monkey patch json.dumps to use our safe version
353
- json.dumps = safe_json_dumps
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
 
355
  # --- Main Business Logic Functions for Flask ---
356
 
 
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]
289
+ elif isinstance(item, dict):
290
+ return {str(k): make_serializable(v) for k, v in item.items()}
291
  else:
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