Skip to content

Commit

Permalink
Refactor MessagesList class and update references in core.py
Browse files Browse the repository at this point in the history
  • Loading branch information
taldcroft committed Dec 30, 2023
1 parent 3fd1f66 commit a6e1dd6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
21 changes: 21 additions & 0 deletions sparkles/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ def __getitem__(self, key):
return getattr(self, key)


class MessagesList(list[Message]):
categories = ("all", "info", "caution", "warning", "critical", "none")

def __eq__(self, other):
if isinstance(other, str):
return [msg for msg in self if msg["category"] == other]
else:
return super().__eq__(other)

def __ge__(self, other):
if isinstance(other, str):
other_idx = self.categories.index(other)
return [
msg
for msg in self
if self.categories.index(msg["category"]) >= other_idx
]
else:
return super().__ge__(other)


def acar_check_wrapper(func):
"""Wrapper to call check functions with ACAReviewTable.

Expand Down
27 changes: 3 additions & 24 deletions sparkles/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def get_summary_text(acas):
)

# Warnings
for category in reversed(MessagesList.categories):
for category in reversed(checks.MessagesList.categories):
msgs = aca.messages == category
if msgs:
text = stylize(f" {category.capitalize()}: {len(msgs)}", category)
Expand All @@ -598,27 +598,6 @@ def get_summary_text(acas):
return "\n".join(lines)


class MessagesList(list[checks.Message]):
categories = ("all", "info", "caution", "warning", "critical", "none")

def __eq__(self, other):
if isinstance(other, str):
return [msg for msg in self if msg["category"] == other]
else:
return super().__eq__(other)

def __ge__(self, other):
if isinstance(other, str):
other_idx = self.categories.index(other)
return [
msg
for msg in self
if self.categories.index(msg["category"]) >= other_idx
]
else:
return super().__ge__(other)


class ACAReviewTable(ACATable, RollOptimizeMixin):
# Whether this instance is a roll option (controls how HTML report page is formatted)
is_roll_option = MetaAttribute()
Expand Down Expand Up @@ -663,7 +642,7 @@ def __init__(self, *args, **kwargs):
# Add row and col columns from yag/zag, if not already there.
self.add_row_col()

self.messages = MessagesList() # Warning messages
self.messages = checks.MessagesList() # Warning messages

# Instance attributes that won't survive pickling
self.context = {} # Jinja2 context for output HTML review
Expand Down Expand Up @@ -927,7 +906,7 @@ def make_roll_options_report(self):
msgs_summaries = []
for aca in acas:
texts = []
for category in reversed(MessagesList.categories):
for category in reversed(checks.MessagesList.categories):
msgs = aca.messages == category
if msgs:
text = stylize(f"{category.capitalize()}: {len(msgs)}", category)
Expand Down

0 comments on commit a6e1dd6

Please sign in to comment.