Skip to content

Commit

Permalink
Catch non-RGB tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
masaccio committed Jul 16, 2023
1 parent b882dcb commit 0ccff28
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
33 changes: 18 additions & 15 deletions src/numbers_parser/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def __post_init__(self):
if not isinstance(self.alignment, Alignment):
raise TypeError("value must be an Alignment class")

check_rgb_type(self.bg_color)
check_rgb_type(self.font_color)
self.bg_color = rgb_color(self.bg_color)
self.font_color = rgb_color(self.font_color)

if not isinstance(self.font_size, float):
raise TypeError("size must be a float number of points")
Expand All @@ -170,6 +170,8 @@ def __setattr__(self, name: str, value: Any) -> None:
possible updates when saving the document"""
if name in self.__dict__:
# Init has been done
if name in ["bg_color", "font_color"]:
value = rgb_color(value)
if name in ["bg_color"]:
self.__dict__["_update_cell_style"] = True
elif name in ["alignment"]:
Expand All @@ -189,17 +191,18 @@ def __setattr__(self, name: str, value: Any) -> None:
self.__dict__[name] = value


def check_rgb_type(color) -> RGB:
def rgb_color(color) -> RGB:
"""Raise a TypeError if a color is not a valid RGB value"""
if color is None:
return
return None
if isinstance(color, RGB):
return
return color
if isinstance(color, tuple):
if not (len(color) == 3 and all([isinstance(x, int) for x in color])):
raise TypeError("RGB color must be an RGB or a tuple of 3 integers")
return RGB(*color)
elif isinstance(color, list):
[check_rgb_type(c) for c in color]
return [rgb_color(c) for c in color]


class Cell:
Expand Down Expand Up @@ -243,9 +246,9 @@ def from_storage(cls, cell_storage: CellStorage): # noqa: C901
elif cell_storage.type == CellType.RICH_TEXT:
cell = RichTextCell(*row_col, cell_storage.value)
else:
raise UnsupportedError( # pragma: no cover
f"Unsupport cell type {cell_storage.type} "
+ "@:({cell_storage.row_num},{cell_storage.col_num})"
raise UnsupportedError(
f"Unsupported cell type {cell_storage.type} "
+ f"@:({cell_storage.row_num},{cell_storage.col_num})"
)

cell._table_id = cell_storage.table_id
Expand Down Expand Up @@ -520,10 +523,10 @@ def xl_rowcol_to_cell(row, col, row_abs=False, col_abs=False):
Returns:
A1 style string.
"""
if row < 0: # pragma: no cover
raise IndexError(f"Row reference {row} below zero")
if row < 0:
raise IndexError(f"row reference {row} below zero")

if col < 0: # pragma: no cover
if col < 0:
raise IndexError(f"column reference {col} below zero")

row += 1 # Change to 1-index.
Expand All @@ -544,8 +547,8 @@ def xl_col_to_name(col, col_abs=False):
Column style string.
"""
col_num = col
if col_num < 0: # pragma: no cover
raise IndexError(f"Column reference {col_num} below zero")
if col_num < 0:
raise IndexError(f"column reference {col_num} below zero")

col_num += 1 # Change to 1-index.
col_str = ""
Expand All @@ -555,7 +558,7 @@ def xl_col_to_name(col, col_abs=False):
# Set remainder from 1 .. 26
remainder = col_num % 26

if remainder == 0: # pragma: no cover
if remainder == 0:
remainder = 26

# Convert the remainder to a character.
Expand Down
9 changes: 6 additions & 3 deletions tests/test_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,17 @@ def test_style_exceptions():
doc.add_style(bg_color=(0, 0, 1.0))
assert "RGB color must be an RGB" in str(e)

style = doc.add_style(bg_color=(100, 100, 100))
assert style.bg_color == RGB(100, 100, 100)

styles = doc.styles
assert "Title" in styles
assert len(styles) == 6
assert len(styles) == 7

dummy = doc.add_style()
assert dummy.name == "Custom Style 1"
dummy = doc.add_style()
assert dummy.name == "Custom Style 2"
dummy = doc.add_style()
assert dummy.name == "Custom Style 3"

with pytest.raises(TypeError) as e:
_ = Style(alignment=Alignment("invalid", "top"))
Expand Down

0 comments on commit 0ccff28

Please sign in to comment.