sort_stronger_modules.py 981 B

1234567891011121314151617181920212223242526272829303132333435
  1. from __future__ import annotations
  2. import argparse
  3. from collections.abc import Sequence
  4. def main(argv: Sequence[str] | None = None) -> int:
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument("filenames", nargs="*")
  7. args = parser.parse_args(argv)
  8. ret = 0
  9. for filename in args.filenames:
  10. with open(filename) as f:
  11. contents = f.read()
  12. rest = contents
  13. b1, m1, rest = rest.partition("# begin: stronger typing\n")
  14. b2, m2, rest = rest.partition("module = [\n")
  15. b3, m3, rest = rest.partition("]\n")
  16. b4, m4, rest = rest.partition("# end: stronger typing\n")
  17. b3 = "".join(sorted(frozenset(b3.splitlines(True))))
  18. new_contents = b1 + m1 + b2 + m2 + b3 + m3 + b4 + m4 + rest
  19. if new_contents != contents:
  20. with open(filename, "w") as f:
  21. f.write(new_contents)
  22. ret = 1
  23. return ret
  24. if __name__ == "__main__":
  25. raise SystemExit(main())