Skip to content

Commit a4692b5

Browse files
committed
feat: enhance get_placeholders function to support both class and instance inputs
1 parent 07ffb8d commit a4692b5

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

design/slide_master.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,28 @@ class MySlideLayout(TpptSlideLayout):
7575
text: Annotated[str, Placeholder]
7676

7777

78-
def get_placeholders(slide: type[TpptSlideLayout]) -> dict[str, Any]:
79-
"""クラスのメタ情報を解析し、プレースホルダーのフィールドのキーバリューを取得する"""
80-
return slide.__placeholders__
78+
def get_placeholders(
79+
slide_or_class: type[TpptSlideLayout] | TpptSlideLayout,
80+
) -> dict[str, Any]:
81+
"""クラスのメタ情報を解析し、プレースホルダーのフィールドのキーバリューを取得する
82+
83+
引数にインスタンスが渡された場合は、実際のプレースホルダー値を返す
84+
クラスが渡された場合は、プレースホルダーの型情報を返す
85+
"""
86+
if isinstance(slide_or_class, type):
87+
# クラスが渡された場合は型情報を返す
88+
return slide_or_class.__placeholders__
89+
else:
90+
# インスタンスの場合は実際の値を返す
91+
values = {}
92+
for field_name in slide_or_class.__class__.__placeholders__:
93+
if hasattr(slide_or_class, field_name):
94+
values[field_name] = getattr(slide_or_class, field_name)
95+
return values
8196

8297

8398
# TODO: 以下のアサーションが通るようにせよ
8499
Myslide: type[TpptSlideLayout] = MySlideLayout
85100
myslide = Myslide(title="a", text="b")
86101

87-
assert get_placeholders(Myslide) == {"title": "a", "text": "b"}
102+
assert get_placeholders(myslide) == {"title": "a", "text": "b"}

0 commit comments

Comments
 (0)