A bin folder, holding helpfull scripts and commands
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/home/dirk/bin/venv/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import jinja2
  4. import optparse
  5. import os
  6. import shutil
  7. import time
  8. TYPES = ['brief', 'merkblatt', 'präsentation']
  9. TARGET_EXTENTION = '.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. rv.sort(reverse=True)
  29. return rv
  30. template_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'latex_doc', 'templates')
  31. static_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'latex_doc', 'static')
  32. if __name__ == "__main__":
  33. parser = optparse.OptionParser("usage: %%prog [options] folder_for_document")
  34. parser.add_option("-t", "--type", dest="type", default=None, type="string", help="|".join(TYPES))
  35. parser.add_option("-f", "--force", dest="force", action="store_true", default=False)
  36. (options, args) = parser.parse_args()
  37. if len(args) != 1 or options.type is None or options.type not in TYPES:
  38. parser.print_help()
  39. else:
  40. target_file = os.path.join(os.path.abspath('.'), args[0])
  41. if not target_file.endswith(TARGET_EXTENTION):
  42. target_file = os.path.join(target_file, os.path.basename(target_file) + TARGET_EXTENTION)
  43. full_path = target_file
  44. for original, replacement in get_path_replace_list():
  45. if full_path.startswith(original):
  46. full_path = replacement + full_path[len(original):]
  47. break
  48. full_path = full_path.replace('~', '$\\sim$').replace('_', '\_')
  49. full_folder = os.path.dirname(full_path)
  50. filename = os.path.splitext(os.path.basename(target_file))[0]
  51. short_date = time.strftime('%d.%m.%Y')
  52. #
  53. # Main Actions
  54. #
  55. latex_jinja_env = jinja2.Environment(
  56. block_start_string = '\BLOCK{',
  57. block_end_string = '}',
  58. variable_start_string = '\VAR{',
  59. variable_end_string = '}',
  60. comment_start_string = '\#{',
  61. comment_end_string = '}',
  62. line_statement_prefix = '%_',
  63. line_comment_prefix = '%#',
  64. trim_blocks = True,
  65. autoescape = False,
  66. loader = jinja2.FileSystemLoader(template_path)
  67. )
  68. if not os.path.exists(target_file) or options.force:
  69. mkdir(os.path.dirname(target_file))
  70. with open(target_file, 'w') as fh:
  71. fh.write(latex_jinja_env.get_template(options.type + TARGET_EXTENTION).render(
  72. full_path=full_path,
  73. full_folder=full_folder,
  74. filename=filename,
  75. short_date=short_date))
  76. if options.type == 'merkblatt':
  77. shutil.copytree(os.path.join(static_path, options.type), os.path.join(os.path.dirname(target_file), 'icons'))
  78. else:
  79. print("File exists. Use option -f to create file")