Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- import subprocess
- import signal
- from PySide.QtCore import *
- from PySide.QtGui import *
- from PySide.QtWebKit import *
- DJANGO_PROJECT_PATH = 'PATH/TO/YOUR/DJANGO/APP' # Change this!
- DJANGO_OUTPUT_FILE = 'output.txt'
- class DjangoServer(QObject):
- """Start, stop and check the output of a django server."""
- signalGotOutput = Signal(str)
- def __init__(self, parent=None):
- super(DjangoServer, self).__init__(parent)
- self.process = None
- def start(self):
- '''Start the django server, write its output into a file.'''
- script_path = os.path.join(DJANGO_PROJECT_PATH, 'manage.py')
- cmd = ['python', script_path, 'runserver']
- with open(DJANGO_OUTPUT_FILE, "wb") as err:
- self.process = subprocess.Popen(cmd, stderr=err,
- cwd=DJANGO_PROJECT_PATH)
- def stop(self):
- '''
- Try to stop the server.
- Note: This does not necessarily kill the correct process, the pid to
- kill might be different in some cases, in this case use 'grep' magic
- to determine the correct pid.
- '''
- os.kill(self.process.pid + 1, signal.SIGKILL)
- def check_output(self):
- '''
- Scan for output of the django server, emit signalGotOutput in case of
- the files content changed.
- '''
- output = ''
- while True:
- QApplication.processEvents()
- with open(DJANGO_OUTPUT_FILE, "r") as err:
- text = err.read()
- if text != output:
- output = text
- self.signalGotOutput.emit(output)
- QThread.msleep(100)
- class ViewBrowser(QWebView):
- """View to show a webpage."""
- signalClosed = Signal()
- def closeEvent(self, e):
- '''The user closed this window, emit the signalClosed.'''
- self.signalClosed.emit()
- e.accept()
- class ViewTerminal(QTextEdit):
- """Display to show the output of a process."""
- signalClosed = Signal()
- def closeEvent(self, e):
- '''The user closed this window, emit the signalClosed.'''
- self.signalClosed.emit()
- e.accept()
- class Controller(QObject):
- """Coordinates the view and the model."""
- def __init__(self):
- '''Setup the UI and connect signals with slots.'''
- super(Controller, self).__init__()
- self.view_browser = ViewBrowser()
- self.view_browser.setGeometry(20, 20, 500, 500)
- self.view_browser.signalClosed.connect(self.view_closed)
- self.view_terminal = ViewTerminal()
- self.view_terminal.setGeometry(20 + self.view_browser.width() + 50, 20,
- 500, 300)
- self.view_terminal.signalClosed.connect(self.view_closed)
- self.server = DjangoServer()
- self.server.signalGotOutput.connect(self.view_terminal.setPlainText)
- self.closed_views = 0
- def start(self):
- '''Start the django server and show both views.'''
- self.server.start()
- self.view_browser.show()
- self.view_terminal.show()
- timer = QTimer(self)
- timer.timeout.connect(self.server.check_output)
- timer.start(100)
- callb = lambda: self.view_browser.load(QUrl("http://127.0.0.1:8000/"))
- QTimer.singleShot(500, callb)
- def view_closed(self):
- '''
- The user closed one of the views.
- Stop the server if the last view is closed.
- '''
- self.closed_views += 1
- if self.closed_views == 2:
- self.server.stop()
- sys.exit(0)
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- controller = Controller()
- controller.start()
- sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment