Skip to content

Commit b5ac6f0

Browse files
committed
refactor: remove redundant initializers and methods from various classes; add ShadowFormat class for shadow effects
1 parent 80497f9 commit b5ac6f0

File tree

18 files changed

+176
-241
lines changed

18 files changed

+176
-241
lines changed

src/tppt/pptx/action.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pptx.action import ActionSetting as _PptxActionSetting
2+
from pptx.action import Hyperlink as _PptxHyperlink
3+
from pptx.enum.action import PP_ACTION_TYPE
4+
5+
from tppt.pptx.converter import PptxConvertible
6+
from tppt.pptx.shape import SubShape
7+
from tppt.pptx.slide import Slide
8+
9+
10+
class Hyperlink(SubShape[_PptxHyperlink]):
11+
@property
12+
def address(self) -> str | None:
13+
return self._pptx.address
14+
15+
@address.setter
16+
def address(self, value: str | None):
17+
self._pptx.address = value
18+
19+
20+
class ActionSetting(PptxConvertible[_PptxActionSetting]):
21+
@property
22+
def action(self) -> PP_ACTION_TYPE:
23+
return self._pptx.action
24+
25+
@property
26+
def hyperlink(self) -> Hyperlink:
27+
return Hyperlink(self._pptx.hyperlink)
28+
29+
@property
30+
def target_slide(self) -> Slide | None:
31+
if target_slide := self._pptx.target_slide:
32+
return Slide(target_slide)
33+
return None
34+
35+
@target_slide.setter
36+
def target_slide(self, value: Slide | None):
37+
if value is None:
38+
self._pptx.target_slide = None
39+
else:
40+
self._pptx.target_slide = value._pptx

src/tppt/pptx/dml/effect.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pptx.dml.effect import ShadowFormat as _PptxShadowFormat
2+
3+
from tppt.pptx.converter import PptxConvertible
4+
5+
6+
class ShadowFormat(PptxConvertible[_PptxShadowFormat]):
7+
@property
8+
def inherit(self) -> bool:
9+
return self._pptx.inherit
10+
11+
@inherit.setter
12+
def inherit(self, value: bool) -> None:
13+
self._pptx.inherit = value

src/tppt/pptx/presentation.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
from .slide import SlideBuilder
2323

2424
if TYPE_CHECKING:
25+
from tppt.pptx.shape import BaseShape
2526
from tppt.pptx.shape.placeholder import MasterPlaceholder
2627
from tppt.pptx.slide import Slide
27-
28-
from .slide_master import SlideMaster
28+
from tppt.pptx.slide_master import SlideMaster
2929

3030

3131
class Presentation(PptxConvertible[_PptxPresentation]):
@@ -40,7 +40,7 @@ def __init__(
4040
from pptx import Presentation
4141

4242
pptx = Presentation(os.fspath(pptx))
43-
self._pptx = pptx
43+
super().__init__(pptx)
4444

4545
@property
4646
def core_properties(self) -> _PptxCorePropertiesPart:
@@ -121,15 +121,6 @@ def save(self, file: FilePath | IO[bytes]) -> None:
121121
file = os.fspath(file)
122122
self._pptx.save(file)
123123

124-
def to_pptx(self) -> _PptxPresentation:
125-
"""Convert to pptx presentation."""
126-
return self._pptx
127-
128-
@classmethod
129-
def from_pptx(cls, pptx_obj: _PptxPresentation) -> Self:
130-
"""Create from pptx presentation."""
131-
return cls(pptx_obj)
132-
133124

134125
class PresentationBuilder(Generic[GenericTpptSlideMaster]):
135126
"""Builder for presentations."""
@@ -206,6 +197,13 @@ def placeholders(self) -> "list[MasterPlaceholder]":
206197
MasterPlaceholder(placeholder) for placeholder in self._pptx.placeholders
207198
]
208199

200+
@property
201+
def shapes(self) -> "list[BaseShape]":
202+
"""Get the shapes."""
203+
from tppt.pptx.shape import BaseShape
204+
205+
return [BaseShape(shape) for shape in self._pptx.shapes]
206+
209207

210208
class NotesMaster(_BaseMaster):
211209
"""Notes master."""

src/tppt/pptx/shape/__init__.py

Lines changed: 100 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
"""Shape wrapper implementation."""
22

3-
from typing import TYPE_CHECKING, Self, TypedDict
3+
from typing import TYPE_CHECKING, TypedDict
44

5-
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
6-
from pptx.opc.package import XmlPart
75
from pptx.shapes import Subshape as PptxSubshape
86
from pptx.shapes.autoshape import Shape as PptxShape
97
from pptx.shapes.base import BaseShape as PptxBaseShape
8+
from pptx.util import Length as PptxLength
109
from typing_extensions import TypeVar
1110

12-
from tppt.pptx.converter import PptxConvertible
13-
from tppt.types._length import Length, LiteralLength
11+
from tppt.pptx.converter import PptxConvertible, to_pptx_length
12+
from tppt.types._length import Length, LiteralLength, to_length
1413

