123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import sys, os
- from socket import socket
- import socketserver
- import http.server
- import threading
- from PyQt5.QtCore import QUrl, QFileSystemWatcher
- from PyQt5.QtWidgets import (QAction, QApplication, QLineEdit, QMainWindow, QSizePolicy, QStyle, QTextEdit)
- from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkRequest
- from PyQt5.QtWebKitWidgets import QWebPage, QWebView
- import sass
- class Server():
- def __init__(self, parent=None):
-
- sock = socket()
- sock.bind(('', 0))
- self._port = sock.getsockname()[1]
- sock.close()
- self.httpd = http.server.HTTPServer(('', self.port), http.server.SimpleHTTPRequestHandler)
- self.thread = threading.Thread(target=self.httpd.serve_forever)
- self.thread.start()
- print("serving at port", self._port)
- @property
- def port(self):
- return self._port
- class Compiler():
- def __init__(self,parent=None):
- paths = [
- 'assets',
- 'assets/scss',
- 'assets/scss/styles.scss'
- ]
- self.fs_watcher = QFileSystemWatcher(paths)
- self.fs_watcher.directoryChanged.connect(self.directory_changed)
- self.fs_watcher.fileChanged.connect(self.compile_scss)
-
- def directory_changed(path):
- print("Directory changed : %s" % path)
- def compile_scss(path = ""):
- print("compiling sass : %s" % path)
-
- scss = sass.compile_file(b'assets/scss/main.scss')
- with open('assets/scss/main.css', 'w') as fp:
- fp.write(scss.decode('utf8'))
- class WebkitView(QWebView):
- def __init__(self, port):
- self.port = port
-
- self.view = QWebView.__init__(self)
-
- self.load(QUrl('http://localhost:'+str(self.port)))
-
- def main():
-
- app = QApplication(sys.argv)
- server = Server()
- compiler = Compiler()
- webkitview = WebkitView(server.port)
- webkitview.show()
-
- app.exec_()
- if __name__ == "__main__":
- main()
|