Skip to content

Commit 8977071

Browse files
authored
Merge pull request #264 from e10harvey/issue263
Fixes PowerpointShape._pptx_inches
2 parents 273e5d3 + 95c106e commit 8977071

File tree

5 files changed

+227
-9
lines changed

5 files changed

+227
-9
lines changed

opencsp/common/lib/render/lib/PowerpointImage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ def dims_pptx(self):
249249
The dimensions of the image in inches.
250250
"""
251251
# ChatGPT 4o-mini assisted with generating this doc string
252+
assert self.has_dims()
252253
return self._pptx_inches(self.dims)
253254

254255
@staticmethod

opencsp/common/lib/render/lib/PowerpointShape.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _pptx_inches(self, vals: int | float | Iterable):
4444
# Parameters
4545
# ----------
4646
# vals : int, float, or Iterable
47-
# The value(s) to convert to inches. Can be a single value or an iterable of values.
47+
# The value(s) to convert to inches. Can be a single value or an iterable of values. Iterable will be a list
4848
#
4949
# Returns
5050
# -------
@@ -55,13 +55,22 @@ def _pptx_inches(self, vals: int | float | Iterable):
5555
# -----
5656
# If a single value is provided, it returns a single integer. If an iterable is provided, it returns a list of integers.
5757
# "ChatGPT 4o" assisted with generating this doc
58-
try:
59-
ret = []
60-
for val in vals:
61-
ret.append(pptx.util.Inches(val))
62-
return ret
63-
except:
64-
return pptx.util.Inches(val)
58+
if isinstance(vals, int) or isinstance(vals, float):
59+
return pptx.util.Inches(vals)
60+
elif isinstance(vals, list) or isinstance(vals, tuple):
61+
if all(isinstance(x, (int, float)) for x in vals):
62+
ret = []
63+
for val in vals:
64+
ret.append(pptx.util.Inches(val))
65+
return ret
66+
else:
67+
for x in vals:
68+
if not isinstance(x, (int, float)):
69+
print(f"Element '{x}' is of type '{type(x).__name__}'")
70+
raise TypeError(f"\"vals\" must contain only int or float values.")
71+
else:
72+
vals_type = type(vals).__name__
73+
raise TypeError(f"\"vals\" of type \"{vals_type}\" is not supported.")
6574

6675
def cell_dims_pptx(self):
6776
"""Returns the PowerPoint-style inches that bound this shape (left, top, width, height).

