1
1
"""
2
- Base class for date format parsing and serializing
2
+ Base class for converting date between different formats and calendars.
3
3
4
- To add support for a new date format:
4
+ To add support for a new date format or conversion :
5
5
6
- - create a new file under undate/dateformat
7
- - extend BaseDateFormat and implement parse and to_string methods
6
+ - create a new file or module under undate/converters
7
+ - extend BaseDateConverter and implement parse and to_string methods
8
8
as desired/appropriate
9
9
10
- It should be loaded automatically and included in the formatters
11
- returned by :meth:`BaseDateFormat.available_formatters `
10
+ The new subclass should be loaded automatically and included in the converters
11
+ returned by :meth:`BaseDateConverter.available_converters `
12
12
13
13
"""
14
14
21
21
logger = logging .getLogger (__name__ )
22
22
23
23
24
- class BaseDateFormat :
25
- """Base class for parsing and formatting dates for specific formats."""
24
+ class BaseDateConverter :
25
+ """Base class for parsing, formatting, and converting dates to handle
26
+ specific formats and different calendars."""
26
27
27
28
# Subclasses should define a unique name.
28
- name : str = "Base Formatter "
29
+ name : str = "Base Converter "
29
30
30
31
def parse (self , value : str ):
31
32
# can't add type hint here because of circular import
@@ -40,22 +41,22 @@ def to_string(self, undate) -> str:
40
41
# cache import class method to ensure we only import once
41
42
@classmethod
42
43
@cache
43
- def import_formatters (cls ) -> int :
44
- """Import all undate.dateformat formatters
45
- so that they will be included in available formatters
44
+ def import_converters (cls ) -> int :
45
+ """Import all undate converters
46
+ so that they will be included in available converters
46
47
even if not explicitly imported. Only import once.
47
48
returns the count of modules imported."""
48
49
49
- logger .debug ("Loading formatters under undate.dateformat " )
50
- import undate .dateformat
50
+ logger .debug ("Loading converters under undate.converters " )
51
+ import undate .converters
51
52
52
53
# load packages under this path with curent package prefix
53
- formatter_path = undate .dateformat .__path__
54
- formatter_prefix = f"{ undate .dateformat .__name__ } ."
54
+ converter_path = undate .converters .__path__
55
+ converter_prefix = f"{ undate .converters .__name__ } ."
55
56
56
57
import_count = 0
57
58
for importer , modname , ispkg in pkgutil .iter_modules (
58
- formatter_path , formatter_prefix
59
+ converter_path , converter_prefix
59
60
):
60
61
# import everything except the current file
61
62
if not modname .endswith (".base" ):
@@ -65,7 +66,7 @@ def import_formatters(cls) -> int:
65
66
return import_count
66
67
67
68
@classmethod
68
- def available_formatters (cls ) -> Dict [str , Type ["BaseDateFormat " ]]:
69
- # ensure undate formatters are imported
70
- cls .import_formatters ()
69
+ def available_converters (cls ) -> Dict [str , Type ["BaseDateConverter " ]]:
70
+ # ensure undate converters are imported
71
+ cls .import_converters ()
71
72
return {c .name : c for c in cls .__subclasses__ ()} # type: ignore
0 commit comments