Skip to content

Commit 7cd15a9

Browse files
committed
start of new settings logic
1 parent 0ac942b commit 7cd15a9

File tree

6 files changed

+112
-67
lines changed

6 files changed

+112
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ __pycache__/
55

66
playground.py
77
/config.py
8+
/settings.json
89
dbase.sqlite
910

1011
# C extensions

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ RUN echo 'ALL ALL=NOPASSWD: /usr/sbin/tc, /usr/sbin/route, /usr/sbin/ip' > /etc/
5252

5353
RUN npm install -g node-gyp puppeteer
5454

55-
# If own config.py exists it will overwrite the SAMPLE
55+
# If own settings.json exists it will overwrite the default
5656
COPY . /usr/src/app
5757

5858
RUN chown --recursive pptruser:pptruser /usr/src/app

helpers/setting_helper.py

Lines changed: 99 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
import json
3+
import os
4+
from pathlib import Path
5+
16
config = {}
27

38
config_mapping = {
4-
("useragent", "general.useragent", "agent", "ua"): "string|USERAGENT",
9+
("useragent", "general.useragent", "USERAGENT"): "string|general.useragent",
510
(
611
"details",
712
"general.review.details",
8-
"use_detailed_report"): "bool|USE_DETAILED_REPORT",
13+
"use_detailed_report",
14+
"USE_DETAILED_REPORT"): "bool|general.review.details",
915
(
1016
"improve-only",
1117
"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",
1320
(
1421
"timeout",
1522
"general.request.timeout",
16-
"http_request_timeout"): "int|HTTP_REQUEST_TIMEOUT",
23+
"http_request_timeout",
24+
"HTTP_REQUEST_TIMEOUT"): "int|general.request.timeout",
1725
(
1826
"cache",
1927
"general.cache.use",
20-
"cache_when_possible"): "bool|general.cache.use",
28+
"cache_when_possible",
29+
"CACHE_WHEN_POSSIBLE"): "bool|general.cache.use",
2130
(
2231
"cachetime",
2332
"general.cache.interval"
@@ -29,56 +38,69 @@
2938
(
3039
"dnsserver",
3140
"general.dns.address",
32-
"dns_server"): "string|DNS_SERVER",
41+
"dns_server",
42+
"DNS_SERVER"): "string|general.dns.address",
3343
(
3444
"githubkey",
3545
"github.api.key",
36-
"github_api_key"): "string|GITHUB_API_KEY",
46+
"github_api_key",
47+
"GITHUB_API_KEY"): "string|github.api.key",
3748
(
3849
"webbkollsleep",
3950
"tests.webbkoll.sleep",
40-
"webbkoll_sleep"): "int|WEBBKOLL_SLEEP",
51+
"webbkoll_sleep",
52+
"WEBBKOLL_SLEEP"): "int|tests.webbkoll.sleep",
4153
(
4254
"tests.w3c.group",
4355
"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",
4558
(
4659
"sitespeeddocker",
4760
"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",
4963
(
5064
"sitespeedtimeout",
5165
"tests.sitespeed.timeout",
52-
"sitespeed_timeout"): "int|SITESPEED_TIMEOUT",
66+
"sitespeed_timeout",
67+
"SITESPEED_TIMEOUT"): "int|tests.sitespeed.timeout",
5368
(
5469
"sitespeediterations",
5570
"tests.sitespeed.iterations",
56-
"sitespeed_iterations"): "int|SITESPEED_ITERATIONS",
71+
"sitespeed_iterations",
72+
"SITESPEED_ITERATIONS"): "int|tests.sitespeed.iterations",
5773
(
5874
"csponly",
5975
"tests.http.csp-only",
60-
"csp_only"): "bool|CSP_ONLY",
76+
"csp_only",
77+
"CSP_ONLY"): "bool|tests.http.csp-only",
6178
(
6279
"stealth",
6380
"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",
6583
(
6684
"advisorydatabase",
6785
"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",
7089
(
7190
"browser",
7291
"tests.software.browser",
73-
"software_browser"): "string|SOFTWARE_BROWSER",
92+
"software_browser",
93+
"SOFTWARE_BROWSER"): "string|tests.software.browser",
7494
(
7595
"mailport25",
7696
"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",
7899
(
79100
"mailipv6",
80101
"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"
82104
}
83105

84106

@@ -92,25 +114,30 @@ def get_config(name):
92114
Returns:
93115
The configuration value if found, otherwise None.
94116
"""
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+
95128
# Lets see if we have it from terminal or in cache
96129
name = name.lower()
97130
if name in config:
98131
return config[name]
99132

100133
# 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')
108135
if value is not None:
109136
config[name] = value
110137
return value
111138

112139
# 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')
114141
if value is not None:
115142
config[name] = value
116143
return value
@@ -139,17 +166,27 @@ def get_config_from_module(config_name, module_name):
139166
Returns:
140167
The configuration value associated with the given config_name and module_name.
141168
"""
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
150169

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
151187
return None
152188

189+
153190
def set_config_from_cmd(arg):
154191
"""
155192
Set configuration settings based on user input.
@@ -171,13 +208,7 @@ def set_config_from_cmd(arg):
171208
if nof_pair > 1:
172209
value = pair[1]
173210

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)
181212
if config_name is None:
182213
return False
183214

@@ -198,6 +229,18 @@ def set_config_from_cmd(arg):
198229
return True
199230
return False
200231

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+
201244

202245
def handle_cmd_bool_value(setting_name, value):
203246
"""
@@ -251,3 +294,17 @@ def handle_cmd_str_value(_, value):
251294
str: The original input string value.
252295
"""
253296
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()

tests/lighthouse_base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def run_test(url, strategy, category, silance, lighthouse_translations):
6262
print(global_translation('TEXT_TEST_START').format(
6363
datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
6464

65-
6665
json_content = get_json_result(
6766
lang_code,
6867
url,

update_software.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
CACHE_TIME_DELTA = get_config('CACHE_TIME_DELTA')
2121
CONFIG_WARNINGS = {}
2222

23-
try:
24-
github_adadvisory_database_path = get_config(
23+
github_adadvisory_database_path = get_config(
2524
'SOFTWARE_GITHUB_ADADVISORY_DATABASE_PATH')
26-
except:
27-
# If software_github_adadvisory_database_path variable is not set in config.py this will be the default
25+
# If software_github_adadvisory_database_path variable is not
26+
# set in config.py this will be the default
27+
if github_adadvisory_database_path == '':
2828
github_adadvisory_database_path = None
2929

3030

utils.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
from datetime import datetime
44
import traceback
5-
from helpers.setting_helper import get_config
5+
from helpers.setting_helper import get_config, get_used_configuration
66
from models import SiteTests
77
from tests.page_not_found import run_test as run_test_page_not_found
88
from tests.html_validator_w3c import run_test as run_test_html_validator_w3c
@@ -173,25 +173,13 @@ def get_error_info(url, lang_code, test_type, show_reviews, ex):
173173
f'\nTest Type(s): {test_type}',
174174
f'\nShow Reviews: {show_reviews}',
175175
'\n###############################################'
176-
'\n# Configuration (from config.py):',
177-
f"\nuseragent: {get_config('USERAGENT')}",
178-
f"\nhttp_request_timeout: {get_config('HTTP_REQUEST_TIMEOUT')}",
179-
f"\nwebbkoll_sleep: {get_config('WEBBKOLL_SLEEP')}",
180-
f"\ncss_review_group_errors: { \
181-
get_config('CSS_REVIEW_GROUP_ERRORS')}",
182-
f"\nreview_show_improvements_only: { \
183-
get_config('REVIEW_SHOW_IMPROVEMENTS_ONLY')}",
184-
f"\nsitespeed_use_docker: {get_config('SITESPEED_USE_DOCKER')}",
185-
f"\nsitespeed_iterations: {get_config('SITESPEED_ITERATIONS')}",
186-
f"\ncache_when_possible: {get_config('CACHE_WHEN_POSSIBLE')}",
187-
f"\ncache_time_delta: {get_config('CACHE_TIME_DELTA')}",
188-
f"\nsoftware_use_stealth: {get_config('SOFTWARE_USE_STEALTH')}",
189-
f"\nuse_detailed_report: {get_config('USE_DETAILED_REPORT')}",
190-
f"\ncsp_only: {get_config('CSP_ONLY')}",
191-
f"\ndns_server: {get_config('DNS_SERVER')}",
192-
f"\nsoftware_browser: {get_config('SOFTWARE_BROWSER')}",
193-
'\n###############################################\n',
176+
'\n# Used Configuration:'
194177
])
178+
config = get_used_configuration()
179+
for name, value in config.items():
180+
result.append(f"\n{name}: {value}")
181+
182+
result.append('\n###############################################\n')
195183
result.extend(traceback.format_exception(ex, ex, ex.__traceback__))
196184
result.append('###############################################\n\n')
197185
return result

0 commit comments

Comments
 (0)