From 0def259861c8a4c82f7997556f8ffe66bcfc0215 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Fri, 21 Jun 2024 18:39:00 -0500 Subject: [PATCH] Adjust for YAML config file; boolean settings are actually boolean --- src/pgwui_common/check_settings.py | 22 ++++++++++------------ src/pgwui_common/checkset.py | 19 +++++++------------ tests/test_check_settings.py | 12 ++++++------ tests/test_checkset.py | 4 ++-- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/pgwui_common/check_settings.py b/src/pgwui_common/check_settings.py index 8330046..2ef7acd 100644 --- a/src/pgwui_common/check_settings.py +++ b/src/pgwui_common/check_settings.py @@ -24,7 +24,6 @@ ''' import re -from ast import literal_eval from . import constants from . import exceptions as ex @@ -49,15 +48,11 @@ def require_setting(errors, setting, pgwui_settings, formatter): def boolean_setting(errors, setting, pgwui_settings): - if setting in pgwui_settings: - try: - val = literal_eval(pgwui_settings[setting]) - except ValueError: - val = None - if (val is not True - and val is not False): - errors.append(ex.NotBooleanSettingError( - key_to_ini(setting), pgwui_settings[setting])) + val = pgwui_settings.get(setting, True) + if (val is not True + and val is not False): + errors.append(ex.NotBooleanSettingError( + key_to_ini(setting), pgwui_settings[setting])) def validate_setting_values(errors, settings): @@ -87,11 +82,11 @@ def validate_setting_values(errors, settings): def do_validate_hmac(settings): - '''True unless the user has specificly rejected hmac validation + '''True unless the user has specifically rejected hmac validation ''' pgwui_settings = settings['pgwui'] return ('validate_hmac' not in pgwui_settings - or literal_eval(pgwui_settings['validate_hmac'])) + or pgwui_settings['validate_hmac'] is True) def validate_hmac(errors, settings): @@ -235,6 +230,9 @@ def validate_page_setting(errors, settings, page_key): def validate_settings(errors, settings): '''Validate all core settings ''' + # This is different from all the other component's check_settings(). + # This takes all the settings, whereas the others take only their + # component's settings. validate_setting_values(errors, settings) validate_hmac(errors, settings) validate_page_setting(errors, settings, 'home_page') diff --git a/src/pgwui_common/checkset.py b/src/pgwui_common/checkset.py index d696541..238939b 100644 --- a/src/pgwui_common/checkset.py +++ b/src/pgwui_common/checkset.py @@ -1,4 +1,5 @@ -# Copyright (C) 2020, 2021 The Meme Factory, Inc. http://www.karlpinc.com/ +# Copyright (C) 2020, 2021, 2024 The Meme Factory, Inc. +# http://www.karlpinc.com/ # This file is part of PGWUI_Common. # @@ -22,8 +23,6 @@ '''Helper routines for checking a PGWUI component's settings ''' -from ast import literal_eval - from . import exceptions @@ -48,15 +47,11 @@ def unknown_settings(component, settings, conf): def boolean_settings(component, booleans, conf): errors = [] for setting in booleans: - if setting in conf: - try: - val = literal_eval(conf[setting]) - except ValueError: - val = None - if (val is not True - and val is not False): - errors.append(exceptions.NotBooleanSettingError( - '{}:{}'.format(component, setting), conf[setting])) + val = conf.get(setting, True) + if (val is not True + and val is not False): + errors.append(exceptions.NotBooleanSettingError( + '{}:{}'.format(component, setting), conf[setting])) return errors diff --git a/tests/test_check_settings.py b/tests/test_check_settings.py index d4fbe9a..c928b47 100644 --- a/tests/test_check_settings.py +++ b/tests/test_check_settings.py @@ -85,17 +85,17 @@ def test_boolean_setting_missing(): def test_boolean_setting_true(): - '''Does nothing when the setting is "True"''' + '''Does nothing when the setting is True''' errors = [] - check_settings.boolean_setting(errors, 'key', {'key': 'True'}) + check_settings.boolean_setting(errors, 'key', {'key': True}) assert errors == [] def test_boolean_setting_false(): - '''Does nothing when the setting is "False"''' + '''Does nothing when the setting is False''' errors = [] - check_settings.boolean_setting(errors, 'key', {'key': 'False'}) + check_settings.boolean_setting(errors, 'key', {'key': False}) assert errors == [] @@ -149,14 +149,14 @@ def test_do_validate_hmac_none(): def test_do_validate_hmac_True(): '''Require hmac validation when pgwui.validate_hmac is True''' result = check_settings.do_validate_hmac( - {'pgwui': {'validate_hmac': 'True'}}) + {'pgwui': {'validate_hmac': True}}) assert result is True def test_do_validate_hmac_False(): '''No hmac validation when pgwui.validate_hmac is False''' result = check_settings.do_validate_hmac( - {'pgwui': {'validate_hmac': 'False'}}) + {'pgwui': {'validate_hmac': False}}) assert result is False diff --git a/tests/test_checkset.py b/tests/test_checkset.py index 54d55e8..f43551b 100644 --- a/tests/test_checkset.py +++ b/tests/test_checkset.py @@ -87,7 +87,7 @@ def test_unknown_settings_bad(): def test_boolean_settings_good(): '''No errors when boolean settings are boolean ''' - conf = {'settinga': 'a', 'settingb': 'True', 'settingc': 'False'} + conf = {'settinga': 'a', 'settingb': True, 'settingc': False} bools = ['settingc', 'settingb'] result = checkset.boolean_settings('testcomp', bools, conf) @@ -104,7 +104,7 @@ def test_boolean_settings_bad(): result = checkset.boolean_settings('testcomp', bools, conf) - assert len(result) == 1 + assert len(result) == 2 for error in result: assert isinstance(error, ex.NotBooleanSettingError) -- 2.34.1