Skip to content

Commit a3c64a5

Browse files
committed
fix: make paper size pydantic compatible
1 parent 79f67b9 commit a3c64a5

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

src/structurizr/view/paper_size.py

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,58 @@
2727
class PaperSize(Enum):
2828
"""Represent paper sizes in pixels at 300dpi."""
2929

30-
A6_Portrait = ("A6", Orientation.Portrait, 1240, 1748)
31-
A6_Landscape = ("A6", Orientation.Landscape, 1748, 1240)
30+
def __new__(
31+
cls,
32+
value: str,
33+
size: str,
34+
orientation: Orientation,
35+
width: int,
36+
height: int,
37+
**kwargs
38+
) -> "PaperSize":
39+
"""
40+
Construct a specific paper size but with a string value.
3241
33-
A5_Portrait = ("A5", Orientation.Portrait, 1748, 2480)
34-
A5_Landscape = ("A5", Orientation.Landscape, 2480, 1748)
42+
References:
43+
https://docs.python.org/3/library/enum.html#when-to-use-new-vs-init
3544
36-
A4_Portrait = ("A4", Orientation.Portrait, 2480, 3508)
37-
A4_Landscape = ("A4", Orientation.Landscape, 3508, 2480)
45+
"""
46+
obj = object.__new__(cls)
47+
obj._value_ = value
48+
obj.size = size
49+
obj.orientation = orientation
50+
obj.width = width
51+
obj.height = height
3852

39-
A3_Portrait = ("A3", Orientation.Portrait, 3508, 4961)
40-
A3_Landscape = ("A3", Orientation.Landscape, 4961, 3508)
53+
return obj
4154

42-
A2_Portrait = ("A2", Orientation.Portrait, 4961, 7016)
43-
A2_Landscape = ("A2", Orientation.Landscape, 7016, 4961)
55+
A6_Portrait = ("A6_Portrait", "A6", Orientation.Portrait, 1240, 1748)
56+
A6_Landscape = ("A6_Landscape", "A6", Orientation.Landscape, 1748, 1240)
4457

45-
A1_Portrait = ("A1", Orientation.Portrait, 7016, 9933)
46-
A1_Landscape = ("A1", Orientation.Landscape, 9933, 7016)
58+
A5_Portrait = ("A5_Portrait", "A5", Orientation.Portrait, 1748, 2480)
59+
A5_Landscape = ("A5_Landscape", "A5", Orientation.Landscape, 2480, 1748)
4760

48-
A0_Portrait = ("A0", Orientation.Portrait, 9933, 14043)
49-
A0_Landscape = ("A0", Orientation.Landscape, 14043, 9933)
61+
A4_Portrait = ("A4_Portrait", "A4", Orientation.Portrait, 2480, 3508)
62+
A4_Landscape = ("A4_Landscape", "A4", Orientation.Landscape, 3508, 2480)
5063

51-
Letter_Portrait = ("Letter", Orientation.Portrait, 2550, 3300)
52-
Letter_Landscape = ("Letter", Orientation.Landscape, 3300, 2550)
64+
A3_Portrait = ("A3_Portrait", "A3", Orientation.Portrait, 3508, 4961)
65+
A3_Landscape = ("A3_Landscape", "A3", Orientation.Landscape, 4961, 3508)
5366

54-
Legal_Portrait = ("Legal", Orientation.Portrait, 2550, 4200)
55-
Legal_Landscape = ("Legal", Orientation.Landscape, 4200, 2550)
67+
A2_Portrait = ("A2_Portrait", "A2", Orientation.Portrait, 4961, 7016)
68+
A2_Landscape = ("A2_Landscape", "A2", Orientation.Landscape, 7016, 4961)
5669

57-
Slide_4_3 = ("Slide 4:3", Orientation.Landscape, 3306, 2480)
58-
Slide_16_9 = ("Slide 16:9", Orientation.Landscape, 3508, 1973)
59-
Slide_16_10 = ("Slide 16:10", Orientation.Landscape, 3508, 2193)
70+
A1_Portrait = ("A1_Portrait", "A1", Orientation.Portrait, 7016, 9933)
71+
A1_Landscape = ("A1_Landscape", "A1", Orientation.Landscape, 9933, 7016)
6072

61-
def __init__(
62-
self, name: str, orientation: Orientation, width: int, height: int, **kwargs
63-
) -> None:
64-
"""Initialize a specific paper size."""
65-
super().__init__(**kwargs)
66-
self.size_name = name
67-
self.orientation = orientation
68-
self.width = width
69-
self.height = height
73+
A0_Portrait = ("A0_Portrait", "A0", Orientation.Portrait, 9933, 14043)
74+
A0_Landscape = ("A0_Landscape", "A0", Orientation.Landscape, 14043, 9933)
75+
76+
Letter_Portrait = ("Letter_Portrait", "Letter", Orientation.Portrait, 2550, 3300)
77+
Letter_Landscape = ("Letter_Landscape", "Letter", Orientation.Landscape, 3300, 2550)
78+
79+
Legal_Portrait = ("Legal_Portrait", "Legal", Orientation.Portrait, 2550, 4200)
80+
Legal_Landscape = ("Legal_Landscape", "Legal", Orientation.Landscape, 4200, 2550)
81+
82+
Slide_4_3 = ("Slide_4_3", "Slide 4:3", Orientation.Landscape, 3306, 2480)
83+
Slide_16_9 = ("Slide_16_9", "Slide 16:9", Orientation.Landscape, 3508, 1973)
84+
Slide_16_10 = ("Slide_16_10", "Slide 16:10", Orientation.Landscape, 3508, 2193)

0 commit comments

Comments
 (0)