core.py 8.2 KB

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