You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
20 KiB

  1. #!/usr/bin/python
  2. from string import Template
  3. import subprocess
  4. import sys
  5. with open('all_settings.tex', 'r') as f:
  6. template = Template(f.read())
  7. themes = 'Default', 'Rays', 'Basic', 'Simple', 'Envelope', 'Wave', 'Board', 'Autumn', 'Desert'
  8. colors = 'Default', 'Australia', 'Britain', 'Sweden', 'Spain', 'Russia', 'Denmark', 'Germany'
  9. palettes = 'Default', 'BlueGrayOrange', 'GreenGrayViolet', 'PurpleGrayBlue', 'BrownBlueOrange'
  10. backgrounds = 'Default', 'VerticalGradation', 'Rays', 'BottomVerticalGradation', 'Empty'
  11. titles = 'Default', 'Basic', 'Empty', 'Filled', 'Envelope', 'Wave', 'VerticalShading'
  12. blocks = 'Default', 'Basic', 'Minimal', 'Envelope', 'Corner', 'Slide', 'TornOut'
  13. notes = 'Default', 'VerticalShading', 'Corner', 'Sticky'
  14. ##################################################################################
  15. ## This function creates a latex document from the template in
  16. ## "all_settings.tex" by substituting the values for the theme, and
  17. ## for the color, background, title, block and note commands, and
  18. ## adding a specific message as the title and a sub-message as the
  19. ## author of the document. Then it compiles this file (called
  20. ## "filename.tex"), keeps only the pdf file, and remembers its name
  21. ## for future.
  22. ##
  23. ## We also need latex commands because the resulting pdf contains the
  24. ## code of the document. So we need to modify certain characters for
  25. ## latex.
  26. ##
  27. def createTexAndCompile( message, submessage, theme, colorcommand,
  28. backgroundcommand, titlecommand,
  29. blockcommand, notecommand, themelatexcommand,
  30. colorlatexcommand, backgroundlatexcommand,
  31. titlelatexcommand, blocklatexcommand,
  32. notelatexcommand, filename, filelist ):
  33. page = template.substitute(
  34. {
  35. 'messagevar': message,
  36. 'submessagevar': submessage,
  37. 'themevar': theme,
  38. 'colorvar': colorcommand,
  39. 'backgroundvar': backgroundcommand,
  40. 'titlevar': titlecommand,
  41. 'blockvar': blockcommand,
  42. 'notevar': notecommand,
  43. 'themestylevar': themelatexcommand,
  44. 'colorstylevar': colorlatexcommand,
  45. 'backgroundstylevar': backgroundlatexcommand,
  46. 'titlestylevar': titlelatexcommand,
  47. 'blockstylevar': blocklatexcommand,
  48. 'notestylevar': notelatexcommand,
  49. }
  50. )
  51. ## create a tex file
  52. with open(filename + '.tex', 'w') as f:
  53. f.write(page)
  54. ## compile the tex file
  55. subprocess.call('pdflatex {}.tex'.format(filename).split())
  56. ## clean up auxiliary files (non-pdf)
  57. subprocess.call('rm -f {}.tex'.format(filename).split())
  58. subprocess.call('rm -f {}.aux'.format(filename).split())
  59. subprocess.call('rm -f {}.log'.format(filename).split())
  60. ## remember the name of the file for future
  61. filelist.append(filename)
  62. return
  63. ##################################################################################
  64. ##################################################################################
  65. ## This function is for testing styles using the Default theme, or for
  66. ## testing each theme without modifications.
  67. ##
  68. ## This function creates latex commands for each style, depending on
  69. ## the values of theme, color, palette, background, title, block and
  70. ## note. Then calls function createTexAndCompile that uses these
  71. ## commands to create and compile the actual document.
  72. ##
  73. ## The main difference of this function with
  74. ## createTexAndCompileAllOptions is follows. The generated pdf
  75. ## contains the code used to generate it, so that users could easily
  76. ## see the settings. In this function the code is kept minimal, so the
  77. ## commands of the form \usesomestyle{Default} are not
  78. ## displayed. Therefore, if one uses a non-Default theme and then, for
  79. ## instance, default block style, it would have effect in the
  80. ## generated pdf, but the code in the pdf would not be correct.
  81. ##
  82. ## Thus, in principle this function can be used for arbitrary
  83. ## combinations of options, but the correctness of the code in the pdf
  84. ## file is guaranteed only for testing styles using the Default theme,
  85. ## or for testing each theme without modifications.
  86. ##
  87. def createTexAndCompileOneOption( message, submessage, theme, color, palette,
  88. background, title, block, note, filelist ):
  89. themelatexcommand = ''
  90. colorcommand = colorlatexcommand = ''
  91. backgroundcommand = backgroundlatexcommand = ''
  92. titlecommand = titlelatexcommand = ''
  93. blockcommand = blocklatexcommand = ''
  94. notecommand = notelatexcommand = ''
  95. ## only if theme is the default one, we also specify other styles
  96. if theme != 'Default' and theme != '':
  97. themelatexcommand = r'\bs usetheme\{' + theme + r'\}\\'
  98. if color != '':
  99. if palette != '':
  100. colorcommand = r'\usecolorstyle[colorPalette=' + palette + ']{' + color + '}'
  101. colorlatexcommand = r'\bs usecolorstyle[colorPalette=' + palette + ']\{' + color + r'\}\\'
  102. else:
  103. colorcommand = r'\usecolorstyle{' + color + '}'
  104. colorlatexcommand = r'\bs usecolorstyle\{' + color + r'\}\\'
  105. if colorcommand == r'\usecolorstyle{Default}' or colorcommand == r'\usecolorstyle[colorPalette=Default]{Default}':
  106. colorcommand = colorlatexcommand = ''
  107. elif palette != '' and palette != 'Default':
  108. colorcommand = r'\usecolorstyle[colorPalette=' + palette + ']{Default}'
  109. colorlatexcommand = r'\bs usecolorstyle[colorPalette=' + palette + r']\{Default\}\\'
  110. if background != 'Default' and background != '':
  111. backgroundcommand = r'\usebackgroundstyle{' + background + '}'
  112. backgroundlatexcommand = r'\bs usebackgroundstyle\{' + background + r'\}\\'
  113. if title != 'Default' and title != '':
  114. titlecommand = r'\usetitlestyle{' + title + '}'
  115. titlelatexcommand = r'\bs usetitlestyle\{' + title + r'\}\\'
  116. if block != 'Default' and block != '':
  117. blockcommand = r'\useblockstyle{' + block + '}'
  118. blocklatexcommand = r'\bs useblockstyle\{' + block + r'\}\\'
  119. if note != 'Default' and note != '':
  120. notecommand = r'\usenotestyle{' + note + '}'
  121. notelatexcommand = r'\bs usenotestyle\{' + note + r'\}\\'
  122. filename = 'ff_' + theme + color + palette + background + title + block + note
  123. createTexAndCompile( message, submessage, theme, colorcommand,
  124. backgroundcommand, titlecommand,
  125. blockcommand, notecommand, themelatexcommand,
  126. colorlatexcommand, backgroundlatexcommand,
  127. titlelatexcommand, blocklatexcommand,
  128. notelatexcommand, filename, filelist )
  129. return
  130. ##################################################################################
  131. ##################################################################################
  132. ## This function is for testing all combinations of styles.
  133. ##
  134. ## This function creates latex commands for each style, depending on
  135. ## the values of color, palette, background, title, block and note.
  136. ## Then calls function createTexAndCompile that uses these commands to
  137. ## create and compile the actual document.
  138. ##
  139. def createTexAndCompileAllOptions( message, submessage, theme, color,
  140. palette, background, title, block, note,
  141. filelist ):
  142. themelatexcommand = ''
  143. colorcommand = colorlatexcommand = ''
  144. backgroundcommand = backgroundlatexcommand = ''
  145. titlecommand = titlelatexcommand = ''
  146. blockcommand = blocklatexcommand = ''
  147. notecommand = notelatexcommand = ''
  148. if theme != '':
  149. themelatexcommand = r'\bs usetheme\{' + theme + r'\}\\'
  150. else:
  151. themelatexcommand = r'\bs usetheme\{Default\}\\'
  152. if color != '':
  153. if palette != '':
  154. colorcommand = r'\usecolorstyle[colorPalette=' + palette + ']{' + color + '}'
  155. colorlatexcommand = r'\bs usecolorstyle[colorPalette=' + palette + ']\{' + color + r'\}\\'
  156. else:
  157. colorcommand = r'\usecolorstyle{' + color + '}'
  158. colorlatexcommand = r'\bs usecolorstyle\{' + color + r'\}\\'
  159. elif palette != '':
  160. colorcommand = r'\usecolorstyle[colorPalette=' + palette + ']{Default}'
  161. colorlatexcommand = r'\bs usecolorstyle[colorPalette=' + palette + r']\{Default\}\\'
  162. if background != '':
  163. backgroundcommand = r'\usebackgroundstyle{' + background + '}'
  164. backgroundlatexcommand = r'\bs usebackgroundstyle\{' + background + r'\}\\'
  165. if title != '':
  166. titlecommand = r'\usetitlestyle{' + title + '}'
  167. titlelatexcommand = r'\bs usetitlestyle\{' + title + r'\}\\'
  168. if block != '':
  169. blockcommand = r'\useblockstyle{' + block + '}'
  170. blocklatexcommand = r'\bs useblockstyle\{' + block + r'\}\\'
  171. if note != '':
  172. notecommand = r'\usenotestyle{' + note + '}'
  173. notelatexcommand = r'\bs usenotestyle\{' + note + r'\}\\'
  174. filename = 'ff_' + theme + color + palette + background + title + block + note
  175. createTexAndCompile( message, submessage, theme, colorcommand,
  176. backgroundcommand, titlecommand,
  177. blockcommand, notecommand, themelatexcommand,
  178. colorlatexcommand, backgroundlatexcommand,
  179. titlelatexcommand, blocklatexcommand,
  180. notelatexcommand, filename, filelist )
  181. return
  182. ##################################################################################
  183. ##################################################################################
  184. ## A function to combine all pdfs whose names are given in the list filelist,
  185. ## in a file called outputFile
  186. ##
  187. def combinePdfs( outputFile, filelist ):
  188. ## merging all pdfs in filelist
  189. command = 'gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=' + outputFile + '.pdf'
  190. subprocess.call(
  191. command.split() + [t + '.pdf' for t in filelist]
  192. )
  193. return
  194. ##################################################################################
  195. ##################################################################################
  196. ## A function to remove pdf files whose names are given in the list filelist
  197. ##
  198. def removePdfs( filelist ):
  199. ## removing the pdfs
  200. for filename in filelist:
  201. subprocess.call('rm -f {}.pdf'.format(filename).split())
  202. return
  203. ##################################################################################
  204. ##################################################################################
  205. ## A function to combine all pdfs whose names are given in the list filelist,
  206. ## in a file called outputFile, then remove these files
  207. ##
  208. def combineAndRemovePdfs( outputFile, filelist ):
  209. combinePdfs( outputFile, filelist )
  210. removePdfs( filelist )
  211. return
  212. ##################################################################################
  213. ##################################################################################
  214. ## A function to try all options: color palettes, color styles, backgrounds,
  215. ## titles, blocks and notes separately
  216. def testAllOptionsSeparately( ):
  217. filenames = []
  218. ##########------------------------------##########
  219. ##########------------------------------##########
  220. ########## varying themes
  221. color = palette = background = title = block = note = ''
  222. submessage = 'All options are defined by the theme'
  223. outputFile = 'all_themes'
  224. for theme in themes:
  225. message = 'Using ' + theme + ' theme'
  226. createTexAndCompileOneOption(message, submessage, theme, color, palette,
  227. background, title, block, note, filenames)
  228. combineAndRemovePdfs(outputFile, filenames)
  229. ##########------------------------------##########
  230. ########## varying color styles
  231. del filenames[:]
  232. theme = background = title = block = note = 'Default'
  233. palette = ''
  234. submessage = 'with the rest being Default'
  235. outputFile = 'all_colors'
  236. for color in colors:
  237. message = 'Using ' + color + ' color style'
  238. createTexAndCompileOneOption(message, submessage, theme, color, palette,
  239. background, title, block, note, filenames)
  240. combineAndRemovePdfs(outputFile, filenames)
  241. ########## varying color palettes
  242. del filenames[:]
  243. theme = color = background = title = block = note = 'Default'
  244. submessage = 'with the Default color style'
  245. outputFile = 'all_palettes'
  246. for palette in palettes:
  247. message = 'Using ' + palette + ' color palette'
  248. createTexAndCompileOneOption(message, submessage, theme, color, palette,
  249. background, title, block, note, filenames)
  250. combineAndRemovePdfs(outputFile, filenames)
  251. ##########------------------------------##########
  252. ########## varying backgrounds
  253. del filenames[:]
  254. theme = color = palette = title = block = note = 'Default'
  255. submessage = 'with the rest being Default'
  256. outputFile = 'all_backgrounds'
  257. for background in backgrounds:
  258. message = 'Using ' + background + ' background style'
  259. createTexAndCompileOneOption(message, submessage, theme, color, palette,
  260. background, title, block, note, filenames)
  261. combineAndRemovePdfs(outputFile, filenames)
  262. ##########------------------------------##########
  263. ########## varying titles
  264. del filenames[:]
  265. theme = color = palette = background = block = note = 'Default'
  266. submessage = 'with the rest being Default'
  267. outputFile = 'all_titles'
  268. for title in titles:
  269. message = 'Using ' + title + ' title style'
  270. createTexAndCompileOneOption(message, submessage, theme, color, palette,
  271. background, title, block, note, filenames)
  272. combineAndRemovePdfs(outputFile, filenames)
  273. ##########------------------------------##########
  274. ########## varying blocks
  275. del filenames[:]
  276. theme = color = palette = background = title = note = 'Default'
  277. submessage = 'with the rest being Default'
  278. outputFile = 'all_blocks'
  279. for block in blocks:
  280. message = 'Using ' + block + ' block style'
  281. createTexAndCompileOneOption(message, submessage, theme, color, palette,
  282. background, title, block, note, filenames)
  283. combineAndRemovePdfs(outputFile, filenames)
  284. ##########------------------------------##########
  285. ########## varying notes
  286. del filenames[:]
  287. theme = color = palette = background = title = block = 'Default'
  288. submessage = 'with the rest being Default'
  289. outputFile = 'all_notes'
  290. for note in notes:
  291. message = 'Using ' + note + ' note style'
  292. createTexAndCompileOneOption(message, submessage, theme, color, palette,
  293. background, title, block, note, filenames)
  294. combineAndRemovePdfs(outputFile, filenames)
  295. del filenames[:]
  296. ###############--------------------###############
  297. ############### combine the 7 pdfs into one called all_settings
  298. outputs = 'all_themes', 'all_colors', 'all_palettes', 'all_backgrounds', 'all_titles', 'all_blocks', 'all_notes'
  299. combinePdfs('all_settings', outputs)
  300. return
  301. ##################################################################################
  302. ##################################################################################
  303. ## A function to try all combinations of options: themes, palettes,
  304. ## color, background, title, block and note styles
  305. ##
  306. def testAllOptionsCombined( ):
  307. filenames = []
  308. outputFile = 'all_combinations'
  309. for theme in themes:
  310. for color in colors:
  311. for palette in palettes:
  312. for background in backgrounds:
  313. for title in titles:
  314. for block in blocks:
  315. for note in notes:
  316. message = 'Using ' + theme + ', ' + palette
  317. submessage = color + background + title + block + note
  318. createTexAndCompileAllOptions(message,
  319. submessage,
  320. theme, color,
  321. palette,
  322. background,
  323. title, block,
  324. note,
  325. filenames)
  326. combineAndRemovePdfs(outputFile, filenames)
  327. del filenames[:]
  328. return
  329. ##################################################################################
  330. ##################################################################################
  331. ## A function to test all predefined themes
  332. def testThemes( ):
  333. filenames = []
  334. color = palette = background = title = block = note = ''
  335. submessage = 'All options are defined by the theme'
  336. outputFile = 'all_themes'
  337. for theme in themes:
  338. message = 'Using ' + theme + ' theme'
  339. createTexAndCompileOneOption(message, submessage, theme, color, palette,
  340. background, title, block, note, filenames)
  341. combineAndRemovePdfs(outputFile, filenames)
  342. del filenames[:]
  343. return
  344. ##################################################################################
  345. ##################################################################################
  346. def usageMessage():
  347. print 'usage: '
  348. print ' all_settings.py'
  349. print ' produces all_settings.pdf by '
  350. print ' testing all themes and all options separately, and '
  351. print ' combining them into one file.'
  352. print r' all_settings.py all'
  353. print r' tests all combinations of all options. Be careful! 352800 combinations'
  354. return
  355. ##################################################################################
  356. ##################################################################################
  357. ############################## Main Body #########################################
  358. ##################################################################################
  359. def main():
  360. if len(sys.argv) == 1:
  361. ## tests all options separately, and generates files all_settings,
  362. ## all_themes, all_colors, all_palettes, all_backgrounds,
  363. ## all_titles, all_blocks, and all_notes
  364. testAllOptionsSeparately()
  365. elif len(sys.argv) == 2:
  366. if sys.argv[1] == 'all':
  367. testAllOptionsCombined()
  368. else:
  369. ## in future we can allow more parameters, for instance compiling
  370. ## all options for a particular theme
  371. usageMessage()
  372. sys.exit(2)
  373. else:
  374. usageMessage()
  375. sys.exit(2)
  376. if __name__ == "__main__":
  377. main()