33from typing_extensions import TypeVar , dataclass_transform
44
55from tppt .exception import (
6+ MasterLayoutNotFoundError ,
67 SlideMasterAttributeMustBeSlideLayoutError ,
78 SlideMasterAttributeNotFoundError ,
89)
2728
2829class _SlideMasterMeta (type ):
2930 def __getattr__ (self , key : str ) -> "type[SlideLayout]" :
30- # 1. クラス自身の属性を確認
31+ # 1. Check the class's own attributes
3132 if key in self .__dict__ :
3233 value = self .__dict__ [key ]
3334 if isinstance (value , type ) and issubclass (value , SlideLayout ):
3435 return value
3536
36- # 2. アノテーションを確認
37+ # 2. Check annotations
3738 if hasattr (self , "__annotations__" ) and key in self .__annotations__ :
3839 annotation = self .__annotations__ [key ]
3940
40- # Annotated型からの抽出
41+ # Extract from Annotated type
4142 origin = get_origin (annotation )
4243 if origin is Annotated :
4344 args = get_args (annotation )
@@ -49,7 +50,7 @@ def __getattr__(self, key: str) -> "type[SlideLayout]":
4950 return args [0 ]
5051 else :
5152 raise SlideMasterAttributeMustBeSlideLayoutError (key )
52- # クラスの場合は直接チェック
53+ # Direct check for class type
5354 elif isinstance (annotation , type ) and issubclass (annotation , SlideLayout ):
5455 return annotation
5556 else :
@@ -103,3 +104,34 @@ class DefaultSlideMaster(SlideMaster):
103104 "GenericTpptSlideMaster" ,
104105 bound = SlideMaster ,
105106)
107+
108+ def get_master_layout (slide_master : type [SlideMaster ]) -> type [SlideLayout ]:
109+ """Get the slide tagged with MasterLayout."""
110+ for attr_name , annotation in slide_master .__annotations__ .items ():
111+ origin = get_origin (annotation )
112+ if origin is Annotated :
113+ args = get_args (annotation )
114+ # Check the class name instead of directly checking the type of args[1]
115+ if len (args ) > 1 and args [1 ].__class__ .__name__ == "MasterLayout" :
116+ return getattr (slide_master , attr_name )
117+
118+ raise MasterLayoutNotFoundError (slide_master .__name__ )
119+
120+
121+ def get_layouts (slide_master : type [SlideMaster ]) -> list [type [SlideLayout ]]:
122+ """Get an array of slides tagged with Layout."""
123+ layouts = []
124+
125+ for attr_name , annotation in slide_master .__annotations__ .items ():
126+ origin = get_origin (annotation )
127+ if origin is Annotated :
128+ args = get_args (annotation )
129+ # Identify Layout and MasterLayout using class names
130+ if len (args ) > 1 :
131+ if args [1 ].__class__ .__name__ == "Layout" :
132+ layouts .append (getattr (slide_master , attr_name ))
133+ elif args [1 ].__class__ .__name__ == "MasterLayout" :
134+ # Exclude MasterLayout
135+ pass
136+
137+ return layouts
0 commit comments