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