1import codecs 2from codecs import BOM_UTF8 3import os 4import re 5import shlex 6import sys 7import tempfile 8 9import tkinter.filedialog as tkFileDialog 10import tkinter.messagebox as tkMessageBox 11from tkinter.simpledialog import askstring 12 13import idlelib 14from idlelib.config import idleConf 15 16if idlelib.testing: # Set True by test.test_idle to avoid setlocale. 17 encoding = 'utf-8' 18 errors = 'surrogateescape' 19else: 20 # Try setting the locale, so that we can find out 21 # what encoding to use 22 try: 23 import locale 24 locale.setlocale(locale.LC_CTYPE, "") 25 except (ImportError, locale.Error): 26 pass 27 28 if sys.platform == 'win32': 29 encoding = 'utf-8' 30 errors = 'surrogateescape' 31 else: 32 try: 33 # Different things can fail here: the locale module may not be 34 # loaded, it may not offer nl_langinfo, or CODESET, or the 35 # resulting codeset may be unknown to Python. We ignore all 36 # these problems, falling back to ASCII 37 locale_encoding = locale.nl_langinfo(locale.CODESET) 38 if locale_encoding: 39 codecs.lookup(locale_encoding) 40 except (NameError, AttributeError, LookupError): 41 # Try getdefaultlocale: it parses environment variables, 42 # which may give a clue. Unfortunately, getdefaultlocale has 43 # bugs that can cause ValueError. 44 try: 45 locale_encoding = locale.getdefaultlocale()[1] 46 if locale_encoding: 47 codecs.lookup(locale_encoding) 48 except (ValueError, LookupError): 49 pass 50 51 if locale_encoding: 52 encoding = locale_encoding.lower() 53 errors = 'strict' 54 else: 55 # POSIX locale or macOS 56 encoding = 'ascii' 57 errors = 'surrogateescape' 58 # Encoding is used in multiple files; locale_encoding nowhere. 59 # The only use of 'encoding' below is in _decode as initial value 60 # of deprecated block asking user for encoding. 61 # Perhaps use elsewhere should be reviewed. 62 63coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) 64blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII) 65 66def coding_spec(data): 67 """Return the encoding declaration according to PEP 263. 68 69 When checking encoded data, only the first two lines should be passed 70 in to avoid a UnicodeDecodeError if the rest of the data is not unicode. 71 The first two lines would contain the encoding specification. 72 73 Raise a LookupError if the encoding is declared but unknown. 74 """ 75 if isinstance(data, bytes): 76 # This encoding might be wrong. However, the coding 77 # spec must be ASCII-only, so any non-ASCII characters 78 # around here will be ignored. Decoding to Latin-1 should 79 # never fail (except for memory outage) 80 lines = data.decode('iso-8859-1') 81 else: 82 lines = data 83 # consider only the first two lines 84 if '\n' in lines: 85 lst = lines.split('\n', 2)[:2] 86 elif '\r' in lines: 87 lst = lines.split('\r', 2)[:2] 88 else: 89 lst = [lines] 90 for line in lst: 91 match = coding_re.match(line) 92 if match is not None: 93 break 94 if not blank_re.match(line): 95 return None 96 else: 97 return None 98 name = match.group(1) 99 try: 100 codecs.lookup(name) 101 except LookupError: 102 # The standard encoding error does not indicate the encoding 103 raise LookupError("Unknown encoding: "+name) 104 return name 105 106 107class IOBinding: 108# One instance per editor Window so methods know which to save, close. 109# Open returns focus to self.editwin if aborted. 110# EditorWindow.open_module, others, belong here. 111 112 def __init__(self, editwin): 113 self.editwin = editwin 114 self.text = editwin.text 115 self.__id_open = self.text.bind("<<open-window-from-file>>", self.open) 116 self.__id_save = self.text.bind("<<save-window>>", self.save) 117 self.__id_saveas = self.text.bind("<<save-window-as-file>>", 118 self.save_as) 119 self.__id_savecopy = self.text.bind("<<save-copy-of-window-as-file>>", 120 self.save_a_copy) 121 self.fileencoding = None 122 self.__id_print = self.text.bind("<<print-window>>", self.print_window) 123 124 def close(self): 125 # Undo command bindings 126 self.text.unbind("<<open-window-from-file>>", self.__id_open) 127 self.text.unbind("<<save-window>>", self.__id_save) 128 self.text.unbind("<<save-window-as-file>>",self.__id_saveas) 129 self.text.unbind("<<save-copy-of-window-as-file>>", self.__id_savecopy) 130 self.text.unbind("<<print-window>>", self.__id_print) 131 # Break cycles 132 self.editwin = None 133 self.text = None 134 self.filename_change_hook = None 135 136 def get_saved(self): 137 return self.editwin.get_saved() 138 139 def set_saved(self, flag): 140 self.editwin.set_saved(flag) 141 142 def reset_undo(self): 143 self.editwin.reset_undo() 144 145 filename_change_hook = None 146 147 def set_filename_change_hook(self, hook): 148 self.filename_change_hook = hook 149 150 filename = None 151 dirname = None 152 153 def set_filename(self, filename): 154 if filename and os.path.isdir(filename): 155 self.filename = None 156 self.dirname = filename 157 else: 158 self.filename = filename 159 self.dirname = None 160 self.set_saved(1) 161 if self.filename_change_hook: 162 self.filename_change_hook() 163 164 def open(self, event=None, editFile=None): 165 flist = self.editwin.flist 166 # Save in case parent window is closed (ie, during askopenfile()). 167 if flist: 168 if not editFile: 169 filename = self.askopenfile() 170 else: 171 filename=editFile 172 if filename: 173 # If editFile is valid and already open, flist.open will 174 # shift focus to its existing window. 175 # If the current window exists and is a fresh unnamed, 176 # unmodified editor window (not an interpreter shell), 177 # pass self.loadfile to flist.open so it will load the file 178 # in the current window (if the file is not already open) 179 # instead of a new window. 180 if (self.editwin and 181 not getattr(self.editwin, 'interp', None) and 182 not self.filename and 183 self.get_saved()): 184 flist.open(filename, self.loadfile) 185 else: 186 flist.open(filename) 187 else: 188 if self.text: 189 self.text.focus_set() 190 return "break" 191 192 # Code for use outside IDLE: 193 if self.get_saved(): 194 reply = self.maybesave() 195 if reply == "cancel": 196 self.text.focus_set() 197 return "break" 198 if not editFile: 199 filename = self.askopenfile() 200 else: 201 filename=editFile 202 if filename: 203 self.loadfile(filename) 204 else: 205 self.text.focus_set() 206 return "break" 207 208 eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac) 209 eol_re = re.compile(eol) 210 eol_convention = os.linesep # default 211 212 def loadfile(self, filename): 213 try: 214 # open the file in binary mode so that we can handle 215 # end-of-line convention ourselves. 216 with open(filename, 'rb') as f: 217 two_lines = f.readline() + f.readline() 218 f.seek(0) 219 bytes = f.read() 220 except OSError as msg: 221 tkMessageBox.showerror("I/O Error", str(msg), parent=self.text) 222 return False 223 chars, converted = self._decode(two_lines, bytes) 224 if chars is None: 225 tkMessageBox.showerror("Decoding Error", 226 "File %s\nFailed to Decode" % filename, 227 parent=self.text) 228 return False 229 # We now convert all end-of-lines to '\n's 230 firsteol = self.eol_re.search(chars) 231 if firsteol: 232 self.eol_convention = firsteol.group(0) 233 chars = self.eol_re.sub(r"\n", chars) 234 self.text.delete("1.0", "end") 235 self.set_filename(None) 236 self.text.insert("1.0", chars) 237 self.reset_undo() 238 self.set_filename(filename) 239 if converted: 240 # We need to save the conversion results first 241 # before being able to execute the code 242 self.set_saved(False) 243 self.text.mark_set("insert", "1.0") 244 self.text.yview("insert") 245 self.updaterecentfileslist(filename) 246 return True 247 248 def _decode(self, two_lines, bytes): 249 "Create a Unicode string." 250 chars = None 251 # Check presence of a UTF-8 signature first 252 if bytes.startswith(BOM_UTF8): 253 try: 254 chars = bytes[3:].decode("utf-8") 255 except UnicodeDecodeError: 256 # has UTF-8 signature, but fails to decode... 257 return None, False 258 else: 259 # Indicates that this file originally had a BOM 260 self.fileencoding = 'BOM' 261 return chars, False 262 # Next look for coding specification 263 try: 264 enc = coding_spec(two_lines) 265 except LookupError as name: 266 tkMessageBox.showerror( 267 title="Error loading the file", 268 message="The encoding '%s' is not known to this Python "\ 269 "installation. The file may not display correctly" % name, 270 parent = self.text) 271 enc = None 272 except UnicodeDecodeError: 273 return None, False 274 if enc: 275 try: 276 chars = str(bytes, enc) 277 self.fileencoding = enc 278 return chars, False 279 except UnicodeDecodeError: 280 pass 281 # Try ascii: 282 try: 283 chars = str(bytes, 'ascii') 284 self.fileencoding = None 285 return chars, False 286 except UnicodeDecodeError: 287 pass 288 # Try utf-8: 289 try: 290 chars = str(bytes, 'utf-8') 291 self.fileencoding = 'utf-8' 292 return chars, False 293 except UnicodeDecodeError: 294 pass 295 # Finally, try the locale's encoding. This is deprecated; 296 # the user should declare a non-ASCII encoding 297 try: 298 # Wait for the editor window to appear 299 self.editwin.text.update() 300 enc = askstring( 301 "Specify file encoding", 302 "The file's encoding is invalid for Python 3.x.\n" 303 "IDLE will convert it to UTF-8.\n" 304 "What is the current encoding of the file?", 305 initialvalue = encoding, 306 parent = self.editwin.text) 307 308 if enc: 309 chars = str(bytes, enc) 310 self.fileencoding = None 311 return chars, True 312 except (UnicodeDecodeError, LookupError): 313 pass 314 return None, False # None on failure 315 316 def maybesave(self): 317 if self.get_saved(): 318 return "yes" 319 message = "Do you want to save %s before closing?" % ( 320 self.filename or "this untitled document") 321 confirm = tkMessageBox.askyesnocancel( 322 title="Save On Close", 323 message=message, 324 default=tkMessageBox.YES, 325 parent=self.text) 326 if confirm: 327 reply = "yes" 328 self.save(None) 329 if not self.get_saved(): 330 reply = "cancel" 331 elif confirm is None: 332 reply = "cancel" 333 else: 334 reply = "no" 335 self.text.focus_set() 336 return reply 337 338 def save(self, event): 339 if not self.filename: 340 self.save_as(event) 341 else: 342 if self.writefile(self.filename): 343 self.set_saved(True) 344 try: 345 self.editwin.store_file_breaks() 346 except AttributeError: # may be a PyShell 347 pass 348 self.text.focus_set() 349 return "break" 350 351 def save_as(self, event): 352 filename = self.asksavefile() 353 if filename: 354 if self.writefile(filename): 355 self.set_filename(filename) 356 self.set_saved(1) 357 try: 358 self.editwin.store_file_breaks() 359 except AttributeError: 360 pass 361 self.text.focus_set() 362 self.updaterecentfileslist(filename) 363 return "break" 364 365 def save_a_copy(self, event): 366 filename = self.asksavefile() 367 if filename: 368 self.writefile(filename) 369 self.text.focus_set() 370 self.updaterecentfileslist(filename) 371 return "break" 372 373 def writefile(self, filename): 374 text = self.fixnewlines() 375 chars = self.encode(text) 376 try: 377 with open(filename, "wb") as f: 378 f.write(chars) 379 f.flush() 380 os.fsync(f.fileno()) 381 return True 382 except OSError as msg: 383 tkMessageBox.showerror("I/O Error", str(msg), 384 parent=self.text) 385 return False 386 387 def fixnewlines(self): 388 "Return text with final \n if needed and os eols." 389 if (self.text.get("end-2c") != '\n' 390 and not hasattr(self.editwin, "interp")): # Not shell. 391 self.text.insert("end-1c", "\n") 392 text = self.text.get("1.0", "end-1c") 393 if self.eol_convention != "\n": 394 text = text.replace("\n", self.eol_convention) 395 return text 396 397 def encode(self, chars): 398 if isinstance(chars, bytes): 399 # This is either plain ASCII, or Tk was returning mixed-encoding 400 # text to us. Don't try to guess further. 401 return chars 402 # Preserve a BOM that might have been present on opening 403 if self.fileencoding == 'BOM': 404 return BOM_UTF8 + chars.encode("utf-8") 405 # See whether there is anything non-ASCII in it. 406 # If not, no need to figure out the encoding. 407 try: 408 return chars.encode('ascii') 409 except UnicodeError: 410 pass 411 # Check if there is an encoding declared 412 try: 413 # a string, let coding_spec slice it to the first two lines 414 enc = coding_spec(chars) 415 failed = None 416 except LookupError as msg: 417 failed = msg 418 enc = None 419 else: 420 if not enc: 421 # PEP 3120: default source encoding is UTF-8 422 enc = 'utf-8' 423 if enc: 424 try: 425 return chars.encode(enc) 426 except UnicodeError: 427 failed = "Invalid encoding '%s'" % enc 428 tkMessageBox.showerror( 429 "I/O Error", 430 "%s.\nSaving as UTF-8" % failed, 431 parent = self.text) 432 # Fallback: save as UTF-8, with BOM - ignoring the incorrect 433 # declared encoding 434 return BOM_UTF8 + chars.encode("utf-8") 435 436 def print_window(self, event): 437 confirm = tkMessageBox.askokcancel( 438 title="Print", 439 message="Print to Default Printer", 440 default=tkMessageBox.OK, 441 parent=self.text) 442 if not confirm: 443 self.text.focus_set() 444 return "break" 445 tempfilename = None 446 saved = self.get_saved() 447 if saved: 448 filename = self.filename 449 # shell undo is reset after every prompt, looks saved, probably isn't 450 if not saved or filename is None: 451 (tfd, tempfilename) = tempfile.mkstemp(prefix='IDLE_tmp_') 452 filename = tempfilename 453 os.close(tfd) 454 if not self.writefile(tempfilename): 455 os.unlink(tempfilename) 456 return "break" 457 platform = os.name 458 printPlatform = True 459 if platform == 'posix': #posix platform 460 command = idleConf.GetOption('main','General', 461 'print-command-posix') 462 command = command + " 2>&1" 463 elif platform == 'nt': #win32 platform 464 command = idleConf.GetOption('main','General','print-command-win') 465 else: #no printing for this platform 466 printPlatform = False 467 if printPlatform: #we can try to print for this platform 468 command = command % shlex.quote(filename) 469 pipe = os.popen(command, "r") 470 # things can get ugly on NT if there is no printer available. 471 output = pipe.read().strip() 472 status = pipe.close() 473 if status: 474 output = "Printing failed (exit status 0x%x)\n" % \ 475 status + output 476 if output: 477 output = "Printing command: %s\n" % repr(command) + output 478 tkMessageBox.showerror("Print status", output, parent=self.text) 479 else: #no printing for this platform 480 message = "Printing is not enabled for this platform: %s" % platform 481 tkMessageBox.showinfo("Print status", message, parent=self.text) 482 if tempfilename: 483 os.unlink(tempfilename) 484 return "break" 485 486 opendialog = None 487 savedialog = None 488 489 filetypes = ( 490 ("Python files", "*.py *.pyw", "TEXT"), 491 ("Text files", "*.txt", "TEXT"), 492 ("All files", "*"), 493 ) 494 495 defaultextension = '.py' if sys.platform == 'darwin' else '' 496 497 def askopenfile(self): 498 dir, base = self.defaultfilename("open") 499 if not self.opendialog: 500 self.opendialog = tkFileDialog.Open(parent=self.text, 501 filetypes=self.filetypes) 502 filename = self.opendialog.show(initialdir=dir, initialfile=base) 503 return filename 504 505 def defaultfilename(self, mode="open"): 506 if self.filename: 507 return os.path.split(self.filename) 508 elif self.dirname: 509 return self.dirname, "" 510 else: 511 try: 512 pwd = os.getcwd() 513 except OSError: 514 pwd = "" 515 return pwd, "" 516 517 def asksavefile(self): 518 dir, base = self.defaultfilename("save") 519 if not self.savedialog: 520 self.savedialog = tkFileDialog.SaveAs( 521 parent=self.text, 522 filetypes=self.filetypes, 523 defaultextension=self.defaultextension) 524 filename = self.savedialog.show(initialdir=dir, initialfile=base) 525 return filename 526 527 def updaterecentfileslist(self,filename): 528 "Update recent file list on all editor windows" 529 if self.editwin.flist: 530 self.editwin.update_recent_files_list(filename) 531 532def _io_binding(parent): # htest # 533 from tkinter import Toplevel, Text 534 535 root = Toplevel(parent) 536 root.title("Test IOBinding") 537 x, y = map(int, parent.geometry().split('+')[1:]) 538 root.geometry("+%d+%d" % (x, y + 175)) 539 class MyEditWin: 540 def __init__(self, text): 541 self.text = text 542 self.flist = None 543 self.text.bind("<Control-o>", self.open) 544 self.text.bind('<Control-p>', self.print) 545 self.text.bind("<Control-s>", self.save) 546 self.text.bind("<Alt-s>", self.saveas) 547 self.text.bind('<Control-c>', self.savecopy) 548 def get_saved(self): return 0 549 def set_saved(self, flag): pass 550 def reset_undo(self): pass 551 def open(self, event): 552 self.text.event_generate("<<open-window-from-file>>") 553 def print(self, event): 554 self.text.event_generate("<<print-window>>") 555 def save(self, event): 556 self.text.event_generate("<<save-window>>") 557 def saveas(self, event): 558 self.text.event_generate("<<save-window-as-file>>") 559 def savecopy(self, event): 560 self.text.event_generate("<<save-copy-of-window-as-file>>") 561 562 text = Text(root) 563 text.pack() 564 text.focus_set() 565 editwin = MyEditWin(text) 566 IOBinding(editwin) 567 568if __name__ == "__main__": 569 from unittest import main 570 main('idlelib.idle_test.test_iomenu', verbosity=2, exit=False) 571 572 from idlelib.idle_test.htest import run 573 run(_io_binding) 574