Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 1 | """Editor window that can serve as an output file. |
| 2 | """ |
| 3 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 4 | import re |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 5 | |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 6 | from tkinter import messagebox |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 7 | |
| 8 | from idlelib.editor import EditorWindow |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 9 | from idlelib import iomenu |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 10 | |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 11 | |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 12 | file_line_pats = [ |
| 13 | # order of patterns matters |
| 14 | r'file "([^"]*)", line (\d+)', |
| 15 | r'([^\s]+)\((\d+)\)', |
| 16 | r'^(\s*\S.*?):\s*(\d+):', # Win filename, maybe starting with spaces |
| 17 | r'([^\s]+):\s*(\d+):', # filename or path, ltrim |
| 18 | r'^\s*(\S.*?):\s*(\d+):', # Win abs path with embedded spaces, ltrim |
| 19 | ] |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 20 | |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 21 | file_line_progs = None |
| 22 | |
| 23 | |
| 24 | def compile_progs(): |
| 25 | "Compile the patterns for matching to file name and line number." |
| 26 | global file_line_progs |
| 27 | file_line_progs = [re.compile(pat, re.IGNORECASE) |
| 28 | for pat in file_line_pats] |
| 29 | |
| 30 | |
| 31 | def file_line_helper(line): |
| 32 | """Extract file name and line number from line of text. |
| 33 | |
| 34 | Check if line of text contains one of the file/line patterns. |
| 35 | If it does and if the file and line are valid, return |
| 36 | a tuple of the file name and line number. If it doesn't match |
| 37 | or if the file or line is invalid, return None. |
| 38 | """ |
| 39 | if not file_line_progs: |
| 40 | compile_progs() |
| 41 | for prog in file_line_progs: |
| 42 | match = prog.search(line) |
| 43 | if match: |
| 44 | filename, lineno = match.group(1, 2) |
| 45 | try: |
| 46 | f = open(filename, "r") |
| 47 | f.close() |
| 48 | break |
| 49 | except OSError: |
| 50 | continue |
| 51 | else: |
| 52 | return None |
| 53 | try: |
| 54 | return filename, int(lineno) |
| 55 | except TypeError: |
| 56 | return None |
| 57 | |
| 58 | |
| 59 | class OutputWindow(EditorWindow): |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 60 | """An editor window that can serve as an output file. |
| 61 | |
| 62 | Also the future base class for the Python shell window. |
| 63 | This class has no input facilities. |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 64 | |
| 65 | Adds binding to open a file at a line to the text widget. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 66 | """ |
| 67 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 68 | # Our own right-button menu |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 69 | rmenu_specs = [ |
Andrew Svetlov | d183767 | 2012-11-01 22:41:19 +0200 | [diff] [blame] | 70 | ("Cut", "<<cut>>", "rmenu_check_cut"), |
| 71 | ("Copy", "<<copy>>", "rmenu_check_copy"), |
| 72 | ("Paste", "<<paste>>", "rmenu_check_paste"), |
| 73 | (None, None, None), |
| 74 | ("Go to file/line", "<<goto-file-line>>", None), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 75 | ] |
| 76 | |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 77 | def __init__(self, *args): |
| 78 | EditorWindow.__init__(self, *args) |
| 79 | self.text.bind("<<goto-file-line>>", self.goto_file_line) |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 80 | self.text.unbind("<<toggle-code-context>>") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 81 | |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 82 | # Customize EditorWindow |
| 83 | def ispythonsource(self, filename): |
| 84 | "Python source is only part of output: do not colorize." |
| 85 | return False |
| 86 | |
| 87 | def short_title(self): |
| 88 | "Customize EditorWindow title." |
| 89 | return "Output" |
| 90 | |
| 91 | def maybesave(self): |
| 92 | "Customize EditorWindow to not display save file messagebox." |
| 93 | return 'yes' if self.get_saved() else 'no' |
| 94 | |
| 95 | # Act as output file |
| 96 | def write(self, s, tags=(), mark="insert"): |
| 97 | """Write text to text widget. |
| 98 | |
| 99 | The text is inserted at the given index with the provided |
| 100 | tags. The text widget is then scrolled to make it visible |
| 101 | and updated to display it, giving the effect of seeing each |
| 102 | line as it is added. |
| 103 | |
| 104 | Args: |
| 105 | s: Text to insert into text widget. |
| 106 | tags: Tuple of tag strings to apply on the insert. |
| 107 | mark: Index for the insert. |
| 108 | |
| 109 | Return: |
| 110 | Length of text inserted. |
| 111 | """ |
| 112 | if isinstance(s, (bytes, bytes)): |
| 113 | s = s.decode(iomenu.encoding, "replace") |
| 114 | self.text.insert(mark, s, tags) |
| 115 | self.text.see(mark) |
| 116 | self.text.update() |
| 117 | return len(s) |
| 118 | |
| 119 | def writelines(self, lines): |
| 120 | "Write each item in lines iterable." |
| 121 | for line in lines: |
| 122 | self.write(line) |
| 123 | |
| 124 | def flush(self): |
| 125 | "No flushing needed as write() directly writes to widget." |
| 126 | pass |
| 127 | |
| 128 | def showerror(self, *args, **kwargs): |
| 129 | messagebox.showerror(*args, **kwargs) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 130 | |
| 131 | def goto_file_line(self, event=None): |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 132 | """Handle request to open file/line. |
| 133 | |
| 134 | If the selected or previous line in the output window |
| 135 | contains a file name and line number, then open that file |
| 136 | name in a new window and position on the line number. |
| 137 | |
| 138 | Otherwise, display an error messagebox. |
| 139 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 140 | line = self.text.get("insert linestart", "insert lineend") |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 141 | result = file_line_helper(line) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 142 | if not result: |
| 143 | # Try the previous line. This is handy e.g. in tracebacks, |
| 144 | # where you tend to right-click on the displayed source line |
| 145 | line = self.text.get("insert -1line linestart", |
| 146 | "insert -1line lineend") |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 147 | result = file_line_helper(line) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 148 | if not result: |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 149 | self.showerror( |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 150 | "No special line", |
| 151 | "The line you point at doesn't look like " |
| 152 | "a valid file name followed by a line number.", |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 153 | parent=self.text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 154 | return |
| 155 | filename, lineno = result |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 156 | self.flist.gotofileline(filename, lineno) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 157 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 158 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 159 | # These classes are currently not used but might come in handy |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 160 | class OnDemandOutputWindow: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 161 | |
| 162 | tagdefs = { |
| 163 | # XXX Should use IdlePrefs.ColorPrefs |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 164 | "stdout": {"foreground": "blue"}, |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 165 | "stderr": {"foreground": "#007700"}, |
| 166 | } |
| 167 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 168 | def __init__(self, flist): |
| 169 | self.flist = flist |
| 170 | self.owin = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 171 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 172 | def write(self, s, tags, mark): |
| 173 | if not self.owin: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 174 | self.setup() |
| 175 | self.owin.write(s, tags, mark) |
| 176 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 177 | def setup(self): |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 178 | self.owin = owin = OutputWindow(self.flist) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 179 | text = owin.text |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 180 | for tag, cnf in self.tagdefs.items(): |
| 181 | if cnf: |
Raymond Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 182 | text.tag_configure(tag, **cnf) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 183 | text.tag_raise('sel') |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 184 | self.write = self.owin.write |
Cheryl Sabella | 998f496 | 2017-08-27 18:06:00 -0400 | [diff] [blame] | 185 | |
| 186 | if __name__ == '__main__': |
Terry Jan Reedy | 4d92158 | 2018-06-19 19:12:52 -0400 | [diff] [blame] | 187 | from unittest import main |
| 188 | main('idlelib.idle_test.test_outwin', verbosity=2, exit=False) |