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

Python code coverage for Lib/idlelib/filelist.py

#countcontent
1n/aimport os
2n/a
3n/afrom tkinter import *
4n/aimport tkinter.messagebox as tkMessageBox
5n/a
6n/a
7n/aclass FileList:
8n/a
9n/a # N.B. this import overridden in PyShellFileList.
10n/a from idlelib.editor import EditorWindow
11n/a
12n/a def __init__(self, root):
13n/a self.root = root
14n/a self.dict = {}
15n/a self.inversedict = {}
16n/a self.vars = {} # For EditorWindow.getrawvar (shared Tcl variables)
17n/a
18n/a def open(self, filename, action=None):
19n/a assert filename
20n/a filename = self.canonize(filename)
21n/a if os.path.isdir(filename):
22n/a # This can happen when bad filename is passed on command line:
23n/a tkMessageBox.showerror(
24n/a "File Error",
25n/a "%r is a directory." % (filename,),
26n/a master=self.root)
27n/a return None
28n/a key = os.path.normcase(filename)
29n/a if key in self.dict:
30n/a edit = self.dict[key]
31n/a edit.top.wakeup()
32n/a return edit
33n/a if action:
34n/a # Don't create window, perform 'action', e.g. open in same window
35n/a return action(filename)
36n/a else:
37n/a edit = self.EditorWindow(self, filename, key)
38n/a if edit.good_load:
39n/a return edit
40n/a else:
41n/a edit._close()
42n/a return None
43n/a
44n/a def gotofileline(self, filename, lineno=None):
45n/a edit = self.open(filename)
46n/a if edit is not None and lineno is not None:
47n/a edit.gotoline(lineno)
48n/a
49n/a def new(self, filename=None):
50n/a return self.EditorWindow(self, filename)
51n/a
52n/a def close_all_callback(self, *args, **kwds):
53n/a for edit in list(self.inversedict):
54n/a reply = edit.close()
55n/a if reply == "cancel":
56n/a break
57n/a return "break"
58n/a
59n/a def unregister_maybe_terminate(self, edit):
60n/a try:
61n/a key = self.inversedict[edit]
62n/a except KeyError:
63n/a print("Don't know this EditorWindow object. (close)")
64n/a return
65n/a if key:
66n/a del self.dict[key]
67n/a del self.inversedict[edit]
68n/a if not self.inversedict:
69n/a self.root.quit()
70n/a
71n/a def filename_changed_edit(self, edit):
72n/a edit.saved_change_hook()
73n/a try:
74n/a key = self.inversedict[edit]
75n/a except KeyError:
76n/a print("Don't know this EditorWindow object. (rename)")
77n/a return
78n/a filename = edit.io.filename
79n/a if not filename:
80n/a if key:
81n/a del self.dict[key]
82n/a self.inversedict[edit] = None
83n/a return
84n/a filename = self.canonize(filename)
85n/a newkey = os.path.normcase(filename)
86n/a if newkey == key:
87n/a return
88n/a if newkey in self.dict:
89n/a conflict = self.dict[newkey]
90n/a self.inversedict[conflict] = None
91n/a tkMessageBox.showerror(
92n/a "Name Conflict",
93n/a "You now have multiple edit windows open for %r" % (filename,),
94n/a master=self.root)
95n/a self.dict[newkey] = edit
96n/a self.inversedict[edit] = newkey
97n/a if key:
98n/a try:
99n/a del self.dict[key]
100n/a except KeyError:
101n/a pass
102n/a
103n/a def canonize(self, filename):
104n/a if not os.path.isabs(filename):
105n/a try:
106n/a pwd = os.getcwd()
107n/a except OSError:
108n/a pass
109n/a else:
110n/a filename = os.path.join(pwd, filename)
111n/a return os.path.normpath(filename)
112n/a
113n/a
114n/adef _test():
115n/a from idlelib.editor import fixwordbreaks
116n/a import sys
117n/a root = Tk()
118n/a fixwordbreaks(root)
119n/a root.withdraw()
120n/a flist = FileList(root)
121n/a if sys.argv[1:]:
122n/a for filename in sys.argv[1:]:
123n/a flist.open(filename)
124n/a else:
125n/a flist.new()
126n/a if flist.inversedict:
127n/a root.mainloop()
128n/a
129n/aif __name__ == '__main__':
130n/a _test()