/Lib/idlelib/configHelpSourceEdit.py

http://unladen-swallow.googlecode.com/ · Python · 169 lines · 148 code · 10 blank · 11 comment · 19 complexity · b1f9eeb0e7a7434bfac0816f2c63d8cc MD5 · raw file

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