scsscompiler.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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: scsscompiler.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 scss
  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 scss")
  28. try:
  29. import_path = ['./assets/css/']
  30. print("import path",import_path)
  31. comp = scss.Compiler(output_style="compressed", search_path=import_path)
  32. # Compiler().compile_string("a { color: red + green; }")
  33. scss_file = open(os.path.join(self.parent.cwd,'assets/css/main.scss'),"r")
  34. css = comp.compile_string(scss_file.read())
  35. # print("css : %s" % css)
  36. with open(os.path.join(self.parent.cwd,'assets/css/main.css'), 'w') as fp:
  37. fp.write(css)
  38. except Exception as e:
  39. print("Error compiling scss", e)
  40. pass
  41. def refreshPaths(self):
  42. self.paths = [
  43. os.path.join(self.parent.cwd,'assets'),
  44. os.path.join(self.parent.cwd,'assets/css')
  45. ]
  46. # os.path.join(self.parent.cwd,'assets/css/styles.scss')
  47. for f in os.listdir(os.path.join(self.parent.cwd,'assets/css')):
  48. if f.endswith("scss"):
  49. self.paths.append(os.path.join(self.parent.cwd,'assets/css',f))
  50. def reload(self):
  51. print('Reload scss compiler')
  52. self.fs_watcher.removePaths(self.paths)
  53. self.refreshPaths()
  54. print('paths', self.paths)
  55. self.fs_watcher.addPaths(self.paths)
  56. print('files', self.fs_watcher.files())
  57. self.compile_scss()