Skip to content

Commit a02cb0d

Browse files
committed
[CONFIG] Implements entries_color validation
> closes #160
1 parent 40fd08d commit a02cb0d

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project (partially) adheres to [Semantic Versioning](https://semver.org
88
## [Unreleased]
99
### Added
1010
- Python 3.13 & 3.14 official support
11+
- `entries_color` config option validation
1112

1213
## [v4.15.0.0] - 2024-09-30
1314
### Added

archey/colors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from archey.environment import Environment
1010

1111
# REGEXP compiled pattern matching ANSI/ECMA-48 color escape codes.
12-
ANSI_ECMA_REGEXP = re.compile(r"\x1b\[\d+(?:(?:;\d+)+)?m")
12+
ANSI_TEXT_CODES_REGEXP = re.compile(r"\d+(?:(?:;\d+)+)?")
13+
ANSI_ECMA_REGEXP = re.compile(rf"\x1b\[{ANSI_TEXT_CODES_REGEXP.pattern}m")
1314

1415

1516
class Style:

archey/configuration.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from copy import deepcopy
77
from typing import Any, Dict
88

9+
from archey.colors import ANSI_TEXT_CODES_REGEXP
910
from archey.singleton import Singleton
1011
from archey.utility import Utility
1112

@@ -51,6 +52,9 @@ def __init__(self, config_path=None):
5152
self._load_configuration(os.path.expanduser("~/.config/archey4/"))
5253
self._load_configuration(os.getcwd())
5354

55+
# Perform various validations
56+
self._validate_configuration()
57+
5458
def get(self, key: str, default=None) -> Any:
5559
"""
5660
A binding method to imitate the `dict.get()` behavior.
@@ -90,6 +94,19 @@ def _load_configuration(self, path: str) -> None:
9094
logging.ERROR if self.get("suppress_warnings") else logging.WARN
9195
)
9296

97+
def _validate_configuration(self) -> None:
98+
# entries_color
99+
entries_color = self._config.get("entries_color")
100+
if entries_color:
101+
if (
102+
not isinstance(entries_color, str)
103+
or ANSI_TEXT_CODES_REGEXP.fullmatch(entries_color) is None
104+
):
105+
logging.warning(
106+
"Couldn't validate 'entries_color' configuration option value, ignoring..."
107+
)
108+
self._config["entries_color"] = DEFAULT_CONFIG["entries_color"]
109+
93110
def __iter__(self):
94111
"""When used as an iterator, directly yield `_config` elements"""
95112
return iter(self._config.items())

archey/test/test_archey_configuration.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,45 @@ def test_load_configuration(self):
132132
},
133133
)
134134

135+
@patch(
136+
# `_load_configuration` method is mocked to "ignore" local system configurations.
137+
"archey.configuration.Configuration._load_configuration",
138+
Mock(),
139+
)
140+
def test_validate_configuration(self):
141+
"""Test configuration option values validation"""
142+
configuration = Configuration()
143+
144+
# OK
145+
with patch.dict(
146+
configuration._config, # pylint: disable=protected-access
147+
{
148+
"entries_color": "0;1;2",
149+
},
150+
):
151+
configuration._validate_configuration() # pylint: disable=protected-access
152+
self.assertEqual(configuration.get("entries_color"), "0;1;2")
153+
154+
# KO
155+
with patch.dict(
156+
configuration._config, # pylint: disable=protected-access
157+
{
158+
"entries_color": True,
159+
},
160+
):
161+
configuration._validate_configuration() # pylint: disable=protected-access
162+
self.assertEqual(configuration.get("entries_color"), "")
163+
164+
# KO
165+
with patch.dict(
166+
configuration._config, # pylint: disable=protected-access
167+
{
168+
"entries_color": "true",
169+
},
170+
):
171+
configuration._validate_configuration() # pylint: disable=protected-access
172+
self.assertEqual(configuration.get("entries_color"), "")
173+
135174
def test_instantiation_config_path(self):
136175
"""Test for configuration loading from specific user-defined path"""
137176
with tempfile.TemporaryDirectory() as temp_dir:

0 commit comments

Comments
 (0)