sasscompiler.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # @Author: Bachir Soussi Chiadmi <bach>
  4. # @Date: 30-05-2017
  5. # @Email: bachir@figureslibres.io
  6. # @Filename: sasscompiler.py
  7. # @Last modified by: bach
  8. # @Last modified time: 03-06-2017
  9. # @License: GPL-V3
  10. import os
  11. from PyQt5.QtCore import QFileSystemWatcher
  12. import sass
  13. class Compiler():
  14. def __init__(self,parent):
  15. self.parent = parent
  16. self.initWatching()
  17. self.compile_scss()
  18. # def directory_changed(path):
  19. # print("Directory changed : %s" % path)
  20. def initWatching(self):
  21. self.refreshPaths()
  22. self.fs_watcher = QFileSystemWatcher(self.paths)
  23. # self.fs_watcher.directoryChanged.connect(self.directory_changed)
  24. self.fs_watcher.fileChanged.connect(self.compile_scss)
  25. def compile_scss(self):
  26. print("compiling sass")
  27. try:
  28. scss = sass.compile_file(str.encode(os.path.join(self.parent.cwd,'assets/css/main.scss')))
  29. with open(os.path.join(self.parent.cwd,'assets/css/main.css'), 'w') as fp:
  30. fp.write(scss.decode('utf8'))
  31. except Exception as e:
  32. print("Error compiling Sass", e)
  33. pass
  34. def refreshPaths(self):
  35. self.paths = [
  36. os.path.join(self.parent.cwd,'assets'),
  37. os.path.join(self.parent.cwd,'assets/css')
  38. ]
  39. # os.path.join(self.parent.cwd,'assets/css/styles.scss')
  40. for f in os.listdir(os.path.join(self.parent.cwd,'assets/css')):
  41. if f.endswith("scss"):
  42. self.paths.append(os.path.join(self.parent.cwd,'assets/css',f))
  43. def reload(self):
  44. print('Reload sass compiler')
  45. self.fs_watcher.removePaths(self.paths)
  46. self.refreshPaths()
  47. print('paths', self.paths)
  48. self.fs_watcher.addPaths(self.paths)
  49. print('files', self.fs_watcher.files())
  50. self.compile_scss()