ยปCore Development>Code coverage>Lib/lib-tk/tkFileDialog.py

Python code coverage for Lib/lib-tk/tkFileDialog.py

#countcontent
1n/a#
2n/a# Instant Python
3n/a# $Id: tkFileDialog.py 36560 2004-07-18 06:16:08Z tim_one $
4n/a#
5n/a# tk common file dialogues
6n/a#
7n/a# this module provides interfaces to the native file dialogues
8n/a# available in Tk 4.2 and newer, and the directory dialogue available
9n/a# in Tk 8.3 and newer.
10n/a#
11n/a# written by Fredrik Lundh, May 1997.
12n/a#
13n/a
14n/a#
15n/a# options (all have default values):
16n/a#
17n/a# - defaultextension: added to filename if not explicitly given
18n/a#
19n/a# - filetypes: sequence of (label, pattern) tuples. the same pattern
20n/a# may occur with several patterns. use "*" as pattern to indicate
21n/a# all files.
22n/a#
23n/a# - initialdir: initial directory. preserved by dialog instance.
24n/a#
25n/a# - initialfile: initial file (ignored by the open dialog). preserved
26n/a# by dialog instance.
27n/a#
28n/a# - parent: which window to place the dialog on top of
29n/a#
30n/a# - title: dialog title
31n/a#
32n/a# - multiple: if true user may select more than one file
33n/a#
34n/a# options for the directory chooser:
35n/a#
36n/a# - initialdir, parent, title: see above
37n/a#
38n/a# - mustexist: if true, user must pick an existing directory
39n/a#
40n/a#
41n/a
42n/a
43n/afrom tkCommonDialog import Dialog
44n/a
45n/aclass _Dialog(Dialog):
46n/a
47n/a def _fixoptions(self):
48n/a try:
49n/a # make sure "filetypes" is a tuple
50n/a self.options["filetypes"] = tuple(self.options["filetypes"])
51n/a except KeyError:
52n/a pass
53n/a
54n/a def _fixresult(self, widget, result):
55n/a if result:
56n/a # keep directory and filename until next time
57n/a import os
58n/a # convert Tcl path objects to strings
59n/a try:
60n/a result = result.string
61n/a except AttributeError:
62n/a # it already is a string
63n/a pass
64n/a path, file = os.path.split(result)
65n/a self.options["initialdir"] = path
66n/a self.options["initialfile"] = file
67n/a self.filename = result # compatibility
68n/a return result
69n/a
70n/a
71n/a#
72n/a# file dialogs
73n/a
74n/aclass Open(_Dialog):
75n/a "Ask for a filename to open"
76n/a
77n/a command = "tk_getOpenFile"
78n/a
79n/a def _fixresult(self, widget, result):
80n/a if isinstance(result, tuple):
81n/a # multiple results:
82n/a result = tuple([getattr(r, "string", r) for r in result])
83n/a if result:
84n/a import os
85n/a path, file = os.path.split(result[0])
86n/a self.options["initialdir"] = path
87n/a # don't set initialfile or filename, as we have multiple of these
88n/a return result
89n/a if not widget.tk.wantobjects() and "multiple" in self.options:
90n/a # Need to split result explicitly
91n/a return self._fixresult(widget, widget.tk.splitlist(result))
92n/a return _Dialog._fixresult(self, widget, result)
93n/a
94n/aclass SaveAs(_Dialog):
95n/a "Ask for a filename to save as"
96n/a
97n/a command = "tk_getSaveFile"
98n/a
99n/a
100n/a# the directory dialog has its own _fix routines.
101n/aclass Directory(Dialog):
102n/a "Ask for a directory"
103n/a
104n/a command = "tk_chooseDirectory"
105n/a
106n/a def _fixresult(self, widget, result):
107n/a if result:
108n/a # convert Tcl path objects to strings
109n/a try:
110n/a result = result.string
111n/a except AttributeError:
112n/a # it already is a string
113n/a pass
114n/a # keep directory until next time
115n/a self.options["initialdir"] = result
116n/a self.directory = result # compatibility
117n/a return result
118n/a
119n/a#
120n/a# convenience stuff
121n/a
122n/adef askopenfilename(**options):
123n/a "Ask for a filename to open"
124n/a
125n/a return Open(**options).show()
126n/a
127n/adef asksaveasfilename(**options):
128n/a "Ask for a filename to save as"
129n/a
130n/a return SaveAs(**options).show()
131n/a
132n/adef askopenfilenames(**options):
133n/a """Ask for multiple filenames to open
134n/a
135n/a Returns a list of filenames or empty list if
136n/a cancel button selected
137n/a """
138n/a options["multiple"]=1
139n/a return Open(**options).show()
140n/a
141n/a# FIXME: are the following perhaps a bit too convenient?
142n/a
143n/adef askopenfile(mode = "r", **options):
144n/a "Ask for a filename to open, and returned the opened file"
145n/a
146n/a filename = Open(**options).show()
147n/a if filename:
148n/a return open(filename, mode)
149n/a return None
150n/a
151n/adef askopenfiles(mode = "r", **options):
152n/a """Ask for multiple filenames and return the open file
153n/a objects
154n/a
155n/a returns a list of open file objects or an empty list if
156n/a cancel selected
157n/a """
158n/a
159n/a files = askopenfilenames(**options)
160n/a if files:
161n/a ofiles=[]
162n/a for filename in files:
163n/a ofiles.append(open(filename, mode))
164n/a files=ofiles
165n/a return files
166n/a
167n/a
168n/adef asksaveasfile(mode = "w", **options):
169n/a "Ask for a filename to save as, and returned the opened file"
170n/a
171n/a filename = SaveAs(**options).show()
172n/a if filename:
173n/a return open(filename, mode)
174n/a return None
175n/a
176n/adef askdirectory (**options):
177n/a "Ask for a directory, and return the file name"
178n/a return Directory(**options).show()
179n/a
180n/a# --------------------------------------------------------------------
181n/a# test stuff
182n/a
183n/aif __name__ == "__main__":
184n/a # Since the file name may contain non-ASCII characters, we need
185n/a # to find an encoding that likely supports the file name, and
186n/a # displays correctly on the terminal.
187n/a
188n/a # Start off with UTF-8
189n/a enc = "utf-8"
190n/a import sys
191n/a
192n/a # See whether CODESET is defined
193n/a try:
194n/a import locale
195n/a locale.setlocale(locale.LC_ALL,'')
196n/a enc = locale.nl_langinfo(locale.CODESET)
197n/a except (ImportError, AttributeError):
198n/a pass
199n/a
200n/a # dialog for openening files
201n/a
202n/a openfilename=askopenfilename(filetypes=[("all files", "*")])
203n/a try:
204n/a fp=open(openfilename,"r")
205n/a fp.close()
206n/a except:
207n/a print "Could not open File: "
208n/a print sys.exc_info()[1]
209n/a
210n/a print "open", openfilename.encode(enc)
211n/a
212n/a # dialog for saving files
213n/a
214n/a saveasfilename=asksaveasfilename()
215n/a print "saveas", saveasfilename.encode(enc)