1
+ # -*- coding: utf-8 -*-
2
+ import json
3
+ import os
4
+ from pathlib import Path
5
+
1
6
config = {}
2
7
3
8
config_mapping = {
4
- ("useragent" , "general.useragent" , "agent" , "ua" ): "string|USERAGENT " ,
9
+ ("useragent" , "general.useragent" , "USERAGENT" ): "string|general.useragent " ,
5
10
(
6
11
"details" ,
7
12
"general.review.details" ,
8
- "use_detailed_report" ): "bool|USE_DETAILED_REPORT" ,
13
+ "use_detailed_report" ,
14
+ "USE_DETAILED_REPORT" ): "bool|general.review.details" ,
9
15
(
10
16
"improve-only" ,
11
17
"general.review.improve-only" ,
12
- "review_show_improvements_only" ): "bool|REVIEW_SHOW_IMPROVEMENTS_ONLY" ,
18
+ "review_show_improvements_only" ,
19
+ "REVIEW_SHOW_IMPROVEMENTS_ONLY" ): "bool|general.review.improve-only" ,
13
20
(
14
21
"timeout" ,
15
22
"general.request.timeout" ,
16
- "http_request_timeout" ): "int|HTTP_REQUEST_TIMEOUT" ,
23
+ "http_request_timeout" ,
24
+ "HTTP_REQUEST_TIMEOUT" ): "int|general.request.timeout" ,
17
25
(
18
26
"cache" ,
19
27
"general.cache.use" ,
20
- "cache_when_possible" ): "bool|general.cache.use" ,
28
+ "cache_when_possible" ,
29
+ "CACHE_WHEN_POSSIBLE" ): "bool|general.cache.use" ,
21
30
(
22
31
"cachetime" ,
23
32
"general.cache.interval"
29
38
(
30
39
"dnsserver" ,
31
40
"general.dns.address" ,
32
- "dns_server" ): "string|DNS_SERVER" ,
41
+ "dns_server" ,
42
+ "DNS_SERVER" ): "string|general.dns.address" ,
33
43
(
34
44
"githubkey" ,
35
45
"github.api.key" ,
36
- "github_api_key" ): "string|GITHUB_API_KEY" ,
46
+ "github_api_key" ,
47
+ "GITHUB_API_KEY" ): "string|github.api.key" ,
37
48
(
38
49
"webbkollsleep" ,
39
50
"tests.webbkoll.sleep" ,
40
- "webbkoll_sleep" ): "int|WEBBKOLL_SLEEP" ,
51
+ "webbkoll_sleep" ,
52
+ "WEBBKOLL_SLEEP" ): "int|tests.webbkoll.sleep" ,
41
53
(
42
54
"tests.w3c.group" ,
43
55
"tests.css.group" ,
44
- "css_review_group_errors" ): "bool|CSS_REVIEW_GROUP_ERRORS" ,
56
+ "css_review_group_errors" ,
57
+ "CSS_REVIEW_GROUP_ERRORS" ): "bool|tests.css.group" ,
45
58
(
46
59
"sitespeeddocker" ,
47
60
"tests.sitespeed.docker.use" ,
48
- "sitespeed_use_docker" ): "bool|SITESPEED_USE_DOCKER" ,
61
+ "sitespeed_use_docker" ,
62
+ "SITESPEED_USE_DOCKER" ): "bool|tests.sitespeed.docker.use" ,
49
63
(
50
64
"sitespeedtimeout" ,
51
65
"tests.sitespeed.timeout" ,
52
- "sitespeed_timeout" ): "int|SITESPEED_TIMEOUT" ,
66
+ "sitespeed_timeout" ,
67
+ "SITESPEED_TIMEOUT" ): "int|tests.sitespeed.timeout" ,
53
68
(
54
69
"sitespeediterations" ,
55
70
"tests.sitespeed.iterations" ,
56
- "sitespeed_iterations" ): "int|SITESPEED_ITERATIONS" ,
71
+ "sitespeed_iterations" ,
72
+ "SITESPEED_ITERATIONS" ): "int|tests.sitespeed.iterations" ,
57
73
(
58
74
"csponly" ,
59
75
"tests.http.csp-only" ,
60
- "csp_only" ): "bool|CSP_ONLY" ,
76
+ "csp_only" ,
77
+ "CSP_ONLY" ): "bool|tests.http.csp-only" ,
61
78
(
62
79
"stealth" ,
63
80
"tests.software.stealth.use" ,
64
- "software_use_stealth" ): "bool|SOFTWARE_USE_STEALTH" ,
81
+ "software_use_stealth" ,
82
+ "SOFTWARE_USE_STEALTH" ): "bool|tests.software.stealth.use" ,
65
83
(
66
84
"advisorydatabase" ,
67
85
"tests.software.advisory.path" ,
68
- "software_github_adadvisory_database_path"
69
- ): "string|SOFTWARE_GITHUB_ADADVISORY_DATABASE_PATH" ,
86
+ "software_github_adadvisory_database_path" ,
87
+ "SOFTWARE_GITHUB_ADADVISORY_DATABASE_PATH"
88
+ ): "string|tests.software.advisory.path" ,
70
89
(
71
90
"browser" ,
72
91
"tests.software.browser" ,
73
- "software_browser" ): "string|SOFTWARE_BROWSER" ,
92
+ "software_browser" ,
93
+ "SOFTWARE_BROWSER" ): "string|tests.software.browser" ,
74
94
(
75
95
"mailport25" ,
76
96
"tests.email.support.port25" ,
77
- "email_network_support_port25_traffic" ): "bool|EMAIL_NETWORK_SUPPORT_PORT25_TRAFFIC" ,
97
+ "email_network_support_port25_traffic" ,
98
+ "EMAIL_NETWORK_SUPPORT_PORT25_TRAFFIC" ): "bool|tests.email.support.port25" ,
78
99
(
79
100
"mailipv6" ,
80
101
"tests.email.support.ipv6" ,
81
- "email_network_support_ipv6_traffic" ): "bool|EMAIL_NETWORK_SUPPORT_IPV6_TRAFFIC"
102
+ "email_network_support_ipv6_traffic" ,
103
+ "EMAIL_NETWORK_SUPPORT_IPV6_TRAFFIC" ): "bool|tests.email.support.ipv6"
82
104
}
83
105
84
106
@@ -92,25 +114,30 @@ def get_config(name):
92
114
Returns:
93
115
The configuration value if found, otherwise None.
94
116
"""
117
+
118
+ if '.' not in name :
119
+ # Try translate old settings name to new
120
+ config_name = get_setting_name (name )
121
+ if config_name is None :
122
+ print (f'Warning: { name } uses old settings format and is not a known setting' )
123
+ return None
124
+
125
+ config_name_pair = config_name .split ('|' )
126
+ name = config_name_pair [1 ]
127
+
95
128
# Lets see if we have it from terminal or in cache
96
129
name = name .lower ()
97
130
if name in config :
98
131
return config [name ]
99
132
100
133
# Try get config from our configuration file
101
- value = get_config_from_module (name , 'config' )
102
- if value is not None :
103
- config [name ] = value
104
- return value
105
-
106
- name = name .upper ()
107
- value = get_config_from_module (name , 'config' )
134
+ value = get_config_from_module (name , 'settings.json' )
108
135
if value is not None :
109
136
config [name ] = value
110
137
return value
111
138
112
139
# do we have fallback value we can use in our defaults/config.py file?
113
- value = get_config_from_module (name , 'defaults.config ' )
140
+ value = get_config_from_module (name , f 'defaults{ os . path . sep } settings.json ' )
114
141
if value is not None :
115
142
config [name ] = value
116
143
return value
@@ -139,17 +166,27 @@ def get_config_from_module(config_name, module_name):
139
166
Returns:
140
167
The configuration value associated with the given config_name and module_name.
141
168
"""
142
- # do we have fallback value we can use in our defaults/config.py file?
143
- try :
144
- from importlib import import_module # pylint: disable=import-outside-toplevel
145
- tmp_config = import_module (module_name ) # pylint: disable=invalid-name
146
- if hasattr (tmp_config , config_name ):
147
- return getattr (tmp_config , config_name )
148
- except ModuleNotFoundError :
149
- _ = 1
150
169
170
+ base_directory = Path (os .path .dirname (
171
+ os .path .realpath (__file__ )) + os .path .sep ).parent
172
+
173
+ file_path = f'{ base_directory } { os .path .sep } { module_name } '
174
+ if not os .path .isfile (file_path ):
175
+ return None
176
+
177
+ with open (file_path , encoding = 'utf-8' ) as json_file :
178
+ module_config = json .load (json_file )
179
+
180
+ config_path = config_name .split ('.' )
181
+ config_section = module_config
182
+ for section_name in config_path :
183
+ if section_name in config_section :
184
+ config_section = config_section [section_name ]
185
+ if not isinstance (config_section , dict ):
186
+ return config_section
151
187
return None
152
188
189
+
153
190
def set_config_from_cmd (arg ):
154
191
"""
155
192
Set configuration settings based on user input.
@@ -171,13 +208,7 @@ def set_config_from_cmd(arg):
171
208
if nof_pair > 1 :
172
209
value = pair [1 ]
173
210
174
- config_name = None
175
-
176
- for aliases , setting_name in config_mapping .items ():
177
- if name in aliases :
178
- config_name = setting_name
179
- break
180
-
211
+ config_name = get_setting_name (name )
181
212
if config_name is None :
182
213
return False
183
214
@@ -198,6 +229,18 @@ def set_config_from_cmd(arg):
198
229
return True
199
230
return False
200
231
232
+ def get_setting_name (name ):
233
+ config_name = None
234
+
235
+ for aliases , setting_name in config_mapping .items ():
236
+ if name in aliases :
237
+ config_name = setting_name
238
+ break
239
+
240
+ if config_name is None :
241
+ return None
242
+ return config_name
243
+
201
244
202
245
def handle_cmd_bool_value (setting_name , value ):
203
246
"""
@@ -251,3 +294,17 @@ def handle_cmd_str_value(_, value):
251
294
str: The original input string value.
252
295
"""
253
296
return value
297
+
298
+ def get_used_configuration ():
299
+ """
300
+ Returns a copy of the currently used configuration.
301
+
302
+ This function retrieves the current configuration settings and returns a shallow copy
303
+ of the configuration dictionary. The returned dictionary contains the same key-value pairs
304
+ as the original configuration, allowing you to work with a snapshot of the configuration
305
+ without modifying the original data.
306
+
307
+ Returns:
308
+ dict: A shallow copy of the configuration dictionary.
309
+ """
310
+ return config .copy ()
0 commit comments