sasscompiler.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. from __future__ import absolute_import, print_function, division, unicode_literals
  11. import os
  12. from PyQt5.QtCore import QFileSystemWatcher
  13. import sass
  14. class Compiler():
  15. def __init__(self,parent):
  16. self.parent = parent
  17. self.initWatching()
  18. self.compile_scss()
  19. # def directory_changed(path):
  20. # print("Directory changed : %s" % path)
  21. def initWatching(self):
  22. self.refreshPaths()
  23. self.fs_watcher = QFileSystemWatcher(self.paths)
  24. # self.fs_watcher.directoryChanged.connect(self.directory_changed)
  25. self.fs_watcher.fileChanged.connect(self.compile_scss)
  26. def compile_scss(self):
  27. print("compiling sass")
  28. try:
  29. scss = sass.compile_file(str.encode(os.path.join(self.parent.cwd,'assets/css/main.scss')))
  30. with open(os.path.join(self.parent.cwd,'assets/css/main.css'), 'w') as fp:
  31. fp.write(scss.decode('utf8'))
  32. except Exception as e:
  33. print("Error compiling Sass", e)
  34. pass
  35. def refreshPaths(self):
  36. self.paths = [
  37. os.path.join(self.parent.cwd,'assets'),
  38. os.path.join(self.parent.cwd,'assets/css')
  39. ]
  40. # os.path.join(self.parent.cwd,'assets/css/styles.scss')
  41. for f in os.listdir(os.path.join(self.parent.cwd,'assets/css')):
  42. if f.endswith("scss"):
  43. self.paths.append(os.path.join(self.parent.cwd,'assets/css',f))
  44. def reload(self):
  45. print('Reload sass compiler')
  46. self.fs_watcher.removePaths(self.paths)
  47. self.refreshPaths()
  48. print('paths', self.paths)
  49. self.fs_watcher.addPaths(self.paths)
  50. print('files', self.fs_watcher.files())
  51. self.compile_scss()