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

View File

@ -25,7 +25,7 @@ import typing as t
from datetime import date, timedelta
from accounting.locale import gettext
from .description import PeriodDescription
from .description import get_desc
from .parser import parse_spec
from .specification import PeriodSpecification
@ -91,7 +91,7 @@ class Period:
:return: None.
"""
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:
return
self.is_a_month = self.start.day == 1 \