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

Python code coverage for Lib/idlelib/outwin.py

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