codec.py 794 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. from sys import argv, exit
  4. import codecs
  5. import re
  6. import os
  7. if len(argv) != 2:
  8. exit(1)
  9. try:
  10. with open(argv[1]) as fle:
  11. text = fle.readlines()
  12. if text:
  13. match = re.match(r"#\s*coding\s*:\s*(?P<coding>\w+)", text[0])
  14. if match:
  15. text = codecs.lookup(match.groupdict()["coding"]).incrementaldecoder().decode(
  16. ''.join(text).encode('utf-8')).encode('utf-8')
  17. if isinstance(text, list):
  18. text = ''.join(text).encode('utf-8')
  19. compile(text, argv[1], 'exec', 0, 1)
  20. except SyntaxError as err:
  21. print('%s:%s:%s: %s' % (err.filename, err.lineno, err.offset, err.msg))
  22. except Exception as err:
  23. print('%s:%s:%s: %s' % (os.path.abspath(argv[1]), 1, 0, err))