configHelpSourceEdit.py 6.5 KB

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