Mercurial > cpython-lru_cache-weakref
annotate Lib/idlelib/configHelpSourceEdit.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | b53604fad54b |
| children |
| rev | line source |
|---|---|
| 27204 | 1 "Dialog to specify or edit the parameters for a user configured help source." |
| 2 | |
| 27245 | 3 import os |
|
32681
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
4 import sys |
| 27245 | 5 |
|
47183
b53604fad54b
Tkinter rename reversal: remove tkinter package, adapt imports and docs.
Georg Brandl <georg@python.org>
parents:
47101
diff
changeset
|
6 from Tkinter import * |
|
b53604fad54b
Tkinter rename reversal: remove tkinter package, adapt imports and docs.
Georg Brandl <georg@python.org>
parents:
47101
diff
changeset
|
7 import tkMessageBox |
|
b53604fad54b
Tkinter rename reversal: remove tkinter package, adapt imports and docs.
Georg Brandl <georg@python.org>
parents:
47101
diff
changeset
|
8 import tkFileDialog |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
9 |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
10 class GetHelpSourceDialog(Toplevel): |
| 27204 | 11 def __init__(self, parent, title, menuItem='', filePath=''): |
| 27245 | 12 """Get menu entry and url/ local file location for Additional Help |
| 13 | |
| 14 User selects a name for the Help resource and provides a web url | |
| 15 or a local file as its source. The user can enter a url or browse | |
| 16 for the file. | |
| 17 | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
18 """ |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
19 Toplevel.__init__(self, parent) |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
20 self.configure(borderwidth=5) |
| 27204 | 21 self.resizable(height=FALSE, width=FALSE) |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
22 self.title(title) |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
23 self.transient(parent) |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
24 self.grab_set() |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
25 self.protocol("WM_DELETE_WINDOW", self.Cancel) |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
26 self.parent = parent |
| 27204 | 27 self.result = None |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
28 self.CreateWidgets() |
|
22600
0c5c64b75d5d
further work on new config system;
Steven M. Gava <elguavas@python.net>
parents:
21875
diff
changeset
|
29 self.menu.set(menuItem) |
|
0c5c64b75d5d
further work on new config system;
Steven M. Gava <elguavas@python.net>
parents:
21875
diff
changeset
|
30 self.path.set(filePath) |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
31 self.withdraw() #hide while setting geometry |
| 27204 | 32 #needs to be done here so that the winfo_reqwidth is valid |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
33 self.update_idletasks() |
| 27204 | 34 #centre dialog over parent: |
| 26959 | 35 self.geometry("+%d+%d" % |
| 27204 | 36 ((parent.winfo_rootx() + ((parent.winfo_width()/2) |
| 37 -(self.winfo_reqwidth()/2)), | |
| 38 parent.winfo_rooty() + ((parent.winfo_height()/2) | |
| 39 -(self.winfo_reqheight()/2))))) | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
40 self.deiconify() #geometry set, unhide |
| 27204 | 41 self.bind('<Return>', self.Ok) |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
42 self.wait_window() |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
43 |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
44 def CreateWidgets(self): |
| 27204 | 45 self.menu = StringVar(self) |
| 46 self.path = StringVar(self) | |
| 47 self.fontSize = StringVar(self) | |
| 27245 | 48 self.frameMain = Frame(self, borderwidth=2, relief=GROOVE) |
| 27204 | 49 self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH) |
| 50 labelMenu = Label(self.frameMain, anchor=W, justify=LEFT, | |
| 51 text='Menu Item:') | |
| 52 self.entryMenu = Entry(self.frameMain, textvariable=self.menu, | |
| 53 width=30) | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
54 self.entryMenu.focus_set() |
| 27204 | 55 labelPath = Label(self.frameMain, anchor=W, justify=LEFT, |
| 27245 | 56 text='Help File Path: Enter URL or browse for file') |
| 27204 | 57 self.entryPath = Entry(self.frameMain, textvariable=self.path, |
| 58 width=40) | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
59 self.entryMenu.focus_set() |
| 27204 | 60 labelMenu.pack(anchor=W, padx=5, pady=3) |
| 61 self.entryMenu.pack(anchor=W, padx=5, pady=3) | |
| 62 labelPath.pack(anchor=W, padx=5, pady=3) | |
| 63 self.entryPath.pack(anchor=W, padx=5, pady=3) | |
| 27245 | 64 browseButton = Button(self.frameMain, text='Browse', width=8, |
| 65 command=self.browseFile) | |
| 66 browseButton.pack(pady=3) | |
| 27204 | 67 frameButtons = Frame(self) |
| 68 frameButtons.pack(side=BOTTOM, fill=X) | |
| 69 self.buttonOk = Button(frameButtons, text='OK', | |
| 70 width=8, default=ACTIVE, command=self.Ok) | |
| 71 self.buttonOk.grid(row=0, column=0, padx=5,pady=5) | |
| 72 self.buttonCancel = Button(frameButtons, text='Cancel', | |
| 73 width=8, command=self.Cancel) | |
| 74 self.buttonCancel.grid(row=0, column=1, padx=5, pady=5) | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
75 |
| 27245 | 76 def browseFile(self): |
| 77 filetypes = [ | |
| 78 ("HTML Files", "*.htm *.html", "TEXT"), | |
| 79 ("PDF Files", "*.pdf", "TEXT"), | |
| 80 ("Windows Help Files", "*.chm"), | |
| 81 ("Text Files", "*.txt", "TEXT"), | |
| 82 ("All Files", "*")] | |
| 83 path = self.path.get() | |
| 84 if path: | |
| 85 dir, base = os.path.split(path) | |
| 86 else: | |
| 87 base = None | |
|
32681
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
88 if sys.platform[:3] == 'win': |
| 27245 | 89 dir = os.path.join(os.path.dirname(sys.executable), 'Doc') |
| 90 if not os.path.isdir(dir): | |
| 91 dir = os.getcwd() | |
| 92 else: | |
| 93 dir = os.getcwd() | |
| 94 opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes) | |
| 95 file = opendialog.show(initialdir=dir, initialfile=base) | |
| 96 if file: | |
| 97 self.path.set(file) | |
| 27409 | 98 |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
99 def MenuOk(self): |
| 27204 | 100 "Simple validity check for a sensible menu item name" |
| 101 menuOk = True | |
| 102 menu = self.menu.get() | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
103 menu.strip() |
| 27204 | 104 if not menu: |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
105 tkMessageBox.showerror(title='Menu Item Error', |
| 27204 | 106 message='No menu item specified', |
| 107 parent=self) | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
108 self.entryMenu.focus_set() |
| 27204 | 109 menuOk = False |
| 110 elif len(menu) > 30: | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
111 tkMessageBox.showerror(title='Menu Item Error', |
| 27204 | 112 message='Menu item too long:' |
| 113 '\nLimit 30 characters.', | |
| 114 parent=self) | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
115 self.entryMenu.focus_set() |
| 27204 | 116 menuOk = False |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
117 return menuOk |
| 26959 | 118 |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
119 def PathOk(self): |
| 27204 | 120 "Simple validity check for menu file path" |
| 121 pathOk = True | |
| 122 path = self.path.get() | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
123 path.strip() |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
124 if not path: #no path specified |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
125 tkMessageBox.showerror(title='File Path Error', |
| 27204 | 126 message='No help file path specified.', |
| 127 parent=self) | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
128 self.entryPath.focus_set() |
| 27204 | 129 pathOk = False |
|
38704
ba6eebf7c719
Make use of new str.startswith/endswith semantics.
Georg Brandl <georg@python.org>
parents:
38334
diff
changeset
|
130 elif path.startswith(('www.', 'http')): |
|
32681
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
131 pass |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
132 else: |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
133 if path[:5] == 'file:': |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
134 path = path[5:] |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
135 if not os.path.exists(path): |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
136 tkMessageBox.showerror(title='File Path Error', |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
137 message='Help file path does not exist.', |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
138 parent=self) |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
139 self.entryPath.focus_set() |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
140 pathOk = False |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
141 return pathOk |
| 26959 | 142 |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
143 def Ok(self, event=None): |
| 27204 | 144 if self.MenuOk() and self.PathOk(): |
| 145 self.result = (self.menu.get().strip(), | |
| 146 self.path.get().strip()) | |
|
32681
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
147 if sys.platform == 'darwin': |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
148 path = self.result[1] |
|
38704
ba6eebf7c719
Make use of new str.startswith/endswith semantics.
Georg Brandl <georg@python.org>
parents:
38334
diff
changeset
|
149 if path.startswith(('www', 'file:', 'http:')): |
|
32681
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
150 pass |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
151 else: |
|
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
152 # Mac Safari insists on using the URI form for local files |
|
38334
5a147365527e
Without this patch OSX users couldn't add new help sources because the code
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
32681
diff
changeset
|
153 self.result = list(self.result) |
|
32681
104a7fc06bdf
Checking sys.platform for substring 'win' was breaking IDLE docs on Mac
Kurt B. Kaiser <kbk@shore.net>
parents:
27409
diff
changeset
|
154 self.result[1] = "file://" + path |
| 27204 | 155 self.destroy() |
| 26959 | 156 |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
157 def Cancel(self, event=None): |
| 27204 | 158 self.result = None |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
159 self.destroy() |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
160 |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
161 if __name__ == '__main__': |
|
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
162 #test the dialog |
| 27204 | 163 root = Tk() |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
164 def run(): |
| 27204 | 165 keySeq = '' |
| 166 dlg = GetHelpSourceDialog(root, 'Get Help Source') | |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
167 print dlg.result |
| 27204 | 168 Button(root,text='Dialog', command=run).pack() |
|
21875
7faf0fddf7e6
further work on config system and config saving
Steven M. Gava <elguavas@python.net>
parents:
diff
changeset
|
169 root.mainloop() |
