Добавил:
СПбГУТ * ИКСС * Программная инженерия Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Содержание ВКР / ВКР 2022 (с приложениями). Коваленко Л.А. Разработка конструктора нейронных сетей.docx
Скачиваний:
143
Добавлен:
11.06.2022
Размер:
16.55 Mб
Скачать

Приложение м. Модуль «models/template.Py»

  1. import os

  1. import pathlib

  2. import re

  3. from typing import List, Tuple

  4. from slugify import slugify

  5. DATASETS_TEMPLATES_DIR = os.path.join(pathlib.Path(__file__).parent.resolve(), 'datasets')

  6. assert os.path.exists(DATASETS_TEMPLATES_DIR), \

  7. f"Directory '{DATASETS_TEMPLATES_DIR}' is not exists"

  8. assert os.path.isdir(DATASETS_TEMPLATES_DIR), \

  9. f"Object '{DATASETS_TEMPLATES_DIR}' is not a directory"

  10. ARCHITECTURE_TEMPLATES_DIR = os.path.join(pathlib.Path(__file__).parent.resolve(), 'architecture')

  11. assert os.path.exists(ARCHITECTURE_TEMPLATES_DIR), \

  12. f"Directory '{ARCHITECTURE_TEMPLATES_DIR}' is not exists"

  13. assert os.path.isdir(ARCHITECTURE_TEMPLATES_DIR), \

  14. f"Object '{ARCHITECTURE_TEMPLATES_DIR}' is not a directory"

  15. TRAIN_VAL_TEST_TEMPLATES_DIR = os.path.join(pathlib.Path(__file__).parent.resolve(), 'train_val_test')

  16. assert os.path.exists(TRAIN_VAL_TEST_TEMPLATES_DIR), \

  17. f"Directory '{TRAIN_VAL_TEST_TEMPLATES_DIR}' is not exists"

  18. assert os.path.isdir(TRAIN_VAL_TEST_TEMPLATES_DIR), \

  19. f"Object '{TRAIN_VAL_TEST_TEMPLATES_DIR}' is not a directory"

  20. VISUALIZATION_TEMPLATES_DIR = os.path.join(pathlib.Path(__file__).parent.resolve(), 'visualization')

  21. assert os.path.exists(VISUALIZATION_TEMPLATES_DIR), \

  22. f"Directory '{VISUALIZATION_TEMPLATES_DIR}' is not exists"

  23. assert os.path.isdir(VISUALIZATION_TEMPLATES_DIR), \

  24. f"Object '{VISUALIZATION_TEMPLATES_DIR}' is not a directory"

  25. EXPORT_TEMPLATES_DIR = os.path.join(pathlib.Path(__file__).parent.resolve(), 'export')

  26. assert os.path.exists(EXPORT_TEMPLATES_DIR), \

  27. f"Directory '{EXPORT_TEMPLATES_DIR}' is not exists"

  28. assert os.path.isdir(EXPORT_TEMPLATES_DIR), \

  29. f"Object '{EXPORT_TEMPLATES_DIR}' is not a directory"

  30. def list_templates(dir: str) -> List[Tuple[str, str, str]]:

  31. dir_path, dir_names, filenames = next(os.walk(dir,

  32. topdown=True,

  33. onerror=None,

  34. followlinks=False), ('', [], []))

  35. filenames = [os.path.join(dir, filename) for filename in filenames]

  36. template_name_re = re.compile(r'# template-name:(.*)')

  37. template_type_re = re.compile(r'# template-type:(.*)')

  38. files = [] # list of tuples: (filename, template-name, template-type)

  39. for filename in filenames:

  40. try:

  41. template_name = None

  42. template_type = None

  43. with open(filename, 'r', encoding='utf-8') as fp:

  44. for line in fp.readlines():

  45. line = line.strip(' \t\r\n')

  46. match = template_name_re.match(line)

  47. if bool(match):

  48. if template_name is None:

  49. template_name = match.group(1).lstrip(' \t')

  50. else:

  51. template_name += os.linesep + match.group(1).lstrip(' \t')

  52. if template_type is None:

  53. match = template_type_re.match(line)

  54. if bool(match):

  55. template_type = match.group(1).lstrip(' \t')

  56. if template_name is not None and template_type is not None:

  57. files.append((filename, template_type, template_name))

  58. except:

  59. __import__('traceback').print_exc()

  60. return files

  61. def get_template(dir: str, filename: str) -> List[Tuple[str, str, str]]:

  62. dir_path, dir_names, filenames = next(os.walk(dir,

  63. topdown=True,

  64. onerror=None,

  65. followlinks=False), ('', [], []))

  66. filenames = [os.path.join(dir, filename) for filename in filenames]

  67. if filename in filenames:

  68. template_code_block_name_re = re.compile(r'#\s*?<code-block>(.*)')

  69. template_text_block_name_re = re.compile(r'#\s*?<text-block>(.*)')

  70. with open(filename, mode='r', encoding='utf-8') as fp:

  71. blocks = [] # elem -- tuple: (Type:Union['code', 'text'], Caption: str, Content: str)

  72. current_block = []

  73. for line in fp.readlines():

  74. line = line.rstrip(' \t\r\n')

  75. match_code_block = template_code_block_name_re.match(line)

  76. match_text_block = template_text_block_name_re.match(line)

  77. if bool(match_code_block) or bool(match_text_block):

  78. if current_block:

  79. blocks.append(tuple(current_block))

  80. if bool(match_code_block):

  81. block_type = 'code'

  82. caption = match_code_block.group(1).lstrip(' \t')

  83. else:

  84. block_type = 'text'

  85. caption = match_text_block.group(1).lstrip(' \t')

  86. current_block = [block_type, caption, '']

  87. continue

  88. if current_block:

  89. current_block[2] += os.linesep * bool(current_block[2]) + line

  90. if current_block:

  91. blocks.append(tuple(current_block))

  92. return blocks

  93. return [('text', '', 'no template found')]

  94. def create_template(dir_or_filename: str, template_type: str,

  95. template_name: str, blocks: List[Tuple[str, str, str]],

  96. rename: bool = False):

  97. lines = []

  98. for name_part in template_name.splitlines():

  99. name_part = name_part.strip(" \t\r\n")

  100. lines.append(f'# template-name: {name_part}')

  101. template_type = template_type.strip(" \t\r\n")

  102. lines.append(f'# template-type: {template_type}')

  103. for block_type, caption, content in blocks:

  104. if block_type == 'code':

  105. caption = caption.strip(" \t\r\n")

  106. lines.append(rf'# <code-block> {caption}')

  107. lines.append(content)

  108. elif block_type == 'text':

  109. caption = caption.strip(" \t\r\n")

  110. lines.append(rf'# <text-block> {caption}')

  111. lines.append(content)

  112. dir = dir_or_filename if os.path.isdir(dir_or_filename) else os.path.dirname(dir_or_filename)

  113. if rename or os.path.isdir(dir_or_filename):

  114. filename = slugify(f'{template_type}_{template_name}',

  115. entities=True, decimal=True, hexadecimal=True, max_length=50,

  116. word_boundary=True, separator='_')

  117. filename = os.path.join(dir, filename + '.py')

  118. else:

  119. filename = dir_or_filename

  120. with open(filename, mode='wb') as fp:

  121. fp.write(os.linesep.join(lines).encode('utf-8'))

  122. return filename

  123. def edit_template(filename: str, template_type: str, template_name: str, blocks: List[Tuple[str, str, str]],

  124. rename: bool = False):

  125. template_filename = create_template(filename, template_type, template_name, blocks, rename)

  126. if os.path.isfile(filename) and os.path.exists(filename) and os.path.exists(template_filename) and \

  127. os.path.abspath(filename) != os.path.abspath(template_filename):

  128. del_template(filename)

  129. def del_template(filename: str):

  130. os.remove(filename)