/rstlink.py
Python | 210 lines | 209 code | 1 blank | 0 comment | 0 complexity | 2c47d4bb69d5b12bf44bb289476a6a32 MD5 | raw file
- import os
- import wx
- import wx.html as html
- import subprocess
-
- R2HPATH = 'C:\\work\\process\\docutils\\tools\\'
- LISTSDIR = 'C:\\work\\process\\lists'
- INPUTPATH = 'C:\\work\\process\\lists\\macapps.rst'
-
- class TestingFrame(wx.Frame):
- '''A simple frame to wrap around the edit panel'''
- def __init__(self, parent=None):
- super(TestingFrame, self).__init__(parent=None)
-
- self.controller = Controller(self)
- self.panel = RealTimePanel(self, self.controller)
-
- self.statusbar = wx.StatusBar(self, wx.ID_ANY)
- self.SetStatusBar(self.statusbar)
-
- self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
-
- self.SetSize((500, 800))
- self.SetPosition((100,50))
- self.statusbar.SetLabel('OMGZFTWBBQ!')
-
- def OnCloseWindow(self, event):
- '''Clean up that memory'''
- self.Destroy()
-
-
- class RealTimePanel(wx.Panel):
- '''A panel to collect the edit & display controls'''
- def __init__(self, parent, controller):
- super(RealTimePanel, self).__init__(parent)
-
- self.controller = controller
- btnPanel = BtnPanel(self, controller)
- editPanel = EditPanel(self, controller)
-
- b = 5
- hsizer1 = wx.BoxSizer(wx.VERTICAL)
- hsizer1.Add(btnPanel, 0, wx.EXPAND | wx.ALL, b)
- hsizer1.Add(editPanel, 1, wx.EXPAND | wx.ALL, b)
-
- self.SetSizer(hsizer1)
-
- def updateHtml(self):
- '''Attempts to compile the text into HTMl
-
- The current text in the edit pane is passed out the rst2html.py
- script, and if it returns valid HTML, that is rendered into the
- display window.'''
- pass
-
-
- class BtnPanel(wx.Panel):
- '''Simple horizontal list of the buttons'''
- ID_LOAD = 1001
- ID_SAVE = 1002
-
- def __init__(self, parent, controller):
- super(BtnPanel, self).__init__(parent)
-
- self.controller = controller
-
- btnLink = wx.Button(self, 1001, 'Link')
- btnConvert = wx.Button(self, 1003, 'Convert')
- btnEnable = wx.Button(self, 1003, 'Enable')
- btnDisable = wx.Button(self, 1003, 'Disable')
-
- btnSizer = wx.BoxSizer(wx.HORIZONTAL)
-
- btnSizer.Add(btnLink, 0)
- btnSizer.AddSpacer((5,-1))
- btnSizer.Add(btnConvert, 0)
- btnSizer.AddSpacer((5,-1))
- btnSizer.Add(btnEnable, 0)
- btnSizer.AddSpacer((5,-1))
- btnSizer.Add(btnDisable, 0)
-
- btnLink.Bind(wx.EVT_BUTTON, self.onLink)
- btnConvert.Bind(wx.EVT_BUTTON, self.onUpdate)
- btnEnable.Bind(wx.EVT_BUTTON, self.onEnable)
- btnDisable.Bind(wx.EVT_BUTTON, self.onDisable)
-
- self.SetSizer(btnSizer)
-
- def onUpdate(self, event):
- '''docstring for onUpdate'''
- self.controller.updateHtml()
- pass
-
- def onEnable(self, event):
- '''docstring for onEnable'''
- self.controller.enableTimer()
-
- def onDisable(self, event):
- '''docstring for onDisable'''
- self.controller.disableTimer()
- pass
-
- def onLink(self, event):
- '''docstring for onLink'''
- wildcard = "reStructuredText File (*.rst)|*.rst|" \
- "All files (*.*)|*.*"
-
- dlg = wx.FileDialog(
- self, message="Choose file to link",
- defaultDir=LISTSDIR,
- defaultFile="",
- wildcard=wildcard,
- style=wx.OPEN | wx.CHANGE_DIR)
-
- if dlg.ShowModal() == wx.ID_OK:
- # This returns a Python list of files that were selected.
- path = dlg.GetPaths()
- self.controller.linkFile(path[0])
-
- dlg.Destroy()
-
-
- class EditPanel(wx.Panel):
- '''Contains the edit control & html display'''
- def __init__(self, parent, controller):
- super(EditPanel, self).__init__(parent)
-
- self.controller = controller
- controller.editPanel = self
-
- #self.editControl = wx.TextCtrl(self, style=wx.TE_MULTILINE |
- #wx.TE_PROCESS_TAB)
- self.outputControl = html.HtmlWindow(self)
- #outputControl = wx.TextCtrl(self, style=wx.TE_MULTILINE)
-
- b = 5
- hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
- #hsizer1.Add(self.editControl, 3, wx.EXPAND | wx.ALL, b)
- hsizer1.Add(self.outputControl, 1, wx.EXPAND | wx.ALL, b)
- self.SetSizer(hsizer1)
-
-
- class Controller(object):
- '''A controller to allow comunication between controls'''
- def __init__(self, frame):
- '''Set up the timer and disable it to start'''
- self.editPanel = None
- self.inputFile = None
- self.timerEnabled = False
- self.timer = wx.CallLater(1000, self.updateHtml)
- self.timer.Stop()
- self.frame = frame
-
- def updateHtml(self):
- '''Updates the rendered text in the HTML window
-
- This is done by pulling the input from the TextCtrl and
- passing it out (via subprocess) to rst2html, then passing
- the output HTML from that call into the window'''
-
- if self.inputFile:
- inputfile = open(self.inputFile, 'r')
- lines = inputfile.read()
- else:
- lines=''
-
- args = ' --no-generator --no-datestamp --toc-entry-backlinks'
- piper = subprocess.Popen('python rst2html.py' + args,
- shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=R2HPATH)
-
- piper.stdin.write(lines)
-
- piper.stdin.close()
- self.editPanel.outputControl.SetPage(piper.stdout.read())
-
- if self.timerEnabled:
- self.timer.Restart()
-
- def enableTimer(self):
- '''Enable the timer and set the state var'''
- self.timer.Start()
- self.timerEnabled = True
-
- def disableTimer(self):
- '''Disable the timer and set the state var to match'''
- self.timer.Stop()
- self.timerEnabled = False
-
- def fname(self):
- '''docstring for fname'''
- pass
-
- def linkFile(self, fileToLink):
- '''docstring for linkFile'''
- self.inputFile = fileToLink
- fileName = os.path.split(fileToLink)[1]
- self.frame.statusbar.SetLabel('Linked to file: ' + fileName)
-
- pass
-
- if __name__ == '__main__':
- '''If main'''
- app = wx.App()
- TestingFrame().Show()
-
- #import wx.lib.inspection
- #inspector = wx.lib.inspection.InspectionTool()
- #inspector.Show()
-
- app.MainLoop()