Skip to content

Commit e2106f9

Browse files
committed
Implemented DateTimeLabelUnitOptions.
1 parent 73f6139 commit e2106f9

File tree

1 file changed

+194
-25
lines changed

1 file changed

+194
-25
lines changed

highcharts_core/utility_classes/date_time_label_formats.py

Lines changed: 194 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,122 @@
1-
from typing import Optional
1+
from typing import Optional, List
22

33
from validator_collection import validators
44

55
from highcharts_core import constants
66
from highcharts_core.metaclasses import HighchartsMeta
7+
from highcharts_core.decorators import validate_types
8+
9+
10+
class DateTimeLabelUnitOptions(HighchartsMeta):
11+
"""Options for the datetime labels for a particular unit type."""
12+
13+
def __init__(self, **kwargs):
14+
self._list = None
15+
self._main = None
16+
17+
self.list = kwargs.get('list', None)
18+
self.main = kwargs.get('main', None)
19+
20+
@property
21+
def list(self) -> Optional[List[str]]:
22+
"""List of possible values for the datetime unit label. Defaults to
23+
:obj:`None <python:None>`.
24+
25+
:rtype: :class:`List <python:list>` of :class:`str <python:str>` or
26+
:obj:`None <python:None>`
27+
"""
28+
return self._list
29+
30+
@list.setter
31+
def list(self, value):
32+
if not value:
33+
self._list = None
34+
else:
35+
self._list = [str(x) for x in value]
36+
37+
@property
38+
def main(self) -> Optional[str]:
39+
"""Label format to apply to the unit. The default varies by unit:
40+
41+
* ``'day'``: ``'%e %b'``
42+
* ``'hour`':: ``%H:%M'``
43+
* ``'millisecond'``: ``'%H:%M:%S.%L'``
44+
* ``'minute'``: ``'%H:%M'``
45+
* ``'month'``: ``"%b '%Y"``
46+
* ``'second'``: ``'%H:%M:%S'``
47+
* ``'week'``: ``'%e %b'``
48+
* ``'year'``: ``'%Y'``
49+
50+
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
51+
"""
52+
return self._main
53+
54+
@main.setter
55+
def main(self, value):
56+
self._main = validators.string(value, allow_empty = True)
57+
58+
@classmethod
59+
def _get_kwargs_from_dict(cls, as_dict):
60+
kwargs = {
61+
"list": as_dict.get("list", None),
62+
"main": as_dict.get("main", None),
63+
}
64+
65+
return kwargs
66+
67+
def _to_untrimmed_dict(self, in_cls=None) -> dict:
68+
untrimmed = {
69+
"list": self.list,
70+
"main": self.main,
71+
}
72+
73+
return untrimmed
74+
75+
76+
class DateTimeLabelRangedUnitOptions(DateTimeLabelUnitOptions):
77+
"""Options for the datetime labels for unit types that support a range."""
78+
79+
def __init__(self, **kwargs):
80+
self._range = None
81+
82+
self.range = kwargs.get("range", None)
83+
84+
super().__init__(**kwargs)
85+
86+
@property
87+
def range(self) -> Optional[bool]:
88+
"""Whether the unit supports a range. Defaults to :obj:`False <python:False>`.
89+
90+
:rtype: :class:`bool <python:bool>` or :obj:`None <python:None>`
91+
"""
92+
return self._range
93+
94+
@range.setter
95+
def range(self, value):
96+
if value is None:
97+
self._range = None
98+
else:
99+
self._range = bool(value)
100+
101+
@classmethod
102+
def _get_kwargs_from_dict(cls, as_dict):
103+
kwargs = {
104+
"list": as_dict.get("list", None),
105+
"main": as_dict.get("main", None),
106+
107+
'range': as_dict.get('range', None),
108+
}
109+
110+
return kwargs
111+
112+
def _to_untrimmed_dict(self, in_cls=None) -> dict:
113+
untrimmed = {
114+
"list": self.list,
115+
"main": self.main,
116+
'range': slef.range,
117+
}
118+
119+
return untrimmed
7120

8121

9122
class DateTimeLabelFormats(HighchartsMeta):
@@ -32,108 +145,164 @@ def __init__(self, **kwargs):
32145
self.year = kwargs.get('year', None)
33146

34147
@property
35-
def day(self) -> Optional[str]:
148+
def day(self) -> Optional[str | DateTimeLabelUnitOptions]:
36149
"""Label format to apply to days. Defaults to
37150
``'%A, %b %e, %Y'``.
38151
39-
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
152+
:rtype: :class:`str <python:str>` or
153+
:class:`DateTimeLabelUnitOptions <highcharts_core.utility_classes.date_time_label_formats.DateTimeLabelUnitOptions>` or
154+
:obj:`None <python:None>`
40155
"""
41156
return self._day
42157

43158
@day.setter
44159
def day(self, value):
45-
self._day = validators.string(value, allow_empty = True)
160+
if not value:
161+
self._day = None
162+
elif isinstance(value, str):
163+
self._day = value
164+
else:
165+
self._day = validate_types(value, DateTimeLabelUnitOptions)
46166

47167
@property
48-
def hour(self) -> Optional[str]:
168+
def hour(self) -> Optional[str | DateTimeLabelRangedUnitOptions]:
49169
"""Label format to apply to hours. Defaults to
50170
``'%A, %b %e, %H:%M'``.
51171
52-
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
172+
:rtype: :class:`str <python:str>` or
173+
:class:`DateTimeLabelRangedUnitOptions <highcharts_core.utility_classes.date_time_label_formats.DateTimeLabelRangedUnitOptions>` or
174+
:obj:`None <python:None>`
53175
"""
54176
return self._hour
55177

