refacored with class
server works compiler works browser works
This commit is contained in:
@@ -16,61 +16,86 @@ import socketserver
|
|||||||
import http.server
|
import http.server
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from PyQt5.QtCore import QFile, QIODevice, Qt, QTextStream, QUrl, QFileSystemWatcher
|
from PyQt5.QtCore import QUrl, QFileSystemWatcher
|
||||||
from PyQt5.QtWidgets import (QAction, QApplication, QLineEdit, QMainWindow, QSizePolicy, QStyle, QTextEdit)
|
from PyQt5.QtWidgets import (QAction, QApplication, QLineEdit, QMainWindow, QSizePolicy, QStyle, QTextEdit)
|
||||||
from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkRequest
|
from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkRequest
|
||||||
from PyQt5.QtWebKitWidgets import QWebPage, QWebView
|
from PyQt5.QtWebKitWidgets import QWebPage, QWebView
|
||||||
|
|
||||||
import sass
|
import sass
|
||||||
|
|
||||||
_PORT=0
|
|
||||||
|
class Server():
|
||||||
|
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
|
||||||
|
# find free port
|
||||||
|
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)
|
||||||
|
# self.compile_scss()
|
||||||
|
|
||||||
|
|
||||||
|
def directory_changed(path):
|
||||||
|
print("Directory changed : %s" % path)
|
||||||
|
|
||||||
|
def compile_scss(path = ""):
|
||||||
|
print("compiling sass : %s" % path)
|
||||||
|
# src = open( 'assets/scss/main.scss' ).read()
|
||||||
|
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
|
||||||
|
# QT webkit
|
||||||
|
self.view = QWebView.__init__(self)
|
||||||
|
# browser.settings().setAttribute(QWebSettings.PluginsEnabled, True)
|
||||||
|
|
||||||
|
self.load(QUrl('http://localhost:'+str(self.port)))
|
||||||
|
# self.show()
|
||||||
|
|
||||||
|
|
||||||
|
# class MainWindow():
|
||||||
|
# def __init__(self, )
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
server()
|
# app = QtCore.QCoreApplication(sys.argv)
|
||||||
compiler()
|
|
||||||
app()
|
|
||||||
|
|
||||||
def server():
|
|
||||||
# find free port
|
|
||||||
sock = socket()
|
|
||||||
sock.bind(('', 0))
|
|
||||||
global _PORT
|
|
||||||
_PORT = sock.getsockname()[1]
|
|
||||||
sock.close()
|
|
||||||
# PORT = 8000
|
|
||||||
# web werver
|
|
||||||
# Handler = http.server.SimpleHTTPRequestHandler
|
|
||||||
# httpd = socketserver.TCPServer(("", PORT), Handler)
|
|
||||||
# httpd.serve_forever()
|
|
||||||
httpd = http.server.HTTPServer(('', _PORT), http.server.SimpleHTTPRequestHandler)
|
|
||||||
thread = threading.Thread(target=httpd.serve_forever)
|
|
||||||
thread.start()
|
|
||||||
print("serving at port", _PORT)
|
|
||||||
|
|
||||||
def compiler():
|
|
||||||
fs_watcher = QFileSystemWatcher(['assets/scss'])
|
|
||||||
fs_watcher.directoryChanged.connect(compile_scss)
|
|
||||||
compile_scss()
|
|
||||||
|
|
||||||
def compile_scss():
|
|
||||||
# src = open( 'assets/scss/main.scss' ).read()
|
|
||||||
scss = sass.compile_file(b'assets/scss/main.scss')
|
|
||||||
with open('assets/scss/main.css', 'w') as fp:
|
|
||||||
fp.write(scss.decode('utf8'))
|
|
||||||
|
|
||||||
|
|
||||||
def app():
|
|
||||||
|
|
||||||
# QT webkit
|
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
|
|
||||||
browser = QWebView()
|
server = Server()
|
||||||
# browser.settings().setAttribute(QWebSettings.PluginsEnabled, True)
|
compiler = Compiler()
|
||||||
global _PORT
|
webkitview = WebkitView(server.port)
|
||||||
browser.load(QUrl('http://localhost:'+str(_PORT)))
|
webkitview.show()
|
||||||
browser.show()
|
|
||||||
|
|
||||||
sys.exit(app.exec_())
|
|
||||||
|
# sys.exit(app.exec_())
|
||||||
|
app.exec_()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user