|
1 | | -from typing import Optional |
| 1 | +from typing import Optional, List |
2 | 2 |
|
3 | 3 | from validator_collection import validators |
4 | 4 |
|
5 | 5 | from highcharts_core import constants |
6 | 6 | 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 |
7 | 120 |
|
8 | 121 |
|
9 | 122 | class DateTimeLabelFormats(HighchartsMeta): |
@@ -32,108 +145,164 @@ def __init__(self, **kwargs): |
32 | 145 | self.year = kwargs.get('year', None) |
33 | 146 |
|
34 | 147 | @property |
35 | | - def day(self) -> Optional[str]: |
| 148 | + def day(self) -> Optional[str | DateTimeLabelUnitOptions]: |
36 | 149 | """Label format to apply to days. Defaults to |
37 | 150 | ``'%A, %b %e, %Y'``. |
38 | 151 |
|
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>` |
40 | 155 | """ |
41 | 156 | return self._day |
42 | 157 |
|
43 | 158 | @day.setter |
44 | 159 | 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) |
46 | 166 |
|
47 | 167 | @property |
48 | | - def hour(self) -> Optional[str]: |
| 168 | + def hour(self) -> Optional[str | DateTimeLabelRangedUnitOptions]: |
49 | 169 | """Label format to apply to hours. Defaults to |
50 | 170 | ``'%A, %b %e, %H:%M'``. |
51 | 171 |
|
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>` |
53 | 175 | """ |
54 | 176 | return self._hour |
55 | 177 |
|
56 | 178 | @hour.setter |
57 | 179 | 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) |
59 | 186 |
|
60 | 187 | @property |
61 | | - def millisecond(self) -> Optional[str]: |
| 188 | + def millisecond(self) -> Optional[str | DateTimeLabelRangedUnitOptions]: |
62 | 189 | """Label format to apply to milliseconds. Defaults to |
63 | 190 | ``'%A, %b %e, %H:%M:%S.%L'``. |
64 | 191 |
|
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>` |
66 | 195 | """ |
67 | 196 | return self._millisecond |
68 | 197 |
|
69 | 198 | @millisecond.setter |
70 | 199 | 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) |
72 | 206 |
|
73 | 207 | @property |
74 | | - def minute(self) -> Optional[str]: |
| 208 | + def minute(self) -> Optional[str | DateTimeLabelRangedUnitOptions]: |
75 | 209 | """Label format to apply to minutes. Defaults to |
76 | 210 | ``'%A, %b %e, %H:%M'``. |
77 | 211 |
|
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>` |
79 | 215 | """ |
80 | 216 | return self._minute |
81 | 217 |
|
82 | 218 | @minute.setter |
83 | 219 | 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) |
85 | 226 |
|
86 | 227 | @property |
87 | | - def month(self) -> Optional[str]: |
| 228 | + def month(self) -> Optional[str | DateTimeLabelUnitOptions]: |
88 | 229 | """Label format to apply to months. Defaults to |
89 | 230 | ``'%B %Y'``. |
90 | 231 |
|
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>` |
92 | 235 | """ |
93 | 236 | return self._month |
94 | 237 |
|
95 | 238 | @month.setter |
96 | 239 | 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) |
98 | 246 |
|
99 | 247 | @property |
100 | | - def second(self) -> Optional[str]: |
| 248 | + def second(self) -> Optional[str | DateTimeLabelRangedUnitOptions]: |
101 | 249 | """Label format to apply to seconds. Defaults to |
102 | 250 | ``'%A, %b %e, %H:%M:%S'``. |
103 | 251 |
|
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>` |
105 | 255 | """ |
106 | 256 | return self._second |
107 | 257 |
|
108 | 258 | @second.setter |
109 | 259 | 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) |
111 | 266 |
|
112 | 267 | @property |
113 | | - def week(self) -> Optional[str]: |
| 268 | + def week(self) -> Optional[str | DateTimeLabelUnitOptions]: |
114 | 269 | """Label format to apply to weeks. Defaults to |
115 | 270 | ``'Week from %A, %b %e, %Y'``. |
116 | 271 |
|
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>` |
118 | 275 | """ |
119 | 276 | return self._week |
120 | 277 |
|
121 | 278 | @week.setter |
122 | 279 | 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) |
124 | 286 |
|
125 | 287 | @property |
126 | | - def year(self) -> Optional[str]: |
| 288 | + def year(self) -> Optional[str | DateTimeLabelUnitOptions]: |
127 | 289 | """Label format to apply to years. Defaults to |
128 | 290 | ``'%Y'``. |
129 | 291 |
|
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>` |
131 | 295 | """ |
132 | 296 | return self._year |
133 | 297 |
|
134 | 298 | @year.setter |
135 | 299 | 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) |
137 | 306 |
|
138 | 307 | @classmethod |
139 | 308 | def _get_kwargs_from_dict(cls, as_dict): |
|
0 commit comments