clean_json_attrs.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. import json
  3. import os
  4. import re
  5. def Clean(start_dir, attr_pattern, file_pattern):
  6. cleaned = False
  7. def _remove_attrs(json_dict, attr_pattern):
  8. assert isinstance(json_dict, dict)
  9. removed = False
  10. for key, val in json_dict.items():
  11. if isinstance(val, dict):
  12. if _remove_attrs(val, attr_pattern):
  13. removed = True
  14. elif re.search(attr_pattern, key):
  15. del json_dict[key]
  16. removed = True
  17. return removed
  18. for root, dirs, files in os.walk(start_dir):
  19. for f in files:
  20. if not re.search(file_pattern, f):
  21. continue
  22. path = os.path.join(root, f)
  23. json_dict = json.loads(open(path).read())
  24. if not _remove_attrs(json_dict, attr_pattern):
  25. continue
  26. with open(path, 'w') as new_contents:
  27. new_contents.write(json.dumps(json_dict))
  28. cleaned = True
  29. return cleaned
  30. if __name__ == '__main__':
  31. import argparse
  32. import sys
  33. parser = argparse.ArgumentParser(
  34. description='Recursively removes attributes from JSON files')
  35. parser.add_argument('--attr_pattern', type=str, required=True,
  36. help='A regex of attributes to remove')
  37. parser.add_argument('--file_pattern', type=str, required=True,
  38. help='A regex of files to clean')
  39. parser.add_argument('start_dir', type=str,
  40. help='A directory to start scanning')
  41. args = parser.parse_args(sys.argv[1:])
  42. Clean(start_dir=args.start_dir, attr_pattern=args.attr_pattern,
  43. file_pattern=args.file_pattern)