-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_library.py
More file actions
65 lines (52 loc) · 1.65 KB
/
update_library.py
File metadata and controls
65 lines (52 loc) · 1.65 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
import json
import csv
import os
import sys
# Load issue body from environment variable
issue_body = os.environ.get("ISSUE_BODY", "")
try:
# Try to parse JSON
data = json.loads(issue_body)
# Validate required keys
required_keys = {"barcode", "user", "action"}
if not isinstance(data, dict):
raise ValueError("Issue body is not a JSON object")
missing = required_keys - data.keys()
if missing:
raise ValueError(f"Missing required keys: {missing}")
except json.JSONDecodeError:
print("❌ Issue body is not valid JSON.")
sys.exit(1)
except ValueError as e:
print(f"❌ Validation error: {e}")
sys.exit(1)
# If we reach here, JSON is valid and has required fields
barcode = data["barcode"]
user = data["user"]
action = data["action"]
# Load library data
with open("data/library.json", "r") as f:
books = json.load(f)
# Update book status
updated = False
for book in books:
if book["Barcode"] == barcode:
if action == "checkout":
book["Status"] = "Checked Out"
book["User"] = user
updated = True
elif action == "checkin":
book["Status"] = "Available"
book["User"] = ""
updated = True
if not updated:
print(f"⚠️ No book found with barcode {barcode}.")
sys.exit(1)
# Save updated library data
with open("data/library.json", "w") as f:
json.dump(books, f, indent=2)
with open("data/library.csv", "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["Book Title", "Author", "Barcode", "Status", "User"])
writer.writeheader()
writer.writerows(books)
print("✅ Library updated successfully.")