A bin folder, holding helpfull scripts and commands
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

mkdoc 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import jinja2
  4. import optparse
  5. import os
  6. import shutil
  7. import time
  8. TYPES = [u'brief', u'merkblatt', u'präsentation']
  9. TARGET_EXTENTION = u'.tex'
  10. def mkdir(path):
  11. """.. warning:: Needs to be documented
  12. """
  13. path=os.path.abspath(path)
  14. if not os.path.exists(os.path.dirname(path)):
  15. mkdir(os.path.dirname(path))
  16. if not os.path.exists(path):
  17. os.mkdir(path)
  18. return os.path.isdir(path)
  19. def get_path_replace_list():
  20. HOME_PATH_REPLACEMENT = '~'
  21. rv = []
  22. home_path = os.getenv("HOME")
  23. rv.append( (home_path, HOME_PATH_REPLACEMENT) )
  24. for p in os.listdir(home_path):
  25. full_path = os.path.join(home_path, p)
  26. if os.path.islink(full_path):
  27. rv.append( (os.path.realpath(full_path), os.path.join(HOME_PATH_REPLACEMENT, p)) )
  28. return rv
  29. template_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'latex_doc', 'templates')
  30. static_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'latex_doc', 'static')
  31. if __name__ == "__main__":
  32. parser = optparse.OptionParser("usage: %%prog [options] folder_for_document")
  33. parser.add_option("-t", "--type", dest="type", default=None, type="string", help="|".join(TYPES))
  34. parser.add_option("-f", "--force", dest="force", action="store_true", default=False)
  35. (options, args) = parser.parse_args()
  36. if len(args) != 1 or options.type is None or options.type not in TYPES:
  37. parser.print_help()
  38. else:
  39. target_file = os.path.join(os.path.abspath('.'), args[0])
  40. if not target_file.endswith(TARGET_EXTENTION):
  41. target_file = os.path.join(target_file, os.path.basename(target_file) + TARGET_EXTENTION)
  42. full_path = target_file
  43. for original, replacement in get_path_replace_list():
  44. if full_path.startswith(original):
  45. full_path = replacement + full_path[len(original):]
  46. break
  47. full_folder = os.path.dirname(full_path)
  48. filename = os.path.splitext(os.path.basename(target_file))[0]
  49. short_date = time.strftime('%d.%m.%Y')
  50. #
  51. # Main Actions
  52. #
  53. latex_jinja_env = jinja2.Environment(
  54. block_start_string = '\BLOCK{',
  55. block_end_string = '}',
  56. variable_start_string = '\VAR{',
  57. variable_end_string = '}',
  58. comment_start_string = '\#{',
  59. comment_end_string = '}',
  60. line_statement_prefix = '%_',
  61. line_comment_prefix = '%#',
  62. trim_blocks = True,
  63. autoescape = False,
  64. loader = jinja2.FileSystemLoader(template_path)
  65. )
  66. if not os.path.exists(target_file) or options.force:
  67. mkdir(os.path.dirname(target_file))
  68. with open(target_file, 'w') as fh:
  69. fh.write(latex_jinja_env.get_template(options.type + TARGET_EXTENTION).render(
  70. full_path=full_path,
  71. full_folder=full_folder,
  72. filename=filename,
  73. short_date=short_date).encode('UTF-8'))
  74. if options.type == 'merkblatt':
  75. shutil.copytree(os.path.join(static_path, options.type), os.path.join(os.path.dirname(target_file), 'icons'))
  76. else:
  77. print("File exists. Use option -f to create file")