Skip to content

Commit

Permalink
Add categories and subdivisions support to special observed holidays (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
KJhellico authored Nov 28, 2023
1 parent 5c019de commit 5eabe3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
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()
"""Requested holiday categories."""
supported_categories: Tuple[str, ...] = ()
"""All holiday categories supported by this entity."""
Expand Down Expand Up @@ -751,28 +751,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

0 comments on commit 5eabe3a

Please sign in to comment.