|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | + |
| 5 | +def append_structure(module, step, name, count): |
| 6 | + "Генерирует структуру для решения задач главы модуля" |
| 7 | + filename = f"../src/module_{module}.py" |
| 8 | + path = Path(filename) |
| 9 | + |
| 10 | + if not path.exists(): |
| 11 | + print(f"❌ {filename} не найден!") |
| 12 | + return False |
| 13 | + |
| 14 | + with open(filename, "a", encoding="utf-8") as f: |
| 15 | + f.write(f"\n\n# === {module}.{step} {name} ===\n") |
| 16 | + for i in range(1, count + 1): |
| 17 | + f.write( |
| 18 | + f''' |
| 19 | + def m_{module}_{step}_{i}(): |
| 20 | + """ |
| 21 | + ... |
| 22 | + {"-"*37} |
| 23 | + ... |
| 24 | + """ |
| 25 | + pass |
| 26 | +
|
| 27 | +''' |
| 28 | + ) |
| 29 | + print(f"✅ Структура добавлена в {filename}") |
| 30 | + return True |
| 31 | + |
| 32 | + |
| 33 | +def append_docs(module, step, name, count): |
| 34 | + "Генерирует содержимое доков" |
| 35 | + filename = f"../docs/modules/module_{module}.rst" |
| 36 | + path = Path(filename) |
| 37 | + |
| 38 | + if not path.exists(): |
| 39 | + print(f"❌ {filename} не найден!") |
| 40 | + return False |
| 41 | + |
| 42 | + with open(filename, "a", encoding="utf-8") as f: |
| 43 | + title = f"\n\n{name} ({module}.{step})" |
| 44 | + length = max(26, (len(title) // 25 + 1) * 26) |
| 45 | + f.write(f"{title}\n{'-' * length}\n") |
| 46 | + for i in range(1, count + 1): |
| 47 | + line = f".. autofunction:: src.module_{module}.m_{module}_{step}_{i}()\n" |
| 48 | + f.write(line) |
| 49 | + |
| 50 | + print(f"✅ Описание добавлено в {filename}") |
| 51 | + return True |
| 52 | + |
| 53 | + |
| 54 | +def create_test_file(module, step, count): |
| 55 | + """Создаёт файл тестов с импортами""" |
| 56 | + filename = f"../tests/test_{module}_{step}.py" |
| 57 | + path = Path(filename) |
| 58 | + |
| 59 | + if path.exists() and path.stat().st_size > 0: |
| 60 | + print(f"⚠️ {filename} уже существует") |
| 61 | + return True |
| 62 | + |
| 63 | + imports = ",\n".join([f" m_{module}_{step}_{i}" for i in range(1, count + 1)]) |
| 64 | + content = f"import pytest\nfrom src.module_{module} import (\n{imports}\n)" |
| 65 | + |
| 66 | + with open(filename, "w", encoding="utf-8") as f: |
| 67 | + f.write(content) |
| 68 | + |
| 69 | + print(f"✅ Создан файл тестов: {filename} ({count} импортов)") |
| 70 | + return True |
| 71 | + |
| 72 | + |
| 73 | +def create_or_update_tests_docs(module, step, name): |
| 74 | + """Создаёт/обновляет страницу доков с тестами к модулю""" |
| 75 | + filename = f"../docs/modules/tests_{module}.rst" |
| 76 | + path = Path(filename) |
| 77 | + |
| 78 | + if not path.exists(): |
| 79 | + title = f"Тесты к Модулю {module}: НАЗВАНИЕ" |
| 80 | + length = 28 |
| 81 | + header_content = f""" |
| 82 | +Тесты к Модулю {module}: {name} |
| 83 | +{"=" * length} |
| 84 | +
|
| 85 | +.. contents:: |
| 86 | + :local: |
| 87 | +
|
| 88 | +""" |
| 89 | + with open(filename, "w", encoding="utf-8") as f: |
| 90 | + f.write(header_content) |
| 91 | + |
| 92 | + print(f"✅ Создан файл: {filename}") |
| 93 | + title = f"{name} ({module}.{step})" |
| 94 | + length = max(28, (len(title) // 28 + 1) * 28) |
| 95 | + section_content = f""" |
| 96 | +{title} |
| 97 | +{"-" * 28} |
| 98 | +
|
| 99 | +Файл: ``tests/test_{module}_{step}.py`` |
| 100 | +
|
| 101 | +.. literalinclude:: ../../tests/test_{module}_{step}.py |
| 102 | + :language: python |
| 103 | + :linenos: |
| 104 | +
|
| 105 | +""" |
| 106 | + |
| 107 | + with open(filename, "a", encoding="utf-8") as f: |
| 108 | + f.write(section_content) |
| 109 | + |
| 110 | + print(f"✅ Добавлена секция {name} ({module}.{step})") |
| 111 | + return True |
| 112 | + |
| 113 | + |
| 114 | +def main(): |
| 115 | + module = int(input("Номер модуля: ")) |
| 116 | + step = int(input("Номер шага: ")) |
| 117 | + count = int(input("Количество задач: ")) |
| 118 | + name = input("Название главы: ") |
| 119 | + |
| 120 | + success = [ |
| 121 | + append_structure(module, step, name, count), |
| 122 | + create_test_file(module, step, count), |
| 123 | + append_docs(module, step, name, count), |
| 124 | + create_or_update_tests_docs(module, step, name), |
| 125 | + ] |
| 126 | + |
| 127 | + if all(success): |
| 128 | + print("\n🎉 Готово!") |
| 129 | + else: |
| 130 | + print("\n❌ Ошибки при генерации!") |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + main() |
0 commit comments