Skip to content

Commit 70dea2b

Browse files
committed
refactor: remove builder classes from text-related modules to simplify API
1 parent 1f16d80 commit 70dea2b

File tree

9 files changed

+16
-206
lines changed

9 files changed

+16
-206
lines changed

examples/formatted_text.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,6 @@ def formatted_text(text: tppt.pptx.shape.text.Text) -> tppt.pptx.shape.text.Text
3333
width=(400, "pt"),
3434
height=(50, "pt"),
3535
)
36-
.text(
37-
# Builder pattern.
38-
lambda text: text.builder().text_frame(
39-
lambda text_frame: text_frame.builder().paragraph(
40-
lambda paragraph: paragraph.builder().run(
41-
lambda run: run.builder()
42-
.text("Hello, world!")
43-
.font(
44-
lambda font: font.builder()
45-
.color(lambda color: color.builder().rgb("#00FF00"))
46-
.italic(True)
47-
)
48-
)
49-
),
50-
),
51-
left=(50, "pt"),
52-
top=(200, "pt"),
53-
width=(400, "pt"),
54-
height=(50, "pt"),
55-
)
5636
)
5737
.build()
5838
)

src/tppt/pptx/dml/color.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,9 @@ def rgb(self) -> Color | None:
1818
def rgb(self, color: Color | LiteralColor):
1919
self._pptx.rgb = to_pptx_rgb_color(color)
2020

21-
def builder(self) -> "ColorFormatBuilder":
22-
return ColorFormatBuilder(self._pptx)
23-
2421
def to_pptx(self) -> PptxColorFormat:
2522
return self._pptx
2623

2724
@classmethod
2825
def from_pptx(cls, pptx_obj: PptxColorFormat) -> Self:
2926
return cls(pptx_obj)
30-
31-
32-
class ColorFormatBuilder:
33-
def __init__(self, pptx_obj: PptxColorFormat) -> None:
34-
self._pptx = pptx_obj
35-
36-
def rgb(self, color: Color | LiteralColor) -> Self:
37-
self._pptx.rgb = to_pptx_rgb_color(color)
38-
39-
return self
40-
41-
def _build(self) -> ColorFormat:
42-
return ColorFormat(self._pptx)

src/tppt/pptx/shape/text.py

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

33
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE, PP_ALIGN
44
from pptx.shapes.autoshape import Shape as PptxShape
55

66
from tppt.pptx.converter import to_pptx_length, to_pptx_rgb_color
7-
from tppt.pptx.text.text_frame import TextFrame, TextFrameBuilder
7+
from tppt.pptx.text.text_frame import TextFrame
88
from tppt.types._color import Color, LiteralColor
99
from tppt.types._length import Length, LiteralLength
1010

@@ -73,9 +73,6 @@ def __init__(self, pptx_obj: PptxShape, data: TextData | None = None, /) -> None
7373
def text_frame(self) -> TextFrame:
7474
return TextFrame(self._pptx.text_frame)
7575

76-
def builder(self) -> "TextBuilder":
77-
return TextBuilder(self._pptx)
78-
7976
def to_pptx(self) -> PptxShape:
8077
"""Convert to pptx shape."""
8178
return self._pptx
@@ -84,20 +81,3 @@ def to_pptx(self) -> PptxShape:
8481
def from_pptx(cls, pptx_obj: PptxShape) -> Self:
8582
"""Create from pptx shape."""
8683
return cls(pptx_obj)
87-
88-
89-
class TextBuilder:
90-
def __init__(self, pptx_obj: PptxShape) -> None:
91-
self._pptx = pptx_obj
92-
93-
def text_frame(
94-
self, callable: Callable[[TextFrame], TextFrame | TextFrameBuilder]
95-
) -> Self:
96-
text_frame = callable(TextFrame(self._pptx.text_frame))
97-
if isinstance(text_frame, TextFrameBuilder):
98-
text_frame._build()
99-
100-
return self
101-
102-
def _build(self) -> Text:
103-
return Text(self._pptx)

src/tppt/pptx/slide.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .shape import RangeProps, Shape
1313
from .shape.picture import Picture, PictureData, PictureProps
1414
from .shape.table import DataFrame, Table, TableData, TableProps, dataframe2list
15-
from .shape.text import Text, TextBuilder, TextData, TextProps
15+
from .shape.text import Text, TextData, TextProps
1616
from .slide_layout import SlideLayout
1717
from .snotes_slide import NotesSlide
1818

@@ -96,12 +96,12 @@ def text(self, text: str, **kwargs: Unpack[TextProps]) -> Self: ...
9696

