core.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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: 03-06-2017
  8. # @License: GPL-V3
  9. import os, re, 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. # ______
  17. # / ____/___ ________
  18. # / / / __ \/ ___/ _ \
  19. # / /___/ /_/ / / / __/
  20. # \____/\____/_/ \___/
  21. class Core():
  22. def __init__(self, parent=None):
  23. # restore previous preferences
  24. self.appcwd = os.getcwd()
  25. self.restorePreferences()
  26. self._mw = False
  27. self.temp = tempfile.mkdtemp()
  28. # print(self.temp)
  29. self.tempcwd = False
  30. # if ther's not current project folder from restorepref
  31. # initaite a new temp project
  32. if(self.cwd == None or not os.path.isdir(self.cwd)):
  33. self.cwd = os.path.join(self.temp, 'cwd')
  34. self.tempcwd = True
  35. self.initnewproject()
  36. self.initDeamons()
  37. else:
  38. self.initDeamons()
  39. head, tail = os.path.split(self.cwd)
  40. print('tail', tail)
  41. self.projectname = tail
  42. self.loadDocSettings()
  43. def initDeamons(self):
  44. self.server = server.Server(self)
  45. self.sasscompiler = sasscompiler.Compiler(self)
  46. self.contentcompiler = md2html.Compiler(self)
  47. @property
  48. def mainwindow(self):
  49. return self.mainwindow
  50. @mainwindow.setter
  51. def mainwindow(self, mw):
  52. if not self._mw:
  53. self._mw = mw
  54. if not self.tempcwd:
  55. self._mw.setWindowTitle("Cascade – "+self.cwd)
  56. # ____ ____
  57. # / __ \________ / __/____
  58. # / /_/ / ___/ _ \/ /_/ ___/
  59. # / ____/ / / __/ __(__ )
  60. # /_/ /_/ \___/_/ /____/
  61. def restorePreferences(self):
  62. # print("restorePreferences")
  63. settings = QSettings('FiguresLibres', 'Cascade')
  64. # settings.clear()
  65. # print(settings.allKeys())
  66. self.cwd = settings.value('core/cwd', None)
  67. self.dialog_path = settings.value('core/dialog_path', os.path.expanduser('~'))
  68. self.mw_size = settings.value('mainwindow/size', QtCore.QSize(1024, 768))
  69. self.mw_pos = settings.value('mainwindow/pos', QtCore.QPoint(0, 0))
  70. self.mw_curstack = int(settings.value('mainwindow/curstack', 0))
  71. def savePreferences(self):
  72. # print("savePreferences")
  73. settings = QSettings('FiguresLibres', 'Cascade')
  74. # print(settings.allKeys())
  75. if not self.tempcwd:
  76. settings.setValue('core/cwd', self.cwd)
  77. settings.setValue('core/dialog_path', self.dialog_path)
  78. settings.setValue('mainwindow/size', self._mw.size())
  79. settings.setValue('mainwindow/pos', self._mw.pos())
  80. settings.setValue('mainwindow/curstack', self._mw.mainstack.currentIndex())
  81. # ____ _____ __ __ _
  82. # / __ \____ _____ / ___/___ / /_/ /_(_)___ ____ ______
  83. # / / / / __ \/ ___/ \__ \/ _ \/ __/ __/ / __ \/ __ `/ ___/
  84. # / /_/ / /_/ / /__ ___/ / __/ /_/ /_/ / / / / /_/ (__ )
  85. # /_____/\____/\___/ /____/\___/\__/\__/_/_/ /_/\__, /____/
  86. # /____/
  87. def loadDocSettings(self):
  88. self.docsettings = json.loads(open(os.path.join(self.cwd,'.config/docsettings.json')).read())
  89. def recordDocSettings(self,docsettings):
  90. # print("doc settings",docsettings)
  91. for key in docsettings:
  92. self.docsettings[key] = docsettings[key]
  93. jsonfilepath = os.path.join(self.cwd,'.config/docsettings.json')
  94. with open(jsonfilepath, "w") as fp:
  95. json.dump(self.docsettings, fp, ensure_ascii=False, indent="\t")
  96. self.updateScss(False)
  97. self.updateJs()
  98. def updateScss(self, reload=True):
  99. # print(self.docsettings)
  100. sassfilepath = os.path.join(self.cwd,'assets/css/setup.scss')
  101. # print(sassfilepath)
  102. sass = open(sassfilepath,"r").read()
  103. sets = {
  104. 'pw':'page-width',
  105. 'ph':'page-height',
  106. 'mt':'page-margin-top',
  107. 'mb':'page-margin-bottom',
  108. 'me':'page-margin-outside',
  109. 'mi':'page-margin-inside',
  110. 'cs':'crop-size',
  111. 'bs':'bleed',
  112. 'cg':'col-gutter',
  113. 'rg':'row-gutter',
  114. 'lh':'line-height'
  115. }
  116. for s in sets:
  117. sass = re.sub(
  118. r'\$'+sets[s]+':\smm2pt\([0-9|\.]+\);',
  119. '$'+sets[s]+': mm2pt('+self.docsettings[s]+');',
  120. sass)
  121. # $col-number: 9;
  122. sass = re.sub(
  123. r'\$col-number:\s[0-9|\.]+;',
  124. '$col-number: '+self.docsettings['cn']+';',
  125. sass)
  126. # $row-number: 12;
  127. sass = re.sub(
  128. r'\$row-number:\s[0-9|\.]+;',
  129. '$row-number: '+self.docsettings['rn']+';',
  130. sass)
  131. #
  132. # print('sass', sass)
  133. open(sassfilepath,"w").write(sass)
  134. if reload:
  135. self._mw.designstack.webkitview.reload()
  136. def updateJs(self, reload=True):
  137. # print(self.docsettings)
  138. jsfilepath = os.path.join(self.cwd,'assets/js/setup.js')
  139. # print(jsfilepath)
  140. js = open(jsfilepath,"r").read()
  141. # $row-number: 12;
  142. js = re.sub(
  143. r'nb_page=[0-9]+;',
  144. 'nb_page='+str(self.docsettings['np'])+';',
  145. js)
  146. # print('sass', sass)
  147. open(jsfilepath,"w").write(js)
  148. if reload:
  149. self._mw.designstack.webkitview.reload()
  150. def addPage(self):
  151. self.docsettings['np'] = int(self.docsettings['np'])+1
  152. self.updateJs()
  153. def rmPage(self):
  154. self.docsettings['np'] = int(self.docsettings['np'])-1
  155. self.updateJs()
  156. # ____ _ __
  157. # / __ \_________ (_)__ _____/ /_
  158. # / /_/ / ___/ __ \ / / _ \/ ___/ __/
  159. # / ____/ / / /_/ / / / __/ /__/ /_
  160. # /_/ /_/ \____/_/ /\___/\___/\__/
  161. # /___/
  162. def initnewproject(self, cwd = None):
  163. print('initnewproject')
  164. if cwd == None :
  165. cwd = self.cwd
  166. shutil.copytree(os.path.join(self.appcwd,'templates/newproject'), cwd)
  167. self.changeCWD(cwd)
  168. self.loadDocSettings()
  169. # self.docsettings = json.loads(open(os.path.join(cwd,'.config/docsettings.json')).read())
  170. self.summary = json.loads(open(os.path.join(cwd,'.config/summary.json')).read())
  171. self.repository = git.Repo.init(cwd)
  172. self.repository.index.add(['assets','contents','.config'])
  173. self.repository.index.commit("initial commit")
  174. def saveproject(self, cwd = None):
  175. if not cwd == None:
  176. shutil.copytree(self.cwd, cwd)
  177. self.tempcwd = False
  178. self.changeCWD(cwd)
  179. def openproject(self, cwd=None):
  180. if not cwd == None:
  181. self.changeCWD(cwd)
  182. # __ _______ ______
  183. # _____/ /_ ____ _____ ____ ____ / ____/ | / / __ \
  184. # / ___/ __ \/ __ `/ __ \/ __ `/ _ \/ / | | /| / / / / /
  185. # / /__/ / / / /_/ / / / / /_/ / __/ /___ | |/ |/ / /_/ /
  186. # \___/_/ /_/\__,_/_/ /_/\__, /\___/\____/ |__/|__/_____/
  187. # /____/
  188. def changeCWD(self, cwd):
  189. if not cwd == self.cwd:
  190. self.cwd = cwd
  191. self.server.reload()
  192. self.sasscompiler.reload()
  193. self.contentcompiler.reload()
  194. if not self.tempcwd:
  195. self._mw.setWindowTitle("Cascade – "+self.cwd)
  196. head, tail = os.path.split(self.cwd)
  197. print('tail', projectname)
  198. self.projectname = tail
  199. self._mw.designstack.refresh()
  200. self._mw.contentstack.refresh()
  201. # ____ _ __
  202. # / __ \__ __(_) /_
  203. # / / / / / / / / __/
  204. # / /_/ / /_/ / / /_
  205. # \___\_\__,_/_/\__/
  206. def quit(self):
  207. self.savePreferences()
  208. shutil.rmtree(self.temp, ignore_errors=True)
  209. QCoreApplication.instance().quit()