sasscompiler.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. from PyQt5.QtCore import QFileSystemWatcher
  5. import sass
  6. class Compiler():
  7. def __init__(self,parent):
  8. self.parent = parent
  9. self.initWatching()
  10. self.compile_scss()
  11. # def directory_changed(path):
  12. # print("Directory changed : %s" % path)
  13. def initWatching(self):
  14. self.refreshPaths()
  15. self.fs_watcher = QFileSystemWatcher(self.paths)
  16. # self.fs_watcher.directoryChanged.connect(self.directory_changed)
  17. self.fs_watcher.fileChanged.connect(self.compile_scss)
  18. def compile_scss(self):
  19. print("compiling sass")
  20. try:
  21. scss = sass.compile_file(str.encode(os.path.join(self.parent.cwd,'assets/css/main.scss')))
  22. with open(os.path.join(self.parent.cwd,'assets/css/main.css'), 'w') as fp:
  23. fp.write(scss.decode('utf8'))
  24. except Exception as e:
  25. print("Error compiling Sass", e)
  26. pass
  27. def refreshPaths(self):
  28. self.paths = [
  29. os.path.join(self.parent.cwd,'assets'),
  30. os.path.join(self.parent.cwd,'assets/css')
  31. ]
  32. # os.path.join(self.parent.cwd,'assets/css/styles.scss')
  33. for f in os.listdir(os.path.join(self.parent.cwd,'assets/css')):
  34. if f.endswith("scss"):
  35. self.paths.append(os.path.join(self.parent.cwd,'assets/css',f))
  36. def reload(self):
  37. self.fs_watcher.removePaths(self.paths)
  38. self.refreshPaths()
  39. self.fs_watcher.addPaths(self.paths)