core.py 8.1 KB

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