Reza-galaxy21 commited on
Commit
a15ad18
·
verified ·
1 Parent(s): 42c85d0

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +29 -3
utils.py CHANGED
@@ -1,5 +1,8 @@
1
  import pandas as pd
2
  import json
 
 
 
3
 
4
  def parse_excel_file(file_path):
5
  try:
@@ -38,11 +41,34 @@ def parse_excel_file(file_path):
38
  }
39
  materials.append(material)
40
 
 
 
 
41
  return materials
42
 
43
- def save_to_json(data, output_path="material_db.json"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  try:
45
- with open(output_path, "w", encoding="utf-8") as f:
46
- json.dump(data, f, indent=2, ensure_ascii=False)
47
  except Exception as e:
48
  raise ValueError(f"❌ خطا در ذخیره فایل JSON: {str(e)}")
 
1
  import pandas as pd
2
  import json
3
+ import os
4
+
5
+ DB_FILE = "material_db.json"
6
 
7
  def parse_excel_file(file_path):
8
  try:
 
41
  }
42
  materials.append(material)
43
 
44
+ if not materials:
45
+ raise ValueError("⚠️ هیچ ماده معتبری از فایل استخراج نشد.")
46
+
47
  return materials
48
 
49
+ def load_projects():
50
+ if not os.path.exists(DB_FILE):
51
+ return [] # فایل وجود نداره؟ یعنی دیتابیس خالیه
52
+ with open(DB_FILE, "r", encoding="utf-8") as f:
53
+ try:
54
+ return json.load(f)
55
+ except json.JSONDecodeError:
56
+ return [] # خراب بود؟ فایل رو صفر در نظر بگیر
57
+
58
+ def save_project(description, materials):
59
+ projects = load_projects()
60
+
61
+ for p in projects:
62
+ if p.get("description") == description:
63
+ raise ValueError("⚠️ پروژه‌ای با این توصیف قبلاً ثبت شده است.")
64
+
65
+ projects.append({
66
+ "description": description,
67
+ "materials": materials
68
+ })
69
+
70
  try:
71
+ with open(DB_FILE, "w", encoding="utf-8") as f:
72
+ json.dump(projects, f, indent=2, ensure_ascii=False)
73
  except Exception as e:
74
  raise ValueError(f"❌ خطا در ذخیره فایل JSON: {str(e)}")