-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (68 loc) · 3.52 KB
/
main.py
File metadata and controls
94 lines (68 loc) · 3.52 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
92
93
94
import json
import os
from pprint import pprint
from uuid import uuid4
from typer import Typer, confirm, echo, prompt
from models import ProductionSchema, ProductionSchemaStage
app = Typer()
OUTPUT_DIR: str = "output"
@app.command()
def merge_json(output_filename: str = "merged.json") -> None:
def gat_all_abs_paths(directory: str = OUTPUT_DIR):
for dir_path, _, filenames in os.walk(directory):
for filename in filenames:
yield os.path.abspath(os.path.join(dir_path, filename))
files = (path for path in gat_all_abs_paths(OUTPUT_DIR) if path.endswith(".json") and output_filename not in path)
schemas = []
for path in files:
echo(f"Обработка {path}")
with open(path) as f:
schema = json.load(f)
schemas.append(schema)
output_filename = OUTPUT_DIR + "/" + output_filename
with open(output_filename, "w") as f:
json.dump(schemas, f, ensure_ascii=False, indent=4)
echo(f"Единый JSON файл сохранен в {output_filename}")
def dump_schema_json(schema: ProductionSchema) -> None:
schema_dict = schema.dict()
output_filename = f"{OUTPUT_DIR}/{schema.unit_name}-{schema.schema_id}.json"
with open(output_filename, "w") as f:
json.dump(schema_dict, f, ensure_ascii=False, indent=4)
echo(f"Схема {schema.unit_name} сохранена в файл {output_filename}")
def create_schema_stage() -> ProductionSchemaStage:
stage = ProductionSchemaStage(
name=prompt(text="Название этапа", default="", type=str).strip() or None,
type=prompt(text="Тип этапа", default="", type=str).strip() or None,
description=prompt(text="Описание этапа", default="", type=str).strip() or None,
equipment=prompt(text="Список оборудования (через пробел)", default="", type=str).split() or None,
workplace=prompt(text="Рабочее место", default="", type=str).strip() or None,
duration_seconds=prompt(text="Продолжительность в секундах", default=0, type=int),
)
echo("Введённый этап:")
pprint(stage.dict())
return stage
@app.command()
def create_schema() -> None:
schema = ProductionSchema(
unit_name=prompt(text="Название изделия", type=str).strip(),
unit_short_name=prompt(text="Краткое название изделия", default="", type=str).strip() or None,
schema_id=prompt(text="UUID (перезапись)", default=uuid4().hex, type=str).strip(),
required_components_schema_ids=(
prompt(text="ID схем компонентов (через пробел)", default="", type=str).split() or None
),
parent_schema_id=prompt(text="ID родительской схемы", default="", type=str).strip() or None,
schema_type=prompt(text="Тип изделия", type=str).strip(),
)
production_stages = []
while confirm(text="Задать производственный этап", default=True, prompt_suffix="? "):
stage = create_schema_stage()
if confirm(text="Присвоить этап схеме", default=True, prompt_suffix="? "):
production_stages.append(stage)
schema.production_stages = production_stages or None
echo("Введённая схема:")
pprint(schema.dict())
dump_schema_json(schema)
if __name__ == "__main__":
if not os.path.exists(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
app()