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

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