opencsp/common/lib/render/lib/PowerpointText.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class PowerpointText(pps.PowerpointShape):
3030
def __init__(
3131
self,
3232
val: str = None,
33-
dims: tuple[float, float, float, float] = None,
33+
dims: tuple[float, float, float, float] = (1.0, 1.0, 1.0, 1.0),
3434
cell_dims: tuple[float, float, float, float] = None,
3535
is_title=False,
3636
parent_slide=None,
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import inspect
2+
import pptx
3+
import unittest
4+
from typing import Iterable, overload
5+
from opencsp.common.lib.render.lib.PowerpointShape import PowerpointShape
6+
7+
8+
class test_PowerpointShape(unittest.TestCase):
9+
# Test of a single integer value
10+
def test_pptx_inches_single_integer(self):
11+
ppt_shape_instance = PowerpointShape()
12+
result = ppt_shape_instance._pptx_inches(vals=3)
13+
assert result == pptx.util.Inches(3)
14+
15+
# Test of a single float value
16+
def test_pptx_inches_single_float(self):
17+
ppt_shape_instance = PowerpointShape()
18+
result = ppt_shape_instance._pptx_inches(vals=6.0)
19+
assert result == pptx.util.Inches(6.0)
20+
21+
# Test for a None value
22+
def test_pptx_inches_none(self):
23+
ppt_shape_instance = PowerpointShape()
24+
with self.assertRaises(TypeError):
25+
ppt_shape_instance._pptx_inches(None)
26+
27+
# Test for a list of integer values
28+
def test_pptx_inches_list_of_integers(self):
29+
ppt_shape_instance = PowerpointShape()
30+
result = ppt_shape_instance._pptx_inches(vals=[1, 2, 3, 4, 5])
31+
expected = [
32+
pptx.util.Inches(1),
33+
pptx.util.Inches(2),
34+
pptx.util.Inches(3),
35+
pptx.util.Inches(4),
36+
pptx.util.Inches(5),
37+
]
38+
assert expected == result
39+
40+
# Test for a list of float values
41+
def test_pptx_inches_list_of_floats(self):
42+
ppt_shape_instance = PowerpointShape()
43+
result = ppt_shape_instance._pptx_inches(vals=[1.0, 1.1, 1.2, 1.3, 1.4])
44+
expected = [
45+
pptx.util.Inches(1.0),
46+
pptx.util.Inches(1.1),
47+
pptx.util.Inches(1.2),
48+
pptx.util.Inches(1.3),
49+
pptx.util.Inches(1.4),
50+
]
51+
assert expected == result
52+
53+
# Test for a tuple of integers
54+
def test_pptx_inches_tuple_of_integers(self):
55+
ppt_shape_instance = PowerpointShape()
56+
result = ppt_shape_instance._pptx_inches(vals=(1, 2, 3, 4, 5))
57+
expected = [
58+
pptx.util.Inches(1),
59+
pptx.util.Inches(2),
60+
pptx.util.Inches(3),
61+
pptx.util.Inches(4),
62+
pptx.util.Inches(5),
63+
]
64+
assert expected == result
65+
66+
# Test for a tuple of floats
67+
def test_pptx_inches_tuple_of_floats(self):
68+
ppt_shape_instance = PowerpointShape()
69+
result = ppt_shape_instance._pptx_inches(vals=(1.0, 1.1, 1.2, 1.3, 1.4))
70+
expected = [
71+
pptx.util.Inches(1.0),
72+
pptx.util.Inches(1.1),
73+
pptx.util.Inches(1.2),
74+
pptx.util.Inches(1.3),
75+
pptx.util.Inches(1.4),
76+
]
77+
assert expected == result
78+
79+
# Test for a tuple of integers
80+
def test_pptx_inches_tuple_of_integers(self):
81+
ppt_shape_instance = PowerpointShape()
82+
with self.assertRaises(TypeError):
83+
ppt_shape_instance._pptx_inches(vals=(1, 2, "random string", 4, 5))
84+
85+
# Test for an empty list
86+
def test_pptx_inches_empty_list(self):
87+
ppt_shape_instance = PowerpointShape()
88+
result = ppt_shape_instance._pptx_inches(vals=[])
89+
expected = []
90+
assert expected == result
91+
92+
# Test for a set, not supported
93+
def test_unsupported_type_set(self):
94+
ppt_shape_instance = PowerpointShape()
95+
with self.assertRaises(TypeError):
96+
ppt_shape_instance._pptx_inches({1, 2, 3})
97+
98+
# Test for a string, not supported
99+
def test_bad_string(self):
100+
ppt_shape_instance = PowerpointShape()
101+
with self.assertRaises(TypeError):
102+
ppt_shape_instance._pptx_inches("some string")
103+
104+
# Test for a dictionary, not supported
105+
def test_unsupported_type_dictionary(self):
106+
ppt_shape_instance = PowerpointShape()
107+
with self.assertRaises(TypeError):
108+
ppt_shape_instance._pptx_inches({1: 3, 2: 4, 3: 5})
109+
110+
# Test for a list of strings, not supported
111+
def test_unsupported_type_list_of_strings(self):
112+
ppt_shape_instance = PowerpointShape()
113+
with self.assertRaises(TypeError):
114+
ppt_shape_instance._pptx_inches(["unit", "test", "for", "string", "list"])
115+
116+
# Test for a None value
117+
def test_dims_to_str_with_none(self):
118+
ppt_shape_instance = PowerpointShape()
119+
result = ppt_shape_instance._dims_to_str(dims=None)
120+
expected = None
121+
assert expected == result
122+
123+
# Test converting tuple to string (csv)
124+
def test_dims_to_str_with_tuple(self):
125+
ppt_shape_instance = PowerpointShape()
126+
result = ppt_shape_instance._dims_to_str(dims=(5.1, 4.2, 3.3, 2.4, 1.5))
127+
expected = "5.1,4.2,3.3,2.4,1.5"
128+
assert expected == result
129+
130+
# Test converting a csv string to a tuple of floats
131+
def test_str_to_dims_with_tuple(self):
132+
ppt_shape_instance = PowerpointShape()
133+
result = ppt_shape_instance._str_to_dims(sval="1.1,2.2,3.3,4.4,5.5")
134+
expected = (1.1, 2.2, 3.3, 4.4, 5.5)
135+
assert expected == result
136+
137+
# Test with sval equal to None
138+
def test_str_to_dims_with_none(self):
139+
ppt_shape_instance = PowerpointShape()
140+
result = ppt_shape_instance._str_to_dims(sval=None)
141+
expected = None
142+
assert expected == result
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os, unittest
2+
3+
import opencsp.common.lib.opencsp_path.opencsp_root_path as orp
4+
import opencsp.common.lib.render.lib.PowerpointShape as pps
5+
import opencsp.common.lib.tool.file_tools as ft
6+
import opencsp.common.lib.tool.log_tools as lt
7+
from opencsp.common.lib.render.lib.PowerpointText import PowerpointText
8+
9+
10+
class test_PowerpointText(unittest.TestCase):
11+
def test_has_val_with_content(self):
12+
ppt_text_instance = PowerpointText(val="example text written for pytest")
13+
assert ppt_text_instance.has_val() == True
14+
15+
def test_has_val_with_no_content(self):
16+
ppt_text_instance = PowerpointText(val=None)
17+
assert ppt_text_instance.has_val() == False
18+
19+
def test_has_val_with_empty_string(self):
20+
ppt_text_instance = PowerpointText(val="")
21+
assert ppt_text_instance.has_val() == True
22+
23+
def test_get_val_with_content(self):
24+
ppt_text_instance = PowerpointText(val="test get val")
25+
assert ppt_text_instance.get_val() == "test get val"
26+
27+
def test_get_val_with_no_content(self):
28+
ppt_text_instance = PowerpointText(val=None)
29+
assert ppt_text_instance.get_val() == None
30+
31+
def test_set_val(self):
32+
val = "test_set_val test"
33+
ppt_text_instance = PowerpointText(val)
34+
ppt_text_instance.set_val(val)
35+
assert ppt_text_instance._val == val
36+
37+
def test_set_val_none(self):
38+
val = "test_set_val test"
39+
ppt_text_instance = PowerpointText(val)
40+
ppt_text_instance.set_val(val)
41+
assert ppt_text_instance._saved_name_ext == None
42+
43+
def test_has_dims(self):
44+
ppt_text_instance = PowerpointText()
45+
assert ppt_text_instance.has_dims() == True
46+
47+
def test_dims_pptx(self):
48+
powerpoint_var = [914400, 914400, 914400, 914400] # when dims are 1 the direct powerpoint equivalent is 914400
49+
ppt_text_instance = PowerpointText(dims=(1.0, 1.0, 1.0, 1.0))
50+
assert ppt_text_instance.dims_pptx() == powerpoint_var
51+
52+
def test_compute_height(self):
53+
test_font_pnt = 10
54+
test_nlines = 1
55+
value_should_be = 0.20666666666666667
56+
57+
ppt_text_instance = PowerpointText()
58+
assert ppt_text_instance.compute_height(test_font_pnt, test_nlines) == value_should_be
59+
60+
def test_compute_and_assign_height(self):
61+
font_pnt = 10
62+
value_should_be = (1.0, 1.0, 1.0, 0.20666666666666667)
63+
ppt_text_instance = PowerpointText(dims=(1.0, 1.0, 1.0, 1.0))
64+
ppt_text_instance.compute_and_assign_height(font_pnt)
65+
66+
assert ppt_text_instance.dims == value_should_be

0 commit comments

Comments
 (0)