9797
@overload
9898
def text(
99-
self, text: Callable[[Text], Text | TextBuilder], **kwargs: Unpack[RangeProps]
99+
self, text: Callable[[Text], Text], **kwargs: Unpack[RangeProps]
100100
) -> Self: ...
101101

102102
def text(
103103
self,
104-
text: str | Callable[[Text], Text | TextBuilder],
104+
text: str | Callable[[Text], Text],
105105
**kwargs: Unpack[TextProps],
106106
) -> Self:
107107
def _register(slide: Slide) -> Text:
@@ -124,10 +124,7 @@ def _register(slide: Slide) -> Text:
124124
data,
125125
)
126126
if isinstance(text, Callable):
127-
text_builder = text(text_obj)
128-
if isinstance(text_builder, TextBuilder):
129-
text_builder = text_builder._build()
130-
return text_builder
127+
return text(text_obj)
131128
else:
132129
return text_obj
133130

src/tppt/pptx/text/font.py

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from typing import Callable, Self
1+
from typing import Self
22

33
from pptx.enum.text import MSO_TEXT_UNDERLINE_TYPE
44
from pptx.text.text import Font as PptxFont
55

66
from tppt.pptx.converter import PptxConvertible, to_pptx_length, to_tppt_length
7-
from tppt.pptx.dml.color import ColorFormat, ColorFormatBuilder
8-
from tppt.types._length import Length, LiteralLength
7+
from tppt.pptx.dml.color import ColorFormat
8+
from tppt.types._length import Length
99

1010

1111
class Font(PptxConvertible[PptxFont]):
@@ -60,54 +60,9 @@ def color(self) -> ColorFormat:
6060
def color(self, color: ColorFormat) -> None:
6161
self._pptx.color = color.to_pptx()
6262

63-
def builder(self) -> "FontBuilder":
64-
return FontBuilder(self._pptx)
65-
6663
def to_pptx(self) -> PptxFont:
6764
return self._pptx
6865

6966
@classmethod
7067
def from_pptx(cls, pptx_obj: PptxFont) -> Self:
7168
return cls(pptx_obj)
72-
73-
74-
class FontBuilder:
75-
def __init__(self, pptx_obj: PptxFont) -> None:
76-
self._pptx = pptx_obj
77-
78-
def name(self, name: str) -> Self:
79-
self._pptx.name = name
80-
81-
return self
82-
83-
def size(self, size: Length | LiteralLength) -> Self:
84-
self._pptx.size = to_pptx_length(size)
85-
86-
return self
87-
88-
def bold(self, bold: bool) -> Self:
89-
self._pptx.bold = bold
90-
91-
return self
92-
93-
def italic(self, italic: bool) -> Self:
94-
self._pptx.italic = italic
95-
96-
return self
97-
98-
def underline(self, underline: bool | MSO_TEXT_UNDERLINE_TYPE) -> Self:
99-
self._pptx.underline = underline
100-
101-
return self
102-
103-
def color(
104-
self, callable: Callable[[ColorFormat], ColorFormat | ColorFormatBuilder]
105-
) -> Self:
106-
color = callable(ColorFormat(self._pptx.color))
107-
if isinstance(color, ColorFormatBuilder):
108-
color._build()
109-
110-
return self
111-
112-
def _build(self) -> Font:
113-
return Font(self._pptx)

src/tppt/pptx/text/hyperlink.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,9 @@ def address(self) -> str | None:
1717
def address(self, address: str | None) -> None:
1818
self._pptx.address = address
1919

20-
def builder(self) -> "HyperlinkBuilder":
21-
return HyperlinkBuilder(self._pptx)
22-
2320
def to_pptx(self) -> PptxHyperlink:
2421
return self._pptx
2522

2623
@classmethod
2724
def from_pptx(cls, pptx_obj: PptxHyperlink) -> Self:
2825
return cls(pptx_obj)
29-
30-
31-
class HyperlinkBuilder:
32-
def __init__(self, pptx_obj: PptxHyperlink) -> None:
33-
self._pptx = pptx_obj
34-
35-
def _build(self) -> Hyperlink:
36-
return Hyperlink(self._pptx)

src/tppt/pptx/text/paragraph.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import Callable, Self
1+
from typing import Self
22

33
from pptx.text.text import _Paragraph as PptxParagraph
44

55
from tppt.pptx.converter import PptxConvertible
6-
from tppt.pptx.text.run import Run, RunBuilder
6+
from tppt.pptx.text.run import Run
77

88

99
class Paragraph(PptxConvertible[PptxParagraph]):
@@ -17,32 +17,9 @@ def runs(self) -> list[Run]:
1717
def add_run(self) -> Run:
1818
return Run(self._pptx.add_run())
1919

