core.py 8.1 KB

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