-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
91 lines (71 loc) · 2.98 KB
/
cli.py
File metadata and controls
91 lines (71 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
cli.py — интерфейс командной строки для DocFlow.
Запуск: python cli.py
"""
import os
import sys
import platform
import subprocess
from pathlib import Path
from generator import get_data_files, get_templates, generate_pdf
def clear():
os.system("cls" if platform.system() == "Windows" else "clear")
def print_banner():
print("\033[1;36m")
print("╔══════════════════════════════════════════╗")
print("║ DocFlow PDF Generator ║")
print("║ Счета · Акты · Отчёты → PDF ║")
print("╚══════════════════════════════════════════╝")
print("\033[0m")
def choose_from_list(items, title):
print(f"\n\033[1m{title}\033[0m")
print("─" * 44)
for i, item in enumerate(items, 1):
name = Path(item).name if hasattr(item, "name") else str(item)
print(f" \033[33m{i:>2}\033[0m. {name}")
print()
while True:
try:
choice = int(input(" Введите номер: "))
if 1 <= choice <= len(items):
return choice - 1
print(f" \033[31m⚠ Введите число от 1 до {len(items)}\033[0m")
except ValueError:
print(" \033[31m⚠ Введите корректное число\033[0m")
def open_file(path):
try:
if platform.system() == "Windows":
os.startfile(str(path))
elif platform.system() == "Darwin":
subprocess.run(["open", str(path)], check=True)
else:
subprocess.run(["xdg-open", str(path)], check=True)
except Exception as e:
print(f" \033[33m⚠ Не удалось открыть: {e}\033[0m")
def run():
clear()
print_banner()
data_files = get_data_files()
if not data_files:
print("\033[31m✗ Файлы данных не найдены в папке /data\033[0m")
sys.exit(1)
data_idx = choose_from_list(data_files, "📂 Файлы данных (data/)")
data_path = data_files[data_idx]
templates = get_templates()
tmpl_idx = choose_from_list(templates, "📄 Тип документа")
template = templates[tmpl_idx]
print(f"\n \033[90mДанные: {data_path.name}\033[0m")
print(f" \033[90mШаблон: {template}\033[0m")
print("\n ⏳ Генерирую PDF...")
try:
output_path = generate_pdf(data_path, template)
print(f"\n \033[1;32m✓ PDF создан:\033[0m {output_path}")
answer = input("\n Открыть файл? [Y/n]: ").strip().lower()
if answer in ("", "y", "д", "да"):
open_file(output_path)
except Exception as e:
print(f"\n \033[31m✗ Ошибка: {e}\033[0m")
sys.exit(1)
print("\n До свидания!\n")
if __name__ == "__main__":
run()