<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import string <EOL> import tabnanny <EOL> import tokenize <EOL> import tkMessageBox <EOL> from idlelib import PyShell <EOL> from idlelib . configHandler import idleConf <EOL> from idlelib import macosxSupport <EOL> IDENTCHARS = string . ascii_letters + string . digits + "<STR_LIT:_>" <EOL> indent_message = """<STR_LIT>""" <EOL> class ScriptBinding : <EOL> menudefs = [ <EOL> ( '<STR_LIT>' , [ None , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ] ) , ] <EOL> def __init__ ( self , editwin ) : <EOL> self . editwin = editwin <EOL> self . flist = self . editwin . flist <EOL> self . root = self . editwin . root <EOL> if macosxSupport . isCocoaTk ( ) : <EOL> self . editwin . text_frame . bind ( '<STR_LIT>' , self . _run_module_event ) <EOL> def check_module_event ( self , event ) : <EOL> filename = self . getfilename ( ) <EOL> if not filename : <EOL> return '<STR_LIT>' <EOL> if not self . checksyntax ( filename ) : <EOL> return '<STR_LIT>' <EOL> if not self . tabnanny ( filename ) : <EOL> return '<STR_LIT>' <EOL> def tabnanny ( self , filename ) : <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> try : <EOL> tabnanny . process_tokens ( tokenize . generate_tokens ( f . readline ) ) <EOL> except tokenize . TokenError as msg : <EOL> msgtxt , ( lineno , start ) = msg <EOL> self . editwin . gotoline ( lineno ) <EOL> self . errorbox ( "<STR_LIT>" , <EOL> "<STR_LIT>" % msgtxt ) <EOL> return False <EOL> except tabnanny . NannyNag as nag : <EOL> self . editwin . gotoline ( nag . get_lineno ( ) ) <EOL> self . errorbox ( "<STR_LIT>" , indent_message ) <EOL> return False <EOL> return True <EOL> def checksyntax ( self , filename ) : <EOL> self . shell = shell = self . flist . open_shell ( ) <EOL> saved_stream = shell . get_warning_stream ( ) <EOL> shell . set_warning_stream ( shell . stderr ) <EOL> with open ( filename , '<STR_LIT:r>' ) as f : <EOL> source = f . read ( ) <EOL> if '<STR_LIT:\r>' in source : <EOL> source = re . sub ( r"<STR_LIT:\r\n>" , "<STR_LIT:\n>" , source ) <EOL> source = re . sub ( r"<STR_LIT:\r>" , "<STR_LIT:\n>" , source ) <EOL> if source and source [ - <NUM_LIT:1> ] != '<STR_LIT:\n>' : <EOL> source = source + '<STR_LIT:\n>' <EOL> text = self . editwin . text <EOL> text . tag_remove ( "<STR_LIT>" , "<STR_LIT:1.0>" , "<STR_LIT:end>" ) <EOL> try : <EOL> try : <EOL> return compile ( source , filename , "<STR_LIT>" ) <EOL> except ( SyntaxError , OverflowError , ValueError ) as err : <EOL> try : <EOL> msg , ( errorfilename , lineno , offset , line ) = err <EOL> if not errorfilename : <EOL> err . args = msg , ( filename , lineno , offset , line ) <EOL> err . filename = filename <EOL> self . colorize_syntax_error ( msg , lineno , offset ) <EOL> except : <EOL> msg = "<STR_LIT>" + str ( err ) <EOL> self . errorbox ( "<STR_LIT>" , <EOL> "<STR_LIT>" + msg ) <EOL> return False <EOL> finally : <EOL> shell . set_warning_stream ( saved_stream ) <EOL> def colorize_syntax_error ( self , msg , lineno , offset ) : <EOL> text = self . editwin . text <EOL> pos = "<STR_LIT>" % ( lineno - <NUM_LIT:1> , offset - <NUM_LIT:1> ) <EOL> text . tag_add ( "<STR_LIT>" , pos ) <EOL> char = text . get ( pos ) <EOL> if char and char in IDENTCHARS : <EOL> text . tag_add ( "<STR_LIT>" , pos + "<STR_LIT>" , pos ) <EOL> if '<STR_LIT:\n>' == text . get ( pos ) : <EOL> text . mark_set ( "<STR_LIT>" , pos ) <EOL> else : <EOL> text . mark_set ( "<STR_LIT>" , pos + "<STR_LIT>" ) <EOL> text . see ( pos ) <EOL> def run_module_event ( self , event ) : <EOL> """<STR_LIT>""" <EOL> filename = self . getfilename ( ) <EOL> if not filename : <EOL> return '<STR_LIT>' <EOL> code = self . checksyntax ( filename ) <EOL> if not code : <EOL> return '<STR_LIT>' <EOL> if not self . tabnanny ( filename ) : <EOL> return '<STR_LIT>' <EOL> interp = self . shell . interp <EOL> if PyShell . use_subprocess : <EOL> interp . restart_subprocess ( with_cwd = False ) <EOL> dirname = os . path . dirname ( filename ) <EOL> interp . runcommand ( """<STR_LIT>""" . format ( filename = filename , dirname = dirname ) ) <EOL> interp . prepend_syspath ( filename ) <EOL> interp . runcode ( code ) <EOL> return '<STR_LIT>' <EOL> if macosxSupport . isCocoaTk ( ) : <EOL> _run_module_event = run_module_event <EOL> def run_module_event ( self , event ) : <EOL> self . editwin . text_frame . after ( <NUM_LIT:200> , <EOL> lambda : self . editwin . text_frame . event_generate ( '<STR_LIT>' ) ) <EOL> return '<STR_LIT>' <EOL> def getfilename ( self ) : <EOL> """<STR_LIT>""" <EOL> filename = self . editwin . io . filename <EOL> if not self . editwin . get_saved ( ) : <EOL> autosave = idleConf . GetOption ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , type = '<STR_LIT:bool>' ) <EOL> if autosave and filename : <EOL> self . editwin . io . save ( None ) <EOL> else : <EOL> confirm = self . ask_save_dialog ( ) <EOL> self . editwin . text . focus_set ( ) <EOL> if confirm : <EOL> self . editwin . io . save ( None ) <EOL> filename = self . editwin . io . filename <EOL> else : <EOL> filename = None <EOL> return filename <EOL> def ask_save_dialog ( self ) : <EOL> msg = "<STR_LIT>" + <NUM_LIT:5> * '<STR_LIT:U+0020>' + "<STR_LIT>" <EOL> confirm = tkMessageBox . askokcancel ( title = "<STR_LIT>" , <EOL> message = msg , <EOL> default = tkMessageBox . OK , <EOL> master = self . editwin . text ) <EOL> return confirm <EOL> def errorbox ( self , title , message ) : <EOL> tkMessageBox . showerror ( title , message , master = self . editwin . text ) <EOL> self . editwin . text . focus_set ( ) </s>
