app.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # @Author: Bachir Soussi Chiadmi <bach>
  4. # @Date: 23-05-2017
  5. # @Email: bachir@figureslibres.io
  6. # @Last modified by: bach
  7. # @Last modified time: 21-04-2017
  8. # @License: GPL-V3
  9. import sys, os
  10. from socket import socket
  11. import socketserver
  12. import http.server
  13. import threading
  14. from PyQt5.QtCore import QCoreApplication, QUrl, QFileSystemWatcher, pyqtSlot
  15. from PyQt5.QtGui import QKeySequence
  16. from PyQt5.QtWidgets import QApplication, QShortcut
  17. from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkRequest
  18. from PyQt5.QtWebKitWidgets import QWebPage, QWebView
  19. import sass
  20. class Server():
  21. def __init__(self, parent=None):
  22. # find free port
  23. sock = socket()
  24. sock.bind(('', 0))
  25. self._port = sock.getsockname()[1]
  26. sock.close()
  27. self.httpd = http.server.HTTPServer(('', self.port), http.server.SimpleHTTPRequestHandler)
  28. self.thread = threading.Thread(target=self.httpd.serve_forever)
  29. self.thread.start()
  30. print("serving at port", self._port)
  31. @property
  32. def port(self):
  33. return self._port
  34. class Compiler():
  35. def __init__(self,parent=None):
  36. paths = [
  37. 'assets',
  38. 'assets/scss',
  39. 'assets/scss/styles.scss'
  40. ]
  41. self.fs_watcher = QFileSystemWatcher(paths)
  42. self.fs_watcher.directoryChanged.connect(self.directory_changed)
  43. self.fs_watcher.fileChanged.connect(self.compile_scss)
  44. # self.compile_scss()
  45. def directory_changed(path):
  46. print("Directory changed : %s" % path)
  47. def compile_scss(path = ""):
  48. print("compiling sass : %s" % path)
  49. # src = open( 'assets/scss/main.scss' ).read()
  50. scss = sass.compile_file(b'assets/scss/main.scss')
  51. with open('assets/scss/main.css', 'w') as fp:
  52. fp.write(scss.decode('utf8'))
  53. class WebkitView(QWebView):
  54. def __init__(self, port):
  55. self.port = port
  56. # QT webkit
  57. self.view = QWebView.__init__(self)
  58. # browser.settings().setAttribute(QWebSettings.PluginsEnabled, True)
  59. self.load(QUrl('http://localhost:'+str(self.port)))
  60. # self.show()
  61. self.shortcut = QShortcut(QKeySequence("Ctrl+Q"), self)
  62. self.shortcut.activated.connect(self.on_quit)
  63. self.show()
  64. @pyqtSlot()
  65. def on_quit(self):
  66. print("Quit!")
  67. QCoreApplication.instance().quit()
  68. # class MainWindow():
  69. # def __init__(self, )
  70. def main():
  71. # app = QtCore.QCoreApplication(sys.argv)
  72. app = QApplication(sys.argv)
  73. server = Server()
  74. compiler = Compiler()
  75. webkitview = WebkitView(server.port)
  76. sys.exit(app.exec_())
  77. if __name__ == "__main__":
  78. main()