56178
@hour.setter
57179
def hour(self, value):
58-
self._hour = validators.string(value, allow_empty = True)
180+
if not value:
181+
self._hour = None
182+
elif isinstance(value, str):
183+
self._hour = value
184+
else:
185+
self._hour = validate_types(value, DateTimeLabelUnitOptions)
59186

60187
@property
61-
def millisecond(self) -> Optional[str]:
188+
def millisecond(self) -> Optional[str | DateTimeLabelRangedUnitOptions]:
62189
"""Label format to apply to milliseconds. Defaults to
63190
``'%A, %b %e, %H:%M:%S.%L'``.
64191
65-
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
192+
:rtype: :class:`str <python:str>` or
193+
:class:`DateTimeLabelRangedUnitOptions <highcharts_core.utility_classes.date_time_label_formats.DateTimeLabelRangedUnitOptions>` or
194+
:obj:`None <python:None>`
66195
"""
67196
return self._millisecond
68197

69198
@millisecond.setter
70199
def millisecond(self, value):
71-
self._millisecond = validators.string(value, allow_empty = True)
200+
if not value:
201+
self._millisecond = None
202+
elif isinstance(value, str):
203+
self._millisecond = value
204+
else:
205+
self._millisecond = validate_types(value, DateTimeLabelUnitOptions)
72206

73207
@property
74-
def minute(self) -> Optional[str]:
208+
def minute(self) -> Optional[str | DateTimeLabelRangedUnitOptions]:
75209
"""Label format to apply to minutes. Defaults to
76210
``'%A, %b %e, %H:%M'``.
77211
78-
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
212+
:rtype: :class:`str <python:str>` or
213+
:class:`DateTimeLabelRangedUnitOptions <highcharts_core.utility_classes.date_time_label_formats.DateTimeLabelRangedUnitOptions>` or
214+
:obj:`None <python:None>`
79215
"""
80216
return self._minute
81217

82218
@minute.setter
83219
def minute(self, value):
84-
self._minute = validators.string(value, allow_empty = True)
220+
if not value:
221+
self._minute = None
222+
elif isinstance(value, str):
223+
self._minute = value
224+
else:
225+
self._minute = validate_types(value, DateTimeLabelUnitOptions)
85226

86227
@property
87-
def month(self) -> Optional[str]:
228+
def month(self) -> Optional[str | DateTimeLabelUnitOptions]:
88229
"""Label format to apply to months. Defaults to
89230
``'%B %Y'``.
90231
91-
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
232+
:rtype: :class:`str <python:str>` or
233+
:class:`DateTimeLabelUnitOptions <highcharts_core.utility_classes.date_time_label_formats.DateTimeLabelUnitOptions>` or
234+
:obj:`None <python:None>`
92235
"""
93236
return self._month
94237

95238
@month.setter
96239
def month(self, value):
97-
self._month = validators.string(value, allow_empty = True)
240+
if not value:
241+
self._month = None
242+
elif isinstance(value, str):
243+
self._month = value
244+
else:
245+
self._month = validate_types(value, DateTimeLabelUnitOptions)
98246

99247
@property
100-
def second(self) -> Optional[str]:
248+
def second(self) -> Optional[str | DateTimeLabelRangedUnitOptions]:
101249
"""Label format to apply to seconds. Defaults to
102250
``'%A, %b %e, %H:%M:%S'``.
103251
104-
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
252+
:rtype: :class:`str <python:str>` or
253+
:class:`DateTimeLabelRangedUnitOptions <highcharts_core.utility_classes.date_time_label_formats.DateTimeLabelRangedUnitOptions>` or
254+
:obj:`None <python:None>`
105255
"""
106256
return self._second
107257

108258
@second.setter
109259
def second(self, value):
110-
self._second = validators.string(value, allow_empty = True)
260+
if not value:
261+
self._second = None
262+
elif isinstance(value, str):
263+
self._second = value
264+
else:
265+
self._second = validate_types(value, DateTimeLabelUnitOptions)
111266

112267
@property
113-
def week(self) -> Optional[str]:
268+
def week(self) -> Optional[str | DateTimeLabelUnitOptions]:
114269
"""Label format to apply to weeks. Defaults to
115270
``'Week from %A, %b %e, %Y'``.
116271
117-
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
272+
:rtype: :class:`str <python:str>` or
273+
:class:`DateTimeLabelUnitOptions <highcharts_core.utility_classes.date_time_label_formats.DateTimeLabelUnitOptions>` or
274+
:obj:`None <python:None>`
118275
"""
119276
return self._week
120277

121278
@week.setter
122279
def week(self, value):
123-
self._week = validators.string(value, allow_empty = True)
280+
if not value:
281+
self._week = None
282+
elif isinstance(value, str):
283+
self._week = value
284+
else:
285+
self._week = validate_types(value, DateTimeLabelUnitOptions)
124286

125287
@property
126-
def year(self) -> Optional[str]:
288+
def year(self) -> Optional[str | DateTimeLabelUnitOptions]:
127289
"""Label format to apply to years. Defaults to
128290
``'%Y'``.
129291
130-
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
292+
:rtype: :class:`str <python:str>` or
293+
:class:`DateTimeLabelUnitOptions <highcharts_core.utility_classes.date_time_label_formats.DateTimeLabelUnitOptions>` or
294+
:obj:`None <python:None>`
131295
"""
132296
return self._year
133297

134298
@year.setter
135299
def year(self, value):
136-
self._year = validators.string(value, allow_empty = True)
300+
if not value:
301+
self._year = None
302+
elif isinstance(value, str):
303+
self._year = value
304+
else:
305+
self._year = validate_types(value, DateTimeLabelUnitOptions)
137306

138307
@classmethod
139308
def _get_kwargs_from_dict(cls, as_dict):

0 commit comments

Comments
 (0)