annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/iomenu.py @ 68:5028fdace37b

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