Changeset 391 for python/trunk/Lib/idlelib/IOBinding.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/idlelib/IOBinding.py
r2 r391 8 8 import os 9 9 import types 10 import pipes 10 11 import sys 11 12 import codecs … … 17 18 from SimpleDialog import SimpleDialog 18 19 19 from configHandler import idleConf20 from idlelib.configHandler import idleConf 20 21 21 22 try: … … 71 72 encoding = encoding.lower() 72 73 73 coding_re = re.compile( "coding[:=]\s*([-\w_.]+)")74 coding_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)') 74 75 75 76 class EncodingMessage(SimpleDialog): … … 125 126 """ 126 127 # Only consider the first two lines 127 str = str.split("\n")[:2] 128 str = "\n".join(str) 129 130 match = coding_re.search(str) 131 if not match: 128 lst = str.split("\n", 2)[:2] 129 for line in lst: 130 match = coding_re.match(line) 131 if match is not None: 132 break 133 else: 132 134 return None 133 135 name = match.group(1) … … 197 199 198 200 def open(self, event=None, editFile=None): 199 if self.editwin.flist: 201 flist = self.editwin.flist 202 # Save in case parent window is closed (ie, during askopenfile()). 203 if flist: 200 204 if not editFile: 201 205 filename = self.askopenfile() … … 203 207 filename=editFile 204 208 if filename: 205 # If the current window has no filename and hasn't been 206 # modified, we replace its contents (no loss). Otherwise 207 # we open a new window. But we won't replace the 208 # shell window (which has an interp(reter) attribute), which 209 # gets set to "not modified" at every new prompt. 210 try: 211 interp = self.editwin.interp 212 except AttributeError: 213 interp = None 214 if not self.filename and self.get_saved() and not interp: 215 self.editwin.flist.open(filename, self.loadfile) 209 # If editFile is valid and already open, flist.open will 210 # shift focus to its existing window. 211 # If the current window exists and is a fresh unnamed, 212 # unmodified editor window (not an interpreter shell), 213 # pass self.loadfile to flist.open so it will load the file 214 # in the current window (if the file is not already open) 215 # instead of a new window. 216 if (self.editwin and 217 not getattr(self.editwin, 'interp', None) and 218 not self.filename and 219 self.get_saved()): 220 flist.open(filename, self.loadfile) 216 221 else: 217 self.editwin.flist.open(filename)222 flist.open(filename) 218 223 else: 219 self.text.focus_set() 224 if self.text: 225 self.text.focus_set() 220 226 return "break" 221 # 227 222 228 # Code for use outside IDLE: 223 229 if self.get_saved(): … … 244 250 # open the file in binary mode so that we can handle 245 251 # end-of-line convention ourselves. 246 f = open(filename,'rb') 247 chars = f.read() 248 f.close() 249 except IOError, msg: 252 with open(filename, 'rb') as f: 253 chars = f.read() 254 except IOError as msg: 250 255 tkMessageBox.showerror("I/O Error", str(msg), master=self.text) 251 256 return False … … 267 272 self.set_filename(filename) 268 273 self.text.mark_set("insert", "1.0") 269 self.text. see("insert")274 self.text.yview("insert") 270 275 self.updaterecentfileslist(filename) 271 276 return True … … 290 295 try: 291 296 enc = coding_spec(chars) 292 except LookupError ,name:297 except LookupError as name: 293 298 tkMessageBox.showerror( 294 299 title="Error loading the file", … … 321 326 message = "Do you want to save %s before closing?" % ( 322 327 self.filename or "this untitled document") 323 m = tkMessageBox.Message( 324 title="Save On Close", 325 message=message, 326 icon=tkMessageBox.QUESTION, 327 type=tkMessageBox.YESNOCANCEL, 328 master=self.text) 329 reply = m.show() 330 if reply == "yes": 328 confirm = tkMessageBox.askyesnocancel( 329 title="Save On Close", 330 message=message, 331 default=tkMessageBox.YES, 332 master=self.text) 333 if confirm: 334 reply = "yes" 331 335 self.save(None) 332 336 if not self.get_saved(): 333 337 reply = "cancel" 338 elif confirm is None: 339 reply = "cancel" 340 else: 341 reply = "no" 334 342 self.text.focus_set() 335 343 return reply … … 340 348 else: 341 349 if self.writefile(self.filename): 342 self.set_saved( 1)350 self.set_saved(True) 343 351 try: 344 352 self.editwin.store_file_breaks() … … 376 384 chars = chars.replace("\n", self.eol_convention) 377 385 try: 378 f = open(filename, "wb") 379 f.write(chars) 380 f.flush() 381 f.close() 386 with open(filename, "wb") as f: 387 f.write(chars) 382 388 return True 383 except IOError ,msg:389 except IOError as msg: 384 390 tkMessageBox.showerror("I/O Error", str(msg), 385 391 master=self.text) … … 401 407 enc = coding_spec(chars) 402 408 failed = None 403 except LookupError ,msg:409 except LookupError as msg: 404 410 failed = msg 405 411 enc = None … … 466 472 467 473 def print_window(self, event): 468 m = tkMessageBox.Message( 469 title="Print", 470 message="Print to Default Printer", 471 icon=tkMessageBox.QUESTION, 472 type=tkMessageBox.OKCANCEL, 473 default=tkMessageBox.OK, 474 master=self.text) 475 reply = m.show() 476 if reply != tkMessageBox.OK: 474 confirm = tkMessageBox.askokcancel( 475 title="Print", 476 message="Print to Default Printer", 477 default=tkMessageBox.OK, 478 master=self.text) 479 if not confirm: 477 480 self.text.focus_set() 478 481 return "break" … … 489 492 os.unlink(tempfilename) 490 493 return "break" 491 platform =os.name492 printPlatform =1494 platform = os.name 495 printPlatform = True 493 496 if platform == 'posix': #posix platform 494 497 command = idleConf.GetOption('main','General', … … 498 501 command = idleConf.GetOption('main','General','print-command-win') 499 502 else: #no printing for this platform 500 printPlatform =0503 printPlatform = False 501 504 if printPlatform: #we can try to print for this platform 502 command = command % filename505 command = command % pipes.quote(filename) 503 506 pipe = os.popen(command, "r") 504 507 # things can get ugly on NT if there is no printer available. … … 512 515 tkMessageBox.showerror("Print status", output, master=self.text) 513 516 else: #no printing for this platform 514 message ="Printing is not enabled for this platform: %s" % platform517 message = "Printing is not enabled for this platform: %s" % platform 515 518 tkMessageBox.showinfo("Print status", message, master=self.text) 516 519 if tempfilename: … … 522 525 523 526 filetypes = [ 524 ("Python and text files", "*.py *.pyw *.txt", "TEXT"),525 (" All text files", "*", "TEXT"),527 ("Python files", "*.py *.pyw", "TEXT"), 528 ("Text files", "*.txt", "TEXT"), 526 529 ("All files", "*"), 527 530 ]
Note:
See TracChangeset
for help on using the changeset viewer.