|
1 | 1 | """ |
2 | | -:class:`undate.converters.BaseDateConverter` provides a base class for |
| 2 | +:class:`~undate.converters.BaseDateConverter` provides a base class for |
3 | 3 | implementing date converters, which can provide support for |
4 | | -parsing and generating dates in different formats and also converting |
5 | | -dates between different calendars. |
| 4 | +parsing and generating dates in different formats. |
| 5 | +The converter subclass :class:`undate.converters.BaseCalendarConverter` |
| 6 | +provides additional functionaly needed for calendar conversion. |
6 | 7 |
|
7 | | -To add support for a new date format or calendar conversion: |
| 8 | +To add support for a new date converter: |
8 | 9 |
|
9 | 10 | - Create a new file under ``undate/converters/`` |
10 | 11 | - For converters with sufficient complexity, you may want to create a submodule; |
|
18 | 19 | The new subclass should be loaded automatically and included in the converters |
19 | 20 | returned by :meth:`BaseDateConverter.available_converters` |
20 | 21 |
|
| 22 | +To add support for a new calendar converter: |
| 23 | +
|
| 24 | +- Create a new file under ``undate/converters/calendars/`` |
| 25 | + - For converters with sufficient complexity, you may want to create a submodule; |
| 26 | + see ``undate.converters.calendars.hijri`` for an example. |
| 27 | +- Extend ``BaseCalendarConverter`` and implement ``parse`` and ``to_string`` |
| 28 | + formatter methods as desired/appropriate for your converter as well as the |
| 29 | + additional methods for ``max_month``, ``max_day``, and convertion ``to_gregorian`` |
| 30 | + calendar. |
| 31 | +- Import your calendar in ``undate/converters/calendars/__init__.py`` and include in `__all__`` |
| 32 | +- Add unit tests for the new calendar logic under ``tests/test_converters/calendars/`` |
| 33 | +- Add the new calendar to the ``Calendar`` enum of supported calendars in |
| 34 | + ``undate/undate.py`` and confirm that the `get_converter` method loads your |
| 35 | + calendar converter correctly (an existing unit test should cover this). |
| 36 | +- Consider creating a notebook to demonstrate the use of the calendar |
| 37 | + converter. |
| 38 | +
|
| 39 | +Calendar converter subclasses are also automatically loaded and included |
| 40 | +in the list of available converters. |
| 41 | +
|
21 | 42 | ------------------- |
22 | 43 | """ |
23 | 44 |
|
@@ -90,6 +111,54 @@ def available_converters(cls) -> Dict[str, Type["BaseDateConverter"]]: |
90 | 111 | """ |
91 | 112 | Dictionary of available converters keyed on name. |
92 | 113 | """ |
| 114 | + return {c.name: c for c in cls.subclasses()} # type: ignore |
| 115 | + |
| 116 | + @classmethod |
| 117 | + def subclasses(cls) -> list[Type["BaseDateConverter"]]: |
| 118 | + """ |
| 119 | + List of available converters classes. Includes calendar convert |
| 120 | + subclasses. |
| 121 | + """ |
93 | 122 | # ensure undate converters are imported |
94 | 123 | cls.import_converters() |
95 | | - return {c.name: c for c in cls.__subclasses__()} # type: ignore |
| 124 | + |
| 125 | + # find all direct subclasses, excluding base calendar converter |
| 126 | + subclasses = cls.__subclasses__() |
| 127 | + subclasses.remove(BaseCalendarConverter) |
| 128 | + # add all subclasses of calendar converter base class |
| 129 | + subclasses.extend(BaseCalendarConverter.__subclasses__()) |
| 130 | + return subclasses |
| 131 | + |
| 132 | + |
| 133 | +class BaseCalendarConverter(BaseDateConverter): |
| 134 | + """Base class for calendar converters, with additional methods required |
| 135 | + for calendars.""" |
| 136 | + |
| 137 | + #: Converter name. Subclasses must define a unique name. |
| 138 | + name: str = "Base Calendar Converter" |
| 139 | + |
| 140 | + def min_month(self) -> int: |
| 141 | + """Smallest numeric month for this calendar.""" |
| 142 | + raise NotImplementedError |
| 143 | + |
| 144 | + def max_month(self, year: int) -> int: |
| 145 | + """Maximum numeric month for this calendar""" |
| 146 | + raise NotImplementedError |
| 147 | + |
| 148 | + def first_month(self) -> int: |
| 149 | + """first month in this calendar; by default, returns :meth:`min_month`.""" |
| 150 | + return self.min_month() |
| 151 | + |
| 152 | + def last_month(self, year: int) -> int: |
| 153 | + """last month in this calendar; by default, returns :meth:`max_month`.""" |
| 154 | + return self.max_month(year) |
| 155 | + |
| 156 | + def max_day(self, year: int, month: int) -> int: |
| 157 | + """maximum numeric day for the specified year and month in this calendar""" |
| 158 | + raise NotImplementedError |
| 159 | + |
| 160 | + def to_gregorian(self, year, month, day) -> tuple[int, int, int]: |
| 161 | + """Convert a date for this calendar specified by numeric year, month, and day, |
| 162 | + into the Gregorian equivalent date. Should return a tuple of year, month, day. |
| 163 | + """ |
| 164 | + raise NotImplementedError |
0 commit comments