core.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 os, shutil, tempfile
  10. # sys,
  11. from PyQt5 import QtCore
  12. from PyQt5.QtCore import QSettings, QCoreApplication
  13. import json
  14. import git
  15. from classes import server, sasscompiler, md2html
  16. class Core():
  17. def __init__(self, parent=None):
  18. # restore previous preferences
  19. self.appcwd = os.getcwd()
  20. self.restorePreferences()
  21. self._mw = False
  22. self.temp = tempfile.mkdtemp()
  23. # print(self.temp)
  24. self.tempcwd = False
  25. # if ther's not current project folder from restorepref
  26. # initaite a new temp project
  27. if(self.cwd == None or not os.path.isdir(self.cwd)):
  28. self.cwd = os.path.join(self.temp, 'cwd')
  29. self.tempcwd = True
  30. self.initnewproject()
  31. self.initDeamons()
  32. else:
  33. self.initDeamons()
  34. head, tail = os.path.split(self.cwd)
  35. print('tail', tail)
  36. self.projectname = tail
  37. self.loadDocSettings()
  38. def initDeamons(self):
  39. self.server = server.Server(self)
  40. self.sasscompiler = sasscompiler.Compiler(self)
  41. self.contentcompiler = md2html.Compiler(self)
  42. @property
  43. def mainwindow(self):
  44. return self.mainwindow
  45. @mainwindow.setter
  46. def mainwindow(self, mw):
  47. if not self._mw:
  48. self._mw = mw
  49. if not self.tempcwd:
  50. self._mw.setWindowTitle("Cascade – "+self.cwd)
  51. def restorePreferences(self):
  52. # print("restorePreferences")
  53. settings = QSettings('FiguresLibres', 'Cascade')
  54. # settings.clear()
  55. # print(settings.allKeys())
  56. self.cwd = settings.value('core/cwd', None)
  57. self.dialog_path = settings.value('core/dialog_path', os.path.expanduser('~'))
  58. self.mw_size = settings.value('mainwindow/size', QtCore.QSize(1024, 768))
  59. self.mw_pos = settings.value('mainwindow/pos', QtCore.QPoint(0, 0))
  60. self.mw_curstack = int(settings.value('mainwindow/curstack', 0))
  61. def loadDocSettings(self):
  62. self.docsettings = json.loads(open(os.path.join(self.cwd,'.config/prefs.json')).read())
  63. def recordDocSettings(self,docsettings):
  64. # print("doc settings",docsettings)
  65. for key in docsettings:
  66. self.docsettings[key] = docsettings[key]
  67. jsonfilepath = os.path.join(self.cwd,'.config/prefs.json')
  68. with open(jsonfilepath, "w") as fp:
  69. json.dump(self.docsettings, fp, ensure_ascii=False, indent="\t")
  70. def savePreferences(self):
  71. # print("savePreferences")
  72. settings = QSettings('FiguresLibres', 'Cascade')
  73. # print(settings.allKeys())
  74. if not self.tempcwd:
  75. settings.setValue('core/cwd', self.cwd)
  76. settings.setValue('core/dialog_path', self.dialog_path)
  77. settings.setValue('mainwindow/size', self._mw.size())
  78. settings.setValue('mainwindow/pos', self._mw.pos())
  79. settings.setValue('mainwindow/curstack', self._mw.mainstack.currentIndex())
  80. def initnewproject(self, cwd = None):
  81. print('initnewproject')
  82. if cwd == None :
  83. cwd = self.cwd
  84. shutil.copytree(os.path.join(self.appcwd,'templates/newproject'), cwd)
  85. self.changeCWD(cwd)
  86. self.loadDocSettings()
  87. # self.docsettings = json.loads(open(os.path.join(cwd,'.config/prefs.json')).read())
  88. self.summary = json.loads(open(os.path.join(cwd,'.config/summary.json')).read())
  89. self.repository = git.Repo.init(cwd)
  90. self.repository.index.add(['assets','contents','.config'])
  91. self.repository.index.commit("initial commit")
  92. def saveproject(self, cwd = None):
  93. if not cwd == None:
  94. shutil.copytree(self.cwd, cwd)
  95. self.tempcwd = False
  96. self.changeCWD(cwd)
  97. def openproject(self, cwd=None):
  98. if not cwd == None:
  99. self.changeCWD(cwd)
  100. def changeCWD(self, cwd):
  101. if not cwd == self.cwd:
  102. self.cwd = cwd
  103. self.server.reload()
  104. self.sasscompiler.reload()
  105. self.contentcompiler.reload()
  106. if not self.tempcwd:
  107. self._mw.setWindowTitle("Cascade – "+self.cwd)
  108. head, tail = os.path.split(self.cwd)
  109. print('tail', projectname)
  110. self.projectname = tail
  111. self._mw.designstack.refresh()
  112. self._mw.contentstack.refresh()
  113. def quit(self):
  114. self.savePreferences()
  115. shutil.rmtree(self.temp, ignore_errors=True)
  116. QCoreApplication.instance().quit()