PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/rstlink.py

https://bitbucket.org/christoomey/restedit
Python | 210 lines | 209 code | 1 blank | 0 comment | 0 complexity | 2c47d4bb69d5b12bf44bb289476a6a32 MD5 | raw file
  1. import os
  2. import wx
  3. import wx.html as html
  4. import subprocess
  5. R2HPATH = 'C:\\work\\process\\docutils\\tools\\'
  6. LISTSDIR = 'C:\\work\\process\\lists'
  7. INPUTPATH = 'C:\\work\\process\\lists\\macapps.rst'
  8. class TestingFrame(wx.Frame):
  9. '''A simple frame to wrap around the edit panel'''
  10. def __init__(self, parent=None):
  11. super(TestingFrame, self).__init__(parent=None)
  12. self.controller = Controller(self)
  13. self.panel = RealTimePanel(self, self.controller)
  14. self.statusbar = wx.StatusBar(self, wx.ID_ANY)
  15. self.SetStatusBar(self.statusbar)
  16. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  17. self.SetSize((500, 800))
  18. self.SetPosition((100,50))
  19. self.statusbar.SetLabel('OMGZFTWBBQ!')
  20. def OnCloseWindow(self, event):
  21. '''Clean up that memory'''
  22. self.Destroy()
  23. class RealTimePanel(wx.Panel):
  24. '''A panel to collect the edit & display controls'''
  25. def __init__(self, parent, controller):
  26. super(RealTimePanel, self).__init__(parent)
  27. self.controller = controller
  28. btnPanel = BtnPanel(self, controller)
  29. editPanel = EditPanel(self, controller)
  30. b = 5
  31. hsizer1 = wx.BoxSizer(wx.VERTICAL)
  32. hsizer1.Add(btnPanel, 0, wx.EXPAND | wx.ALL, b)
  33. hsizer1.Add(editPanel, 1, wx.EXPAND | wx.ALL, b)
  34. self.SetSizer(hsizer1)
  35. def updateHtml(self):
  36. '''Attempts to compile the text into HTMl
  37. The current text in the edit pane is passed out the rst2html.py
  38. script, and if it returns valid HTML, that is rendered into the
  39. display window.'''
  40. pass
  41. class BtnPanel(wx.Panel):
  42. '''Simple horizontal list of the buttons'''
  43. ID_LOAD = 1001
  44. ID_SAVE = 1002
  45. def __init__(self, parent, controller):
  46. super(BtnPanel, self).__init__(parent)
  47. self.controller = controller
  48. btnLink = wx.Button(self, 1001, 'Link')
  49. btnConvert = wx.Button(self, 1003, 'Convert')
  50. btnEnable = wx.Button(self, 1003, 'Enable')
  51. btnDisable = wx.Button(self, 1003, 'Disable')
  52. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  53. btnSizer.Add(btnLink, 0)
  54. btnSizer.AddSpacer((5,-1))
  55. btnSizer.Add(btnConvert, 0)
  56. btnSizer.AddSpacer((5,-1))
  57. btnSizer.Add(btnEnable, 0)
  58. btnSizer.AddSpacer((5,-1))
  59. btnSizer.Add(btnDisable, 0)
  60. btnLink.Bind(wx.EVT_BUTTON, self.onLink)
  61. btnConvert.Bind(wx.EVT_BUTTON, self.onUpdate)
  62. btnEnable.Bind(wx.EVT_BUTTON, self.onEnable)
  63. btnDisable.Bind(wx.EVT_BUTTON, self.onDisable)
  64. self.SetSizer(btnSizer)
  65. def onUpdate(self, event):
  66. '''docstring for onUpdate'''
  67. self.controller.updateHtml()
  68. pass
  69. def onEnable(self, event):
  70. '''docstring for onEnable'''
  71. self.controller.enableTimer()
  72. def onDisable(self, event):
  73. '''docstring for onDisable'''
  74. self.controller.disableTimer()
  75. pass
  76. def onLink(self, event):
  77. '''docstring for onLink'''
  78. wildcard = "reStructuredText File (*.rst)|*.rst|" \
  79. "All files (*.*)|*.*"
  80. dlg = wx.FileDialog(
  81. self, message="Choose file to link",
  82. defaultDir=LISTSDIR,
  83. defaultFile="",
  84. wildcard=wildcard,
  85. style=wx.OPEN | wx.CHANGE_DIR)
  86. if dlg.ShowModal() == wx.ID_OK:
  87. # This returns a Python list of files that were selected.
  88. path = dlg.GetPaths()
  89. self.controller.linkFile(path[0])
  90. dlg.Destroy()
  91. class EditPanel(wx.Panel):
  92. '''Contains the edit control & html display'''
  93. def __init__(self, parent, controller):
  94. super(EditPanel, self).__init__(parent)
  95. self.controller = controller
  96. controller.editPanel = self
  97. #self.editControl = wx.TextCtrl(self, style=wx.TE_MULTILINE |
  98. #wx.TE_PROCESS_TAB)
  99. self.outputControl = html.HtmlWindow(self)
  100. #outputControl = wx.TextCtrl(self, style=wx.TE_MULTILINE)
  101. b = 5
  102. hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
  103. #hsizer1.Add(self.editControl, 3, wx.EXPAND | wx.ALL, b)
  104. hsizer1.Add(self.outputControl, 1, wx.EXPAND | wx.ALL, b)
  105. self.SetSizer(hsizer1)
  106. class Controller(object):
  107. '''A controller to allow comunication between controls'''
  108. def __init__(self, frame):
  109. '''Set up the timer and disable it to start'''
  110. self.editPanel = None
  111. self.inputFile = None
  112. self.timerEnabled = False
  113. self.timer = wx.CallLater(1000, self.updateHtml)
  114. self.timer.Stop()
  115. self.frame = frame
  116. def updateHtml(self):
  117. '''Updates the rendered text in the HTML window
  118. This is done by pulling the input from the TextCtrl and
  119. passing it out (via subprocess) to rst2html, then passing
  120. the output HTML from that call into the window'''
  121. if self.inputFile:
  122. inputfile = open(self.inputFile, 'r')
  123. lines = inputfile.read()
  124. else:
  125. lines=''
  126. args = ' --no-generator --no-datestamp --toc-entry-backlinks'
  127. piper = subprocess.Popen('python rst2html.py' + args,
  128. shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=R2HPATH)
  129. piper.stdin.write(lines)
  130. piper.stdin.close()
  131. self.editPanel.outputControl.SetPage(piper.stdout.read())
  132. if self.timerEnabled:
  133. self.timer.Restart()
  134. def enableTimer(self):
  135. '''Enable the timer and set the state var'''
  136. self.timer.Start()
  137. self.timerEnabled = True
  138. def disableTimer(self):
  139. '''Disable the timer and set the state var to match'''
  140. self.timer.Stop()
  141. self.timerEnabled = False
  142. def fname(self):
  143. '''docstring for fname'''
  144. pass
  145. def linkFile(self, fileToLink):
  146. '''docstring for linkFile'''
  147. self.inputFile = fileToLink
  148. fileName = os.path.split(fileToLink)[1]
  149. self.frame.statusbar.SetLabel('Linked to file: ' + fileName)
  150. pass
  151. if __name__ == '__main__':
  152. '''If main'''
  153. app = wx.App()
  154. TestingFrame().Show()
  155. #import wx.lib.inspection
  156. #inspector = wx.lib.inspection.InspectionTool()
  157. #inspector.Show()
  158. app.MainLoop()