home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 115 / macaddict115.cdr / Software / Productivity / TextWrangler_2.1.1.dmg / TextWrangler.app / Contents / Resources / PythonCheckSyntax.py < prev    next >
Text File  |  2005-11-17  |  1KB  |  48 lines

  1. """Wrapper script for py_compile which reformats IndentationError and
  2. TabError into a format more palatable to BBEdit and TextWrangler's Python
  3. error parsers."""
  4.  
  5. import py_compile
  6. import sys
  7.  
  8. __revision__ = "$Revision: #1 $"
  9. __all__ = ['main']
  10.  
  11. try:
  12.     True, False
  13. except NameError:
  14.     # Maintain compatibility with Python 2.2
  15.     True, False = 1, 0
  16.     
  17. def print_reformatted_error(exc_type_name, exc_value):
  18.     """Print out IndentationError and TabError in the same format as
  19.     SyntaxError"""
  20.     msg = exc_value[0]
  21.     file = exc_value[1][0]
  22.     line = exc_value[1][1]
  23.     col = exc_value[1][2]
  24.     ctx = exc_value[1][3]
  25.     if ctx.endswith('\r') or ctx.endswith('\n'):
  26.         ctx = ctx[:-1]
  27.     print >> sys.stderr, '''  File "%s", line %d''' % (file, line)
  28.     print >> sys.stderr, ctx
  29.     print >> sys.stderr, ' ' * col + '^'
  30.     print >> sys.stderr, exc_type_name + ": " + msg
  31.  
  32. def main():
  33.     """Check syntax on the file passed in sys.argv[1]"""
  34.     source_file = sys.argv[1]
  35.     try:
  36.         py_compile.compile(source_file, doraise=True)
  37.     except py_compile.PyCompileError, ex:
  38.         (c, v, t) = sys.exc_info()
  39.         exc_names = [IndentationError.__name__, TabError.__name__]
  40.         if  v.exc_type_name in exc_names:
  41.             print_reformatted_error(v.exc_type_name, v.exc_value)
  42.         else:
  43.             print >> sys.stderr, v,
  44.         sys.exit(1)
  45.  
  46. if __name__ == "__main__":
  47.     main()
  48.