annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/iomenu.py @ 69:33d812a61356

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