Skip to content

Commit 850ea88

Browse files
author
Steve Canny
committed
rfctr: consolidate shapes.factory into .shapetree
1 parent 17abb3a commit 850ea88

9 files changed

Lines changed: 397 additions & 435 deletions

File tree

‎docs/api/slides.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ return value of :meth:`add_slide`.
4848
|SlidePlaceholders| objects
4949
---------------------------
5050

51-
.. autoclass:: pptx.shapes.factory.SlidePlaceholders
51+
.. autoclass:: pptx.shapes.shapetree.SlidePlaceholders
5252
:members:

‎docs/dev/analysis/features/placeholders/slide-placeholders/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Candidate protocol
2222
<pptx.shapes.placeholder.ShapePlaceholder object at 0x104e60020>
2323
>>> slide_placeholders = slide.placeholders
2424
>>> slide_placeholders
25-
<pptx.shapes.factory.SlidePlaceholders object at 0x104e60040>
25+
<pptx.shapes.shapetree.SlidePlaceholders object at 0x104e60040>
2626
>>> len(slide_placeholders)
2727
2
2828
>>> slide_placeholders[1]

‎pptx/shapes/factory.py‎

Lines changed: 0 additions & 103 deletions
This file was deleted.

‎pptx/shapes/shapetree.py‎

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,36 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11-
from .autoshape import AutoShapeType
11+
from .autoshape import AutoShapeType, Shape
12+
from .base import BaseShape
1213
from ..enum.shapes import PP_PLACEHOLDER
13-
from .factory import BaseShapeFactory, SlideShapeFactory
14+
from .graphfrm import GraphicFrame
1415
from ..oxml.ns import qn
1516
from ..oxml.shapes.graphfrm import CT_GraphicalObjectFrame
1617
from ..oxml.simpletypes import ST_Direction
17-
from .placeholder import LayoutPlaceholder, MasterPlaceholder
18+
from .picture import Picture
19+
from .placeholder import (
20+
ChartPlaceholder, LayoutPlaceholder, MasterPlaceholder,
21+
PicturePlaceholder, PlaceholderGraphicFrame, PlaceholderPicture,
22+
SlidePlaceholder, TablePlaceholder
23+
)
1824
from ..shared import ParentedElementProxy
1925

2026

27+
def BaseShapeFactory(shape_elm, parent):
28+
"""
29+
Return an instance of the appropriate shape proxy class for *shape_elm*.
30+
"""
31+
tag_name = shape_elm.tag
32+
if tag_name == qn('p:sp'):
33+
return Shape(shape_elm, parent)
34+
if tag_name == qn('p:pic'):
35+
return Picture(shape_elm, parent)
36+
if tag_name == qn('p:graphicFrame'):
37+
return GraphicFrame(shape_elm, parent)
38+
return BaseShape(shape_elm, parent)
39+
40+
2141
class _BaseShapes(ParentedElementProxy):
2242
"""
2343
Base class for a shape collection appearing in a slide-type object,
@@ -203,6 +223,74 @@ def _MasterShapeFactory(shape_elm, parent):
203223
return BaseShapeFactory(shape_elm, parent)
204224

205225

226+
def _SlidePlaceholderFactory(shape_elm, parent):
227+
"""
228+
Return a placeholder shape of the appropriate type for *shape_elm*.
229+
"""
230+
tag = shape_elm.tag
231+
if tag == qn('p:sp'):
232+
Constructor = {
233+
PP_PLACEHOLDER.BITMAP: PicturePlaceholder,
234+
PP_PLACEHOLDER.CHART: ChartPlaceholder,
235+
PP_PLACEHOLDER.PICTURE: PicturePlaceholder,
236+
PP_PLACEHOLDER.TABLE: TablePlaceholder,
237+
}.get(shape_elm.ph_type, SlidePlaceholder)
238+
elif tag == qn('p:graphicFrame'):
239+
Constructor = PlaceholderGraphicFrame
240+
elif tag == qn('p:pic'):
241+
Constructor = PlaceholderPicture
242+
else:
243+
Constructor = BaseShapeFactory
244+
return Constructor(shape_elm, parent)
245+
246+
247+
class SlidePlaceholders(ParentedElementProxy):
248+
"""
249+
Collection of placeholder shapes on a slide. Supports iteration,
250+
:func:`len`, and dictionary-style lookup on the `idx` value of the
251+
placeholders it contains.
252+
"""
253+
254+
__slots__ = ()
255+
256+
def __getitem__(self, idx):
257+
"""
258+
Access placeholder shape having *idx*. Note that while this looks
259+
like list access, idx is actually a dictionary key and will raise
260+
|KeyError| if no placeholder with that idx value is in the
261+
collection.
262+
"""
263+
for e in self._element.iter_ph_elms():
264+
if e.ph_idx == idx:
265+
return SlideShapeFactory(e, self)
266+
raise KeyError('no placeholder on this slide with idx == %d' % idx)
267+
268+
def __iter__(self):
269+
"""
270+
Generate placeholder shapes in `idx` order.
271+
"""
272+
ph_elms = sorted(
273+
[e for e in self._element.iter_ph_elms()], key=lambda e: e.ph_idx
274+
)
275+
return (SlideShapeFactory(e, self) for e in ph_elms)
276+
277+
def __len__(self):
278+
"""
279+
Return count of placeholder shapes.
280+
"""
281+
return len(list(self._element.iter_ph_elms()))
282+
283+
284+
def SlideShapeFactory(shape_elm, parent):
285+
"""
286+
Return an instance of the appropriate shape proxy class for *shape_elm*
287+
on a slide.
288+
"""
289+
if shape_elm.has_ph_elm:
290+
return _SlidePlaceholderFactory(shape_elm, parent)
291+
return BaseShapeFactory(shape_elm, parent)
292+
293+
206294
class SlideShapes(_BaseShapes):
207295
"""
208296
Sequence of shapes appearing on a slide. The first shape in the sequence

‎pptx/slide.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
)
1010

1111
from .enum.shapes import PP_PLACEHOLDER
12-
from .shapes.factory import SlidePlaceholders
1312
from .shapes.shapetree import (
1413
LayoutPlaceholders, LayoutShapes, MasterPlaceholders, MasterShapes,
15-
SlideShapes
14+
SlidePlaceholders, SlideShapes
1615
)
1716
from .shared import ParentedElementProxy, PartElementProxy
1817
from .util import lazyproperty

‎tests/shapes/test_base.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
from pptx.shapes import Subshape
1616
from pptx.shapes.autoshape import Shape
1717
from pptx.shapes.base import BaseShape, _PlaceholderFormat
18-
from pptx.shapes.factory import BaseShapeFactory
1918
from pptx.shapes.graphfrm import GraphicFrame
2019
from pptx.shapes.picture import Picture
21-
from pptx.shapes.shapetree import SlideShapes
20+
from pptx.shapes.shapetree import BaseShapeFactory, SlideShapes
2221

2322
from ..oxml.unitdata.shape import (
2423
a_cNvPr, a_cxnSp, a_graphicFrame, a_grpSp, a_grpSpPr, a_p_xfrm, a_pic,

0 commit comments

Comments
 (0)