|
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 | +- Add unit tests for the new calendar logic under ``tests/test_converters/calendars/`` |
| 32 | +- Add the new calendar to the ``Calendar`` enum of supported calendars in |
| 33 | + ``undate/undate.py`` and confirm that the `get_converter` method loads your |
| 34 | + calendar converter correctly (an existing unit test should cover this). |
| 35 | +- Consider creating a notebook to demonstrate the use of the calendar |
| 36 | + converter. |
| 37 | +
|
| 38 | +Calendar converter subclasses are also automatically loaded and included |
| 39 | +in the list of available converters. |
| 40 | +
|
21 | 41 | -------------------
|
22 | 42 | """
|
23 | 43 |
|
@@ -90,6 +110,42 @@ def available_converters(cls) -> Dict[str, Type["BaseDateConverter"]]:
|
90 | 110 | """
|
91 | 111 | Dictionary of available converters keyed on name.
|
92 | 112 | """
|
| 113 | + return {c.name: c for c in cls.subclasses()} # type: ignore |
| 114 | + |
| 115 | + @classmethod |
| 116 | + def subclasses(cls) -> list[Type["BaseDateConverter"]]: |
| 117 | + """ |
| 118 | + List of available converters classes. Includes calendar convert |
| 119 | + subclasses. |
| 120 | + """ |
93 | 121 | # ensure undate converters are imported
|
94 | 122 | cls.import_converters()
|
95 |
| - return {c.name: c for c in cls.__subclasses__()} # type: ignore |
| 123 | + |
| 124 | + # find all direct subclasses, excluding base calendar converter |
| 125 | + subclasses = cls.__subclasses__() |
| 126 | + subclasses.remove(BaseCalendarConverter) |
| 127 | + # add all subclasses of calendar converter base class |
| 128 | + subclasses.extend(BaseCalendarConverter.__subclasses__()) |
| 129 | + return subclasses |
| 130 | + |
| 131 | + |
| 132 | +class BaseCalendarConverter(BaseDateConverter): |
| 133 | + """Base class for calendar converters, with additional methods required |
| 134 | + for calendars.""" |
| 135 | + |
| 136 | + #: Converter name. Subclasses must define a unique name. |
| 137 | + name: str = "Base Calendar Converter" |
| 138 | + |
| 139 | + def max_month(self, year: int) -> int: |
| 140 | + """Maximum month for this calendar for this year""" |
| 141 | + raise NotImplementedError |
| 142 | + |
| 143 | + def max_day(self, year: int, month: int) -> int: |
| 144 | + """maximum numeric day for the specified year and month in this calendar""" |
| 145 | + raise NotImplementedError |
| 146 | + |
| 147 | + def to_gregorian(self, year, month, day) -> tuple[int, int, int]: |
| 148 | + """Convert a date for this calendar specified by numeric year, month, and day, |
| 149 | + into the Gregorian equivalent date. Should return a tuple of year, month, day. |
| 150 | + """ |
| 151 | + raise NotImplementedError |
0 commit comments