20-
def builder(self) -> "ParagraphBuilder":
21-
return ParagraphBuilder(self._pptx)
22-
2320
def to_pptx(self) -> PptxParagraph:
2421
return self._pptx
2522

2623
@classmethod
2724
def from_pptx(cls, pptx_obj: PptxParagraph) -> Self:
2825
return cls(pptx_obj)
29-
30-
31-
class ParagraphBuilder:
32-
def __init__(self, pptx_obj: PptxParagraph) -> None:
33-
self._pptx = pptx_obj
34-
35-
def text(self, text: str) -> "ParagraphBuilder":
36-
self._pptx.text = text
37-
38-
return self
39-
40-
def run(self, callable: Callable[[Run], Run | RunBuilder]) -> "ParagraphBuilder":
41-
run = callable(Run(self._pptx.add_run()))
42-
if isinstance(run, RunBuilder):
43-
run._build()
44-
45-
return self
46-
47-
def _build(self) -> Paragraph:
48-
return Paragraph(self._pptx)

src/tppt/pptx/text/run.py

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from typing import Callable, Self
1+
from typing import Self
22

33
from pptx.text.text import _Run as PptxRun
44

55
from tppt.pptx.converter import PptxConvertible
6-
from tppt.pptx.text.font import Font, FontBuilder
7-
from tppt.pptx.text.hyperlink import Hyperlink, HyperlinkBuilder
6+
from tppt.pptx.text.font import Font
7+
from tppt.pptx.text.hyperlink import Hyperlink
88

99

1010
class Run(PptxConvertible[PptxRun]):
@@ -27,41 +27,9 @@ def font(self) -> Font:
2727
def hyperlink(self) -> Hyperlink:
2828
return Hyperlink(self._pptx.hyperlink)
2929

30-
def builder(self) -> "RunBuilder":
31-
return RunBuilder(self._pptx)
32-
3330
def to_pptx(self) -> PptxRun:
3431
return self._pptx
3532

3633
@classmethod
3734
def from_pptx(cls, pptx_obj: PptxRun) -> Self:
3835
return cls(pptx_obj)
39-
40-
41-
class RunBuilder:
42-
def __init__(self, pptx_obj: PptxRun) -> None:
43-
self._pptx = pptx_obj
44-
45-
def text(self, text: str) -> Self:
46-
self._pptx.text = text
47-
48-
return self
49-
50-
def font(self, callable: Callable[[Font], Font | FontBuilder]) -> Self:
51-
font = callable(Font(self._pptx.font))
52-
if isinstance(font, FontBuilder):
53-
font._build()
54-
55-
return self
56-
57-
def hyperlink(
58-
self, callable: Callable[[Hyperlink], Hyperlink | HyperlinkBuilder]
59-
) -> Self:
60-
hyperlink = callable(Hyperlink(self._pptx.hyperlink))
61-
if isinstance(hyperlink, HyperlinkBuilder):
62-
hyperlink._build()
63-
64-
return self
65-
66-
def _build(self) -> Run:
67-
return Run(self._pptx)

src/tppt/pptx/text/text_frame.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import Callable, Self
1+
from typing import Self
22

33
from pptx.text.text import TextFrame as PptxTextFrame
44

55
from tppt.pptx.converter import PptxConvertible
6-
from tppt.pptx.text.paragraph import Paragraph, ParagraphBuilder
6+
from tppt.pptx.text.paragraph import Paragraph
77

88

99
class TextFrame(PptxConvertible[PptxTextFrame]):
@@ -13,29 +13,9 @@ def __init__(self, pptx_obj: PptxTextFrame) -> None:
1313
def add_paragraph(self) -> Paragraph:
1414
return Paragraph(self._pptx.add_paragraph())
1515

16-
def builder(self) -> "TextFrameBuilder":
17-
return TextFrameBuilder(self._pptx)
18-
1916
def to_pptx(self) -> PptxTextFrame:
2017
return self._pptx
2118

2219
@classmethod
2320
def from_pptx(cls, pptx_obj: PptxTextFrame) -> Self:
2421
return cls(pptx_obj)
25-
26-
27-
class TextFrameBuilder:
28-
def __init__(self, pptx_obj: PptxTextFrame) -> None:
29-
self._pptx = pptx_obj
30-
31-
def paragraph(
32-
self, callable: Callable[[Paragraph], Paragraph | ParagraphBuilder]
33-
) -> "TextFrameBuilder":
34-
paragraph = callable(Paragraph(self._pptx.add_paragraph()))
35-
if isinstance(paragraph, ParagraphBuilder):
36-
paragraph._build()
37-
38-
return self
39-
40-
def _build(self) -> TextFrame:
41-
return TextFrame(self._pptx)

0 commit comments

Comments
 (0)