Debian Codegraph @

cron_descriptor/Options.py

raw
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2016 Adam Schubert
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in all
13 # copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 from __future__ import annotations
23
24 import locale
25 import warnings
26
27 from .CasingTypeEnum import CasingTypeEnum
28
29
30 class Options:
31 """
32 Options for parsing and describing a Cron Expression
33 """
34
35 locale_code: str
36 casing_type: CasingTypeEnum
37 verbose: bool
38 day_of_week_start_index_zero: bool
39 use_24hour_time_format: bool
40 locale_location: str | None
41
42 _twelve_hour_locales = (
43 "en_US", # United States
44 "en_CA", # Canada (English)
45 "fr_CA", # Canada (French)
46 "en_AU", # Australia
47 "en_NZ", # New Zealand
48 "en_IE", # Ireland
49 "en_PH", # Philippines
50 "es_MX", # Mexico
51 "en_PK", # Pakistan
52 "en_IN", # India
53 "ar_SA", # Saudi Arabia
54 "bn_BD", # Bangladesh
55 "es_HN", # Honduras
56 "es_SV", # El Salvador
57 "es_NI", # Nicaragua
58 "ar_JO", # Jordan
59 "ar_EG", # Egypt
60 "es_CO", # Colombia
61 )
62
63 def __init__(self,
64 casing_type: CasingTypeEnum = CasingTypeEnum.Sentence,
65 *,
66 verbose: bool = False,
67 day_of_week_start_index_zero: bool = True,
68 use_24hour_time_format: bool | None = None,
69 locale_code: str | None = None,
70 locale_location: str | None = None,
71 ) -> None:
72 self.casing_type = casing_type
73 self.verbose = verbose
74 self.day_of_week_start_index_zero = day_of_week_start_index_zero
75 self.locale_location = locale_location
76
77 if not locale_code:
78 # Autodetect
79 code, _encoding = locale.getlocale()
80 if not code:
81 warnings.warn(
82 "No system locale set. Falling back to 'en_US'. "
83 "Set LANG/LC_ALL or pass locale_code to override.",
84 stacklevel=2,
85 )
86 code = "en_US"
87 self.locale_code = code
88 else:
89 self.locale_code = locale_code
90
91 if use_24hour_time_format is None:
92 # Autodetect
93 self.use_24hour_time_format = self.locale_code not in self._twelve_hour_locales
94 else:
95 self.use_24hour_time_format = use_24hour_time_format