fix_quotes.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """Fix inner double quotes in gen_test_case.py"""
  4. with open('gen_test_case.py', 'rb') as f:
  5. text = f.read().decode('utf-8')
  6. lines = text.split('\n')
  7. fixed_lines = []
  8. in_test_cases = False
  9. for line in lines:
  10. if 'TEST_CASES = [' in line:
  11. in_test_cases = True
  12. if in_test_cases and ']' == line.strip() and not line.strip().startswith('#'):
  13. # Might be closing bracket
  14. pass
  15. if in_test_cases:
  16. stripped = line.strip()
  17. # Check if this is a string line with inner double quotes
  18. # Pattern: starts with " and has more than 2 " (start quote, inner, end quote)
  19. if stripped.startswith('"') and stripped.count('"') > 2:
  20. leading = len(line) - len(line.lstrip())
  21. # Determine suffix
  22. if stripped.endswith('",'):
  23. end_suffix = '",'
  24. inner = stripped[1:-2]
  25. elif stripped.endswith('")'):
  26. end_suffix = '")'
  27. inner = stripped[1:-2]
  28. elif stripped.endswith('"'):
  29. end_suffix = '"'
  30. inner = stripped[1:-1]
  31. else:
  32. fixed_lines.append(line)
  33. continue
  34. # Escape inner double quotes
  35. inner_escaped = inner.replace('"', '\\"')
  36. new_line = ' ' * leading + '"' + inner_escaped + end_suffix
  37. fixed_lines.append(new_line)
  38. continue
  39. fixed_lines.append(line)
  40. result = '\n'.join(fixed_lines)
  41. with open('gen_test_case.py', 'wb') as f:
  42. f.write(result.encode('utf-8'))
  43. print('Done - fixed inner double quotes')