Replaced the PeriodDescription object-based utility with the get_desc function-based utility, for simplicity.

This commit is contained in:
依瑪貓 2023-03-09 19:22:06 +08:00
parent 060a52f7a2
commit 7ad3f9e0cb
2 changed files with 129 additions and 127 deletions

View File

@ -22,156 +22,158 @@ from datetime import date, timedelta
from accounting.locale import gettext from accounting.locale import gettext
class PeriodDescription: def get_desc(start: date | None, end: date | None) -> str:
"""The period description composer.""" """Returns the period description.
def __init__(self, start: date | None, end: date | None): :param start: The start of the period.
"""Constructs the period description composer. :param end: The end of the period.
:return: The period description.
"""
if start is None and end is None:
return gettext("for all time")
if start is None:
return __get_until_desc(end)
if end is None:
return __get_since_desc(start)
try:
return __get_year_desc(start, end)
except ValueError:
pass
try:
return __get_month_desc(start, end)
except ValueError:
pass
return __get_day_desc(start, end)
:param start: The start of the period.
:param end: The end of the period. def __get_since_desc(start: date) -> str:
"""Returns the description without the end day.
:param start: The start of the period.
:return: The description without the end day.
"""
def get_start_desc() -> str:
"""Returns the description of the start day.
:return: The description of the start day.
""" """
self.__start: date | None = start if start.month == 1 and start.day == 1:
self.__end: date | None = end return str(start.year)
self.desc: str = self.__get_desc() if start.day == 1:
return __format_month(start)
return __format_day(start)
def __get_desc(self) -> str: return gettext("since %(start)s", start=get_start_desc())
"""Returns the period description.
:return: The period description.
def __get_until_desc(end: date) -> str:
"""Returns the description without the start day.
:param end: The end of the period.
:return: The description without the start day.
"""
def get_end_desc() -> str:
"""Returns the description of the end day.
:return: The description of the end day.
""" """
if self.__start is None and self.__end is None: if end.month == 12 and end.day == 31:
return gettext("for all time") return str(end.year)
if self.__start is None: if (end + timedelta(days=1)).day == 1:
return self.__get_until_desc() return __format_month(end)
if self.__end is None: return __format_day(end)
return self.__get_since_desc()
try:
return self.__get_year_desc()
except ValueError:
pass
try:
return self.__get_month_desc()
except ValueError:
pass
return self.__get_day_desc()
def __get_since_desc(self) -> str: return gettext("until %(end)s", end=get_end_desc())
"""Returns the description without the end day.
:return: The description without the end day.
"""
def get_start_desc() -> str:
"""Returns the description of the start day.
:return: The description of the start day. def __get_year_desc(start: date, end: date) -> str:
""" """Returns the description as a year range.
if self.__start.month == 1 and self.__start.day == 1:
return str(self.__start.year)
if self.__start.day == 1:
return self.__format_month(self.__start)
return self.__format_day(self.__start)
return gettext("since %(start)s", start=get_start_desc()) :param start: The start of the period.
:param end: The end of the period.
:return: The description as a year range.
:raise ValueError: The period is not a year range.
"""
if start.month != 1 or start.day != 1 \
or end.month != 12 or end.day != 31:
raise ValueError
start_text: str = str(start.year)
if start.year == end.year:
return __get_in_desc(start_text)
return __get_from_to_desc(start_text, str(end.year))
def __get_until_desc(self) -> str:
"""Returns the description without the start day.
:return: The description without the start day. def __get_month_desc(start: date, end: date) -> str:
""" """Returns the description as a month range.
def get_end_desc() -> str:
"""Returns the description of the end day.
:return: The description of the end day. :param start: The start of the period.
""" :param end: The end of the period.
if self.__end.month == 12 and self.__end.day == 31: :return: The description as a month range.
return str(self.__end.year) :raise ValueError: The period is not a month range.
if (self.__end + timedelta(days=1)).day == 1: """
return self.__format_month(self.__end) if start.day != 1 or (end + timedelta(days=1)).day != 1:
return self.__format_day(self.__end) raise ValueError
start_text: str = __format_month(start)
if start.year == end.year and start.month == end.month:
return __get_in_desc(start_text)
if start.year == end.year:
return __get_from_to_desc(start_text, str(end.month))
return __get_from_to_desc(start_text, __format_month(end))
return gettext("until %(end)s", end=get_end_desc())
def __get_year_desc(self) -> str: def __get_day_desc(start: date, end: date) -> str:
"""Returns the description as a year range. """Returns the description as a day range.
:return: The description as a year range. :param start: The start of the period.
:raise ValueError: The period is not a year range. :param end: The end of the period.
""" :return: The description as a day range.
if self.__start.month != 1 or self.__start.day != 1 \ :raise ValueError: The period is a month or year range.
or self.__end.month != 12 or self.__end.day != 31: """
raise ValueError start_text: str = __format_day(start)
start: str = str(self.__start.year) if start == end:
if self.__start.year == self.__end.year: return __get_in_desc(start_text)
return self.__get_in_desc(start) if start.year == end.year and start.month == end.month:
return self.__get_from_to_desc(start, str(self.__end.year)) return __get_from_to_desc(start_text, str(end.day))
if start.year == end.year:
end_month_day: str = f"{end.month}/{end.day}"
return __get_from_to_desc(start_text, end_month_day)
return __get_from_to_desc(start_text, __format_day(end))
def __get_month_desc(self) -> str:
"""Returns the description as a month range.
:return: The description as a month range. def __format_month(month: date) -> str:
:raise ValueError: The period is not a month range. """Formats a month.
"""
if self.__start.day != 1 or (self.__end + timedelta(days=1)).day != 1:
raise ValueError
start: str = self.__format_month(self.__start)
if self.__start.year == self.__end.year \
and self.__start.month == self.__end.month:
return self.__get_in_desc(start)
if self.__start.year == self.__end.year:
return self.__get_from_to_desc(start, str(self.__end.month))
return self.__get_from_to_desc(start, self.__format_month(self.__end))
def __get_day_desc(self) -> str: :param month: The month.
"""Returns the description as a day range. :return: The formatted month.
"""
return f"{month.year}/{month.month}"
:return: The description as a day range.
:raise ValueError: The period is a month or year range.
"""
start: str = self.__format_day(self.__start)
if self.__start == self.__end:
return self.__get_in_desc(start)
if self.__start.year == self.__end.year \
and self.__start.month == self.__end.month:
return self.__get_from_to_desc(start, str(self.__end.day))
if self.__start.year == self.__end.year:
end_month_day: str = f"{self.__end.month}/{self.__end.day}"
return self.__get_from_to_desc(start, end_month_day)
return self.__get_from_to_desc(start, self.__format_day(self.__end))
@staticmethod def __format_day(day: date) -> str:
def __format_month(month: date) -> str: """Formats a day.
"""Formats a month.
:param month: The month. :param day: The day.
:return: The formatted month. :return: The formatted day.
""" """
return f"{month.year}/{month.month}" return f"{day.year}/{day.month}/{day.day}"
@staticmethod
def __format_day(day: date) -> str:
"""Formats a day.
:param day: The day. def __get_in_desc(period: str) -> str:
:return: The formatted day. """Returns the description of a whole year, month, or day.
"""
return f"{day.year}/{day.month}/{day.day}"
@staticmethod :param period: The time period.
def __get_in_desc(period: str) -> str: :return: The description of a whole year, month, or day.
"""Returns the description of a whole year, month, or day. """
return gettext("in %(period)s", period=period)
:param period: The time period.
:return: The description of a whole year, month, or day.
"""
return gettext("in %(period)s", period=period)
@staticmethod def __get_from_to_desc(start: str, end: str) -> str:
def __get_from_to_desc(start: str, end: str) -> str: """Returns the description of a separated start and end.
"""Returns the description of a separated start and end.
:param start: The start. :param start: The start.
:param end: The end. :param end: The end.
:return: The description of the separated start and end. :return: The description of the separated start and end.
""" """
return gettext("in %(start)s-%(end)s", start=start, end=end) return gettext("in %(start)s-%(end)s", start=start, end=end)

View File

@ -25,7 +25,7 @@ import typing as t
from datetime import date, timedelta from datetime import date, timedelta
from accounting.locale import gettext from accounting.locale import gettext
from .description import PeriodDescription from .description import get_desc
from .parser import parse_spec from .parser import parse_spec
from .specification import PeriodSpecification from .specification import PeriodSpecification
@ -91,7 +91,7 @@ class Period:
:return: None. :return: None.
""" """
self.spec = PeriodSpecification(self.start, self.end).spec self.spec = PeriodSpecification(self.start, self.end).spec
self.desc = PeriodDescription(self.start, self.end).desc self.desc = get_desc(self.start, self.end)
if self.start is None or self.end is None: if self.start is None or self.end is None:
return return
self.is_a_month = self.start.day == 1 \ self.is_a_month = self.start.day == 1 \