Guest User

PySide-Django hyprid

a guest
Aug 28th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. import sys
  2. import os
  3. import subprocess
  4. import signal
  5.  
  6. from PySide.QtCore import *
  7. from PySide.QtGui import *
  8. from PySide.QtWebKit import *
  9.  
  10.  
  11. DJANGO_PROJECT_PATH = 'PATH/TO/YOUR/DJANGO/APP'         # Change this!
  12. DJANGO_OUTPUT_FILE = 'output.txt'
  13.  
  14.  
  15. class DjangoServer(QObject):
  16.     """Start, stop and check the output of a django server."""
  17.  
  18.     signalGotOutput = Signal(str)
  19.  
  20.     def __init__(self, parent=None):
  21.         super(DjangoServer, self).__init__(parent)
  22.         self.process = None
  23.  
  24.     def start(self):
  25.         '''Start the django server, write its output into a file.'''
  26.  
  27.         script_path = os.path.join(DJANGO_PROJECT_PATH, 'manage.py')
  28.         cmd = ['python', script_path, 'runserver']
  29.         with open(DJANGO_OUTPUT_FILE, "wb") as err:
  30.             self.process = subprocess.Popen(cmd, stderr=err,
  31.                                             cwd=DJANGO_PROJECT_PATH)
  32.  
  33.     def stop(self):
  34.         '''
  35.        Try to stop the server.
  36.        Note: This does not necessarily kill the correct process, the pid to
  37.        kill might be different in some cases, in this case use 'grep' magic
  38.        to determine the correct pid.
  39.        '''
  40.         os.kill(self.process.pid + 1, signal.SIGKILL)
  41.  
  42.     def check_output(self):
  43.         '''
  44.        Scan for output of the django server, emit signalGotOutput in case of
  45.        the files content changed.
  46.        '''
  47.         output = ''
  48.         while True:
  49.             QApplication.processEvents()
  50.             with open(DJANGO_OUTPUT_FILE, "r") as err:
  51.                 text = err.read()
  52.             if text != output:
  53.                 output = text
  54.                 self.signalGotOutput.emit(output)
  55.             QThread.msleep(100)
  56.  
  57.  
  58. class ViewBrowser(QWebView):
  59.     """View to show a webpage."""
  60.  
  61.     signalClosed = Signal()
  62.  
  63.     def closeEvent(self, e):
  64.         '''The user closed this window, emit the signalClosed.'''
  65.         self.signalClosed.emit()
  66.         e.accept()
  67.  
  68.  
  69. class ViewTerminal(QTextEdit):
  70.     """Display to show the output of a process."""
  71.  
  72.     signalClosed = Signal()
  73.  
  74.     def closeEvent(self, e):
  75.         '''The user closed this window, emit the signalClosed.'''
  76.         self.signalClosed.emit()
  77.         e.accept()
  78.  
  79.  
  80. class Controller(QObject):
  81.     """Coordinates the view and the model."""
  82.  
  83.     def __init__(self):
  84.         '''Setup the UI and connect signals with slots.'''
  85.         super(Controller, self).__init__()
  86.         self.view_browser = ViewBrowser()
  87.         self.view_browser.setGeometry(20, 20, 500, 500)
  88.         self.view_browser.signalClosed.connect(self.view_closed)
  89.         self.view_terminal = ViewTerminal()
  90.         self.view_terminal.setGeometry(20 + self.view_browser.width() + 50, 20,
  91.                                        500, 300)
  92.  
  93.         self.view_terminal.signalClosed.connect(self.view_closed)
  94.         self.server = DjangoServer()
  95.         self.server.signalGotOutput.connect(self.view_terminal.setPlainText)
  96.         self.closed_views = 0
  97.  
  98.     def start(self):
  99.         '''Start the django server and show both views.'''
  100.         self.server.start()
  101.         self.view_browser.show()
  102.         self.view_terminal.show()
  103.  
  104.         timer = QTimer(self)
  105.         timer.timeout.connect(self.server.check_output)
  106.         timer.start(100)
  107.  
  108.         callb = lambda: self.view_browser.load(QUrl("http://127.0.0.1:8000/"))
  109.         QTimer.singleShot(500, callb)
  110.  
  111.     def view_closed(self):
  112.         '''
  113.        The user closed one of the views.
  114.        Stop the server if the last view is closed.
  115.        '''
  116.         self.closed_views += 1
  117.         if self.closed_views == 2:
  118.             self.server.stop()
  119.             sys.exit(0)
  120.  
  121.  
  122. if __name__ == '__main__':
  123.     app = QApplication(sys.argv)
  124.     controller = Controller()
  125.     controller.start()
  126.     sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment