ยปCore Development>Code coverage>Lib/idlelib/OutputWindow.py

Python code coverage for Lib/idlelib/OutputWindow.py

#countcontent
1n/afrom tkinter import *
2n/afrom idlelib.EditorWindow import EditorWindow
3n/aimport re
4n/aimport tkinter.messagebox as tkMessageBox
5n/afrom idlelib import IOBinding
6n/a
7n/aclass OutputWindow(EditorWindow):
8n/a
9n/a """An editor window that can serve as an output file.
10n/a
11n/a Also the future base class for the Python shell window.
12n/a This class has no input facilities.
13n/a """
14n/a
15n/a def __init__(self, *args):
16n/a EditorWindow.__init__(self, *args)
17n/a self.text.bind("<<goto-file-line>>", self.goto_file_line)
18n/a
19n/a # Customize EditorWindow
20n/a
21n/a def ispythonsource(self, filename):
22n/a # No colorization needed
23n/a return 0
24n/a
25n/a def short_title(self):
26n/a return "Output"
27n/a
28n/a def maybesave(self):
29n/a # Override base class method -- don't ask any questions
30n/a if self.get_saved():
31n/a return "yes"
32n/a else:
33n/a return "no"
34n/a
35n/a # Act as output file
36n/a
37n/a def write(self, s, tags=(), mark="insert"):
38n/a if isinstance(s, (bytes, bytes)):
39n/a s = s.decode(IOBinding.encoding, "replace")
40n/a self.text.insert(mark, s, tags)
41n/a self.text.see(mark)
42n/a self.text.update()
43n/a return len(s)
44n/a
45n/a def writelines(self, lines):
46n/a for line in lines:
47n/a self.write(line)
48n/a
49n/a def flush(self):
50n/a pass
51n/a
52n/a # Our own right-button menu
53n/a
54n/a rmenu_specs = [
55n/a ("Cut", "<<cut>>", "rmenu_check_cut"),
56n/a ("Copy", "<<copy>>", "rmenu_check_copy"),
57n/a ("Paste", "<<paste>>", "rmenu_check_paste"),
58n/a (None, None, None),
59n/a ("Go to file/line", "<<goto-file-line>>", None),
60n/a ]
61n/a
62n/a file_line_pats = [
63n/a # order of patterns matters
64n/a r'file "([^"]*)", line (\d+)',
65n/a r'([^\s]+)\((\d+)\)',
66n/a r'^(\s*\S.*?):\s*(\d+):', # Win filename, maybe starting with spaces
67n/a r'([^\s]+):\s*(\d+):', # filename or path, ltrim
68n/a r'^\s*(\S.*?):\s*(\d+):', # Win abs path with embedded spaces, ltrim
69n/a ]
70n/a
71n/a file_line_progs = None
72n/a
73n/a def goto_file_line(self, event=None):
74n/a if self.file_line_progs is None:
75n/a l = []
76n/a for pat in self.file_line_pats:
77n/a l.append(re.compile(pat, re.IGNORECASE))
78n/a self.file_line_progs = l
79n/a # x, y = self.event.x, self.event.y
80n/a # self.text.mark_set("insert", "@%d,%d" % (x, y))
81n/a line = self.text.get("insert linestart", "insert lineend")
82n/a result = self._file_line_helper(line)
83n/a if not result:
84n/a # Try the previous line. This is handy e.g. in tracebacks,
85n/a # where you tend to right-click on the displayed source line
86n/a line = self.text.get("insert -1line linestart",
87n/a "insert -1line lineend")
88n/a result = self._file_line_helper(line)
89n/a if not result:
90n/a tkMessageBox.showerror(
91n/a "No special line",
92n/a "The line you point at doesn't look like "
93n/a "a valid file name followed by a line number.",
94n/a master=self.text)
95n/a return
96n/a filename, lineno = result
97n/a edit = self.flist.open(filename)
98n/a edit.gotoline(lineno)
99n/a
100n/a def _file_line_helper(self, line):
101n/a for prog in self.file_line_progs:
102n/a match = prog.search(line)
103n/a if match:
104n/a filename, lineno = match.group(1, 2)
105n/a try:
106n/a f = open(filename, "r")
107n/a f.close()
108n/a break
109n/a except OSError:
110n/a continue
111n/a else:
112n/a return None
113n/a try:
114n/a return filename, int(lineno)
115n/a except TypeError:
116n/a return None
117n/a
118n/a# These classes are currently not used but might come in handy
119n/a
120n/aclass OnDemandOutputWindow:
121n/a
122n/a tagdefs = {
123n/a # XXX Should use IdlePrefs.ColorPrefs
124n/a "stdout": {"foreground": "blue"},
125n/a "stderr": {"foreground": "#007700"},
126n/a }
127n/a
128n/a def __init__(self, flist):
129n/a self.flist = flist
130n/a self.owin = None
131n/a
132n/a def write(self, s, tags, mark):
133n/a if not self.owin:
134n/a self.setup()
135n/a self.owin.write(s, tags, mark)
136n/a
137n/a def setup(self):
138n/a self.owin = owin = OutputWindow(self.flist)
139n/a text = owin.text
140n/a for tag, cnf in self.tagdefs.items():
141n/a if cnf:
142n/a text.tag_configure(tag, **cnf)
143n/a text.tag_raise('sel')
144n/a self.write = self.owin.write