1514
if TYPE_CHECKING:
15+
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE, MSO_SHAPE_TYPE
16+
from pptx.oxml.shapes import ShapeElement as PptxShapeElement
17+
from pptx.parts.slide import BaseSlidePart as PptxBaseSlidePart
18+
from pptx.shapes.base import _PlaceholderFormat as PptxPlaceholderFormat
19+
20+
from tppt.pptx.action import ActionSetting
21+
from tppt.pptx.dml.effect import ShadowFormat
1622
from tppt.pptx.dml.fill import FillFormat
1723
from tppt.pptx.dml.line import LineFormat
1824
from tppt.pptx.text.text_frame import TextFrame
@@ -32,15 +38,95 @@
3238

3339

3440
class BaseShape(PptxConvertible[GenericPptxBaseShape]):
35-
def __init__(self, pptx_shape: GenericPptxBaseShape) -> None:
36-
self._pptx: GenericPptxBaseShape = pptx_shape
41+
def __eq__(self, other: object) -> bool:
42+
if not isinstance(other, BaseShape):
43+
return False
44+
return self._pptx is other._pptx
45+
46+
def __ne__(self, other: object) -> bool:
47+
if not isinstance(other, BaseShape):
48+
return True
49+
return self._pptx is not other._pptx
50+
51+
@property
52+
def click_action(self) -> "ActionSetting":
53+
from tppt.pptx.action import ActionSetting
54+
55+
return ActionSetting(self._pptx.click_action)
56+
57+
@property
58+
def element(self) -> "PptxShapeElement":
59+
return self._pptx.element
60+
61+
@property
62+
def height(self) -> Length:
63+
return to_length(self._pptx.height)
64+
65+
@height.setter
66+
def height(self, value: Length | LiteralLength | PptxLength) -> None:
67+
self._pptx.height = to_pptx_length(value)
68+
69+
@property
70+
def left(self) -> Length:
71+
return to_length(self._pptx.left)
72+
73+
@left.setter
74+
def left(self, value: Length | LiteralLength | PptxLength) -> None:
75+
self._pptx.left = to_pptx_length(value)
76+
77+
@property
78+
def name(self) -> str:
79+
return self._pptx.name
80+
81+
@name.setter
82+
def name(self, value: str) -> None:
83+
self._pptx.name = value
84+
85+
@property
86+
def part(self) -> "PptxBaseSlidePart":
87+
return self._pptx.part
88+
89+
@property
90+
def placeholder_format(self) -> "PptxPlaceholderFormat":
91+
return self._pptx.placeholder_format
92+
93+
@property
94+
def rotation(self) -> float:
95+
return self._pptx.rotation
96+
97+
@rotation.setter
98+
def rotation(self, value: float) -> None:
99+
self._pptx.rotation = value
100+
101+
@property
102+
def shadow(self) -> "ShadowFormat":
103+
from tppt.pptx.dml.effect import ShadowFormat
104+
105+
return ShadowFormat(self._pptx.shadow)
106+
107+
@property
108+
def shape_id(self) -> int:
109+
return self._pptx.shape_id
110+
111+
@property
112+
def shape_type(self) -> "MSO_SHAPE_TYPE":
113+
return self._pptx.shape_type
114+
115+
@property
116+
def top(self) -> Length:
117+
return to_length(self._pptx.top)
37118

38-
def to_pptx(self) -> GenericPptxBaseShape:
39-
return self._pptx
119+
@top.setter
120+
def top(self, value: Length | LiteralLength | PptxLength) -> None:
121+
self._pptx.top = to_pptx_length(value)
122+
123+
@property
124+
def width(self) -> Length:
125+
return to_length(self._pptx.width)
40126

41-
@classmethod
42-
def from_pptx(cls, pptx_obj: GenericPptxBaseShape) -> Self:
43-
return cls(pptx_obj)
127+
@width.setter
128+
def width(self, value: Length | LiteralLength | PptxLength) -> None:
129+
self._pptx.width = to_pptx_length(value)
44130

45131

46132
class Shape(BaseShape[GenericPptxShape]):
@@ -49,7 +135,7 @@ def adjustments(self) -> list[float]:
49135
return [self._pptx.adjustments[i] for i in range(len(self._pptx.adjustments))]
50136

51137
@property
52-
def auto_shape_type(self) -> MSO_AUTO_SHAPE_TYPE | None:
138+
def auto_shape_type(self) -> "MSO_AUTO_SHAPE_TYPE | None":
53139
return self._pptx.auto_shape_type
54140

55141
@property
@@ -83,19 +169,7 @@ def text_frame(self) -> "TextFrame":
83169

84170

85171
class SubShape(PptxConvertible[_GenericPptxSubshape]):
86-
def __init__(self, pptx_shape: _GenericPptxSubshape) -> None:
87-
self._pptx: _GenericPptxSubshape = pptx_shape
88-
89-
@property
90-
def part(self) -> XmlPart:
91-
return self._pptx.part
92-
93-
def to_pptx(self) -> _GenericPptxSubshape:
94-
return self._pptx
95-
96-
@classmethod
97-
def from_pptx(cls, pptx_obj: _GenericPptxSubshape) -> Self:
98-
return cls(pptx_obj)
172+
pass
99173

