reflow.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. # Command-line program to use the parser to reflow
  3. # a Python 2.6 program
  4. import os, sys
  5. from py2_scan import ENDMARKER, Python2Scanner
  6. from py2_format import format_python2_stmts
  7. scan = Python2Scanner()
  8. if len(sys.argv) < 2:
  9. print("I need a filename to reformat")
  10. sys.exit(1)
  11. do_format = True
  12. do_scan_only = False
  13. i = 1
  14. if sys.argv[i] == '--scan':
  15. do_scan_only = True
  16. scan = Python2Scanner()
  17. do_format = False
  18. i += 1
  19. for path in sys.argv[i:]:
  20. if not os.path.exists(path):
  21. print("Can't find file %s; skipping" % path)
  22. continue
  23. with open(path, 'r') as fp:
  24. python2_stmts = fp.read()
  25. print(python2_stmts)
  26. if do_scan_only:
  27. tokens = scan.tokenize(python2_stmts)
  28. for t in tokens: print(t)
  29. print('=' * 30)
  30. else:
  31. formatted = format_python2_stmts(python2_stmts + ENDMARKER,
  32. show_tokens=True, showast=True,
  33. showgrammar=True)
  34. print('=' * 30)
  35. print(formatted)
  36. pass
  37. pass
  38. pass