Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add categories and subdivisions support to special observed holidays #1561

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions holidays/holiday_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def _populate(self, year):
"""Country weekend days."""
default_language: Optional[str] = None
"""The entity language used by default."""
categories: Optional[Set[str]] = None
categories: Set[str] = set()
arkid15r marked this conversation as resolved.
Show resolved Hide resolved
"""Requested holiday categories."""
supported_categories: Set[str] = set()
"""All holiday categories supported by this entity."""
Expand Down Expand Up @@ -750,28 +750,29 @@ def _populate(self, year: int) -> None:
# Populate substituted holidays.
self._add_substituted_holidays()

def _add_special_holidays(self):
def _get_special_holiday_mapping_names(self):
# Check for general special holidays.
special_holidays_mapping_names = ["special_holidays"]
mapping_names = ["special_holidays"]

# Check subdivision specific special holidays.
if self.subdiv is not None:
subdiv = self.subdiv.replace("-", "_").replace(" ", "_").lower()
special_holidays_mapping_names.append(f"special_{subdiv}_holidays")
mapping_names.append(f"special_{subdiv}_holidays")

# Check category specific special holidays (both general and per subdivision).
for category in sorted(self.categories):
special_holidays_mapping_names.append(f"special_{category}_holidays")
mapping_names.append(f"special_{category}_holidays")
if self.subdiv is not None:
special_holidays_mapping_names.append(f"special_{subdiv}_{category}_holidays")

for mapping_name in special_holidays_mapping_names:
special_holidays_mapping = getattr(self, mapping_name, None)
if special_holidays_mapping:
for month, day, name in _normalize_tuple(
special_holidays_mapping.get(self._year, ())
):
self._add_holiday(name, date(self._year, month, day))
mapping_names.append(f"special_{subdiv}_{category}_holidays")

return mapping_names

def _add_special_holidays(self):
for mapping_name in self._get_special_holiday_mapping_names():
for month, day, name in _normalize_tuple(
getattr(self, mapping_name, {}).get(self._year, ())
):
self._add_holiday(name, date(self._year, month, day))

def _add_category_holidays(self):
for category in sorted(self.categories):
Expand Down
7 changes: 5 additions & 2 deletions holidays/observed_holiday_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,12 @@ def _populate_observed(self, dts: Set[date], multiple: bool = False) -> None:
def _add_special_holidays(self):
super()._add_special_holidays()

if self.observed:
if not self.observed:
return None

for mapping_name in self._get_special_holiday_mapping_names():
for month, day, name in _normalize_tuple(
getattr(self, "special_holidays_observed", {}).get(self._year, ())
getattr(self, f"{mapping_name}_observed", {}).get(self._year, ())
):
self._add_holiday(
self.tr(self.observed_label) % self.tr(name), date(self._year, month, day)
Expand Down