100174

101175
class RangeProps(TypedDict):

src/tppt/pptx/shape/background.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
from typing import Self
2-
31
from pptx.slide import _Background as PptxBackground
42

53
from tppt.pptx.converter import PptxConvertible
64

75

86
class Background(PptxConvertible[PptxBackground]):
97
"""Background of the slide."""
10-
11-
def __init__(self, pptx_obj: PptxBackground) -> None:
12-
self._pptx = pptx_obj
13-
14-
def to_pptx(self) -> PptxBackground:
15-
return self._pptx
16-
17-
@classmethod
18-
def from_pptx(cls, pptx_obj: PptxBackground) -> Self:
19-
return cls(pptx_obj)

src/tppt/pptx/shape/picture.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import IO, Literal, NotRequired, Self, assert_never
1+
from typing import IO, Literal, NotRequired, assert_never
22

33
from pptx.opc.constants import CONTENT_TYPE
44
from pptx.shapes.picture import Movie as PptxMovie
@@ -32,15 +32,6 @@ def __init__(
3232
) -> None:
3333
self._pptx = pptx_obj
3434

35-
def to_pptx(self) -> PptxPicture:
36-
"""Convert to pptx shape."""
37-
return self._pptx
38-
39-
@classmethod
40-
def from_pptx(cls, pptx_obj: PptxPicture) -> Self:
41-
"""Create from pptx shape."""
42-
return cls(pptx_obj)
43-
4435

4536
MOVIE_MIME_TYPE = Literal[
4637
"video/x-ms-asf",
@@ -108,12 +99,3 @@ def __init__(
10899
/,
109100
) -> None:
110101
self._pptx = pptx_obj
111-
112-
def to_pptx(self) -> PptxMovie:
113-
"""Convert to pptx shape."""
114-
return self._pptx
115-
116-
@classmethod
117-
def from_pptx(cls, pptx_obj: PptxMovie) -> Self:
118-
"""Create from pptx shape."""
119-
return cls(pptx_obj)

src/tppt/pptx/shape/placeholder.py

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,13 @@
22
from pptx.shapes.placeholder import MasterPlaceholder as _PptxMasterPlaceholder
33
from pptx.shapes.placeholder import SlidePlaceholder as _PptxSlidePlaceholder
44

5-
from tppt.pptx.converter import to_pptx_length, to_tppt_length
6-
from tppt.pptx.presentation import PptxConvertible
7-
from tppt.types._length import Length
8-
95
from . import Shape
106

117

12-
class LayoutPlaceholder(PptxConvertible[_PptxLayoutPlaceholder]): ...
13-
14-
15-
class MasterPlaceholder(PptxConvertible[_PptxMasterPlaceholder]): ...
16-
17-
18-
class SlidePlaceholder(Shape[_PptxSlidePlaceholder]):
19-
@property
20-
def left(self) -> Length | None:
21-
return to_tppt_length(self._pptx.left)
22-
23-
@left.setter
24-
def left(self, value: Length | None) -> None:
25-
self._pptx.left = to_pptx_length(value)
26-
27-
@property
28-
def top(self) -> Length | None:
29-
return to_tppt_length(self._pptx.top)
30-
31-
@top.setter
32-
def top(self, value: Length | None) -> None:
33-
self._pptx.top = to_pptx_length(value)
8+
class LayoutPlaceholder(Shape[_PptxLayoutPlaceholder]): ...
349

35-
@property
36-
def height(self) -> Length | None:
37-
return to_tppt_length(self._pptx.height)
3810

39-
@height.setter
40-
def height(self, value: Length | None) -> None:
41-
self._pptx.height = to_pptx_length(value)
11+
class MasterPlaceholder(Shape[_PptxMasterPlaceholder]): ...
4212

43-
@property
44-
def width(self) -> Length | None:
45-
return to_tppt_length(self._pptx.width)
4613

47-
@width.setter
48-
def width(self, value: Length | None) -> None:
49-
self._pptx.width = to_pptx_length(value)
14+
class SlidePlaceholder(Shape[_PptxSlidePlaceholder]): ...

src/tppt/pptx/shape/text.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, NotRequired, Self
1+
from typing import Literal, NotRequired
22

33
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE, PP_ALIGN
44
from pptx.shapes.autoshape import Shape as PptxShape
@@ -72,12 +72,3 @@ def __init__(self, pptx_obj: PptxShape, data: TextData | None = None, /) -> None
7272
@property
7373
def text_frame(self) -> TextFrame:
7474
return TextFrame(self._pptx.text_frame)
75-
76-
def to_pptx(self) -> PptxShape:
77-
"""Convert to pptx shape."""
78-
return self._pptx
79-
80-
@classmethod
81-
def from_pptx(cls, pptx_obj: PptxShape) -> Self:
82-
"""Create from pptx shape."""
83-
return cls(pptx_obj)

0 commit comments

Comments
 (0)