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

Python code coverage for Lib/idlelib/config_help.py

#countcontent
1n/a"Dialog to specify or edit the parameters for a user configured help source."
2n/a
3n/aimport os
4n/aimport sys
5n/a
6n/afrom tkinter import *
7n/aimport tkinter.messagebox as tkMessageBox
8n/aimport tkinter.filedialog as tkFileDialog
9n/a
10n/aclass GetHelpSourceDialog(Toplevel):
11n/a def __init__(self, parent, title, menuItem='', filePath='', _htest=False):
12n/a """Get menu entry and url/ local file location for Additional Help
13n/a
14n/a User selects a name for the Help resource and provides a web url
15n/a or a local file as its source. The user can enter a url or browse
16n/a for the file.
17n/a
18n/a _htest - bool, change box location when running htest
19n/a """
20n/a Toplevel.__init__(self, parent)
21n/a self.configure(borderwidth=5)
22n/a self.resizable(height=FALSE, width=FALSE)
23n/a self.title(title)
24n/a self.transient(parent)
25n/a self.grab_set()
26n/a self.protocol("WM_DELETE_WINDOW", self.cancel)
27n/a self.parent = parent
28n/a self.result = None
29n/a self.create_widgets()
30n/a self.menu.set(menuItem)
31n/a self.path.set(filePath)
32n/a self.withdraw() #hide while setting geometry
33n/a #needs to be done here so that the winfo_reqwidth is valid
34n/a self.update_idletasks()
35n/a #centre dialog over parent. below parent if running htest.
36n/a self.geometry(
37n/a "+%d+%d" % (
38n/a parent.winfo_rootx() +
39n/a (parent.winfo_width()/2 - self.winfo_reqwidth()/2),
40n/a parent.winfo_rooty() +
41n/a ((parent.winfo_height()/2 - self.winfo_reqheight()/2)
42n/a if not _htest else 150)))
43n/a self.deiconify() #geometry set, unhide
44n/a self.bind('<Return>', self.ok)
45n/a self.wait_window()
46n/a
47n/a def create_widgets(self):
48n/a self.menu = StringVar(self)
49n/a self.path = StringVar(self)
50n/a self.fontSize = StringVar(self)
51n/a self.frameMain = Frame(self, borderwidth=2, relief=GROOVE)
52n/a self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
53n/a labelMenu = Label(self.frameMain, anchor=W, justify=LEFT,
54n/a text='Menu Item:')
55n/a self.entryMenu = Entry(self.frameMain, textvariable=self.menu,
56n/a width=30)
57n/a self.entryMenu.focus_set()
58n/a labelPath = Label(self.frameMain, anchor=W, justify=LEFT,
59n/a text='Help File Path: Enter URL or browse for file')
60n/a self.entryPath = Entry(self.frameMain, textvariable=self.path,
61n/a width=40)
62n/a self.entryMenu.focus_set()
63n/a labelMenu.pack(anchor=W, padx=5, pady=3)
64n/a self.entryMenu.pack(anchor=W, padx=5, pady=3)
65n/a labelPath.pack(anchor=W, padx=5, pady=3)
66n/a self.entryPath.pack(anchor=W, padx=5, pady=3)
67n/a browseButton = Button(self.frameMain, text='Browse', width=8,
68n/a command=self.browse_file)
69n/a browseButton.pack(pady=3)
70n/a frameButtons = Frame(self)
71n/a frameButtons.pack(side=BOTTOM, fill=X)
72n/a self.buttonOk = Button(frameButtons, text='OK',
73n/a width=8, default=ACTIVE, command=self.ok)
74n/a self.buttonOk.grid(row=0, column=0, padx=5,pady=5)
75n/a self.buttonCancel = Button(frameButtons, text='Cancel',
76n/a width=8, command=self.cancel)
77n/a self.buttonCancel.grid(row=0, column=1, padx=5, pady=5)
78n/a
79n/a def browse_file(self):
80n/a filetypes = [
81n/a ("HTML Files", "*.htm *.html", "TEXT"),
82n/a ("PDF Files", "*.pdf", "TEXT"),
83n/a ("Windows Help Files", "*.chm"),
84n/a ("Text Files", "*.txt", "TEXT"),
85n/a ("All Files", "*")]
86n/a path = self.path.get()
87n/a if path:
88n/a dir, base = os.path.split(path)
89n/a else:
90n/a base = None
91n/a if sys.platform[:3] == 'win':
92n/a dir = os.path.join(os.path.dirname(sys.executable), 'Doc')
93n/a if not os.path.isdir(dir):
94n/a dir = os.getcwd()
95n/a else:
96n/a dir = os.getcwd()
97n/a opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes)
98n/a file = opendialog.show(initialdir=dir, initialfile=base)
99n/a if file:
100n/a self.path.set(file)
101n/a
102n/a def menu_ok(self):
103n/a "Simple validity check for a sensible menu item name"
104n/a menu_ok = True
105n/a menu = self.menu.get()
106n/a menu.strip()
107n/a if not menu:
108n/a tkMessageBox.showerror(title='Menu Item Error',
109n/a message='No menu item specified',
110n/a parent=self)
111n/a self.entryMenu.focus_set()
112n/a menu_ok = False
113n/a elif len(menu) > 30:
114n/a tkMessageBox.showerror(title='Menu Item Error',
115n/a message='Menu item too long:'
116n/a '\nLimit 30 characters.',
117n/a parent=self)
118n/a self.entryMenu.focus_set()
119n/a menu_ok = False
120n/a return menu_ok
121n/a
122n/a def path_ok(self):
123n/a "Simple validity check for menu file path"
124n/a path_ok = True
125n/a path = self.path.get()
126n/a path.strip()
127n/a if not path: #no path specified
128n/a tkMessageBox.showerror(title='File Path Error',
129n/a message='No help file path specified.',
130n/a parent=self)
131n/a self.entryPath.focus_set()
132n/a path_ok = False
133n/a elif path.startswith(('www.', 'http')):
134n/a pass
135n/a else:
136n/a if path[:5] == 'file:':
137n/a path = path[5:]
138n/a if not os.path.exists(path):
139n/a tkMessageBox.showerror(title='File Path Error',
140n/a message='Help file path does not exist.',
141n/a parent=self)
142n/a self.entryPath.focus_set()
143n/a path_ok = False
144n/a return path_ok
145n/a
146n/a def ok(self, event=None):
147n/a if self.menu_ok() and self.path_ok():
148n/a self.result = (self.menu.get().strip(),
149n/a self.path.get().strip())
150n/a if sys.platform == 'darwin':
151n/a path = self.result[1]
152n/a if path.startswith(('www', 'file:', 'http:', 'https:')):
153n/a pass
154n/a else:
155n/a # Mac Safari insists on using the URI form for local files
156n/a self.result = list(self.result)
157n/a self.result[1] = "file://" + path
158n/a self.destroy()
159n/a
160n/a def cancel(self, event=None):
161n/a self.result = None
162n/a self.destroy()
163n/a
164n/aif __name__ == '__main__':
165n/a import unittest
166n/a unittest.main('idlelib.idle_test.test_config_help',
167n/a verbosity=2, exit=False)
168n/a
169n/a from idlelib.idle_test.htest import run
170n/a run(GetHelpSourceDialog)