-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcardtemplate.py
More file actions
53 lines (48 loc) · 2.24 KB
/
cardtemplate.py
File metadata and controls
53 lines (48 loc) · 2.24 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
# cardtemplate.py
import json
class CardTemplate:
def __init__(self, data):
self.width = data.get("width", 640) # Default width if not provided
self.height = data.get("height", 920) # Default height if not provided
self.bleed = data.get("bleed", 0) # Default bleed if not provided
self.layers = data.get("layers", []) # Default empty list if not provided
self.data_fields = data.get("data_fields", []) # Default empty list if not provided
self.fonts = data.get("fonts", {}) # Default empty dictionary if not provided
self.data_field_positions = data.get("data_field_positions", {}) # Default empty dictionary if not provided
self.card_image_path = data.get("card_image_path", "") # Default empty string if not provided
def set_card_image_path(self, path):
self.card_image_path = path
@classmethod
def load_from_json(cls, file_path):
try:
with open(file_path, "r") as f:
data = json.load(f)
return cls(data)
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Failed to load template: {e}")
return None
def update(self, data):
self.width = data.get("width", self.width)
self.height = data.get("height", self.height)
self.bleed = data.get("bleed", self.bleed)
self.layers = data.get("layers", self.layers)
self.data_fields = data.get("data_fields", self.data_fields)
self.fonts = data.get("fonts", self.fonts)
self.data_field_positions = data.get("data_field_positions", self.data_field_positions)
self.card_image_path = data.get("card_image_path", self.card_image_path)
def save_to_json(self, file_path):
data = {
"width": self.width,
"height": self.height,
"bleed": self.bleed,
"layers": self.layers,
"data_fields": self.data_fields,
"fonts": self.fonts,
"data_field_positions": self.data_field_positions,
"card_image_path": self.card_image_path,
}
try:
with open(file_path, "w") as f:
json.dump(data, f)
except IOError as e:
print(f"Failed to save template: {e}")