Skip to content

Commit e77b473

Browse files
committed
feat(config): add environment variable parsing for configuration
- Introduced a new function `parse_env` to recursively expand $env:VARIABLE_NAME patterns in strings, dicts, and lists. - Updated `get_config` to utilize `parse_env` for normalizing configuration values.
1 parent 3988ff7 commit e77b473

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/core/config.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import logging
2+
import os
3+
import re
24
import shutil
35
import settings
46
from os import path, makedirs
@@ -75,6 +77,23 @@ def get_stylesheet_path() -> str:
7577
return DEFAULT_STYLES_PATH
7678

7779

80+
def parse_env(obj):
81+
"""
82+
Recursively expand $env:VARIABLE_NAME patterns in strings,
83+
dicts, and lists.
84+
"""
85+
if isinstance(obj, dict):
86+
return {k: parse_env(v) for k, v in obj.items()}
87+
elif isinstance(obj, list):
88+
return [parse_env(item) for item in obj]
89+
elif isinstance(obj, str):
90+
pattern = r'\$env:([\w_]+)'
91+
for var in re.findall(pattern, obj):
92+
val = os.environ.get(var, '')
93+
obj = obj.replace(f'$env:{var}', val)
94+
return obj
95+
96+
7897
def get_config(show_error_dialog=False) -> Union[dict, None]:
7998
config_path = get_config_path()
8099

@@ -83,7 +102,7 @@ def get_config(show_error_dialog=False) -> Union[dict, None]:
83102
config = safe_load(yaml_stream)
84103

85104
if yaml_validator.validate(config, CONFIG_SCHEMA):
86-
return yaml_validator.normalized(config)
105+
return parse_env(yaml_validator.normalized(config))
87106
else:
88107
pretty_errors = dump(yaml_validator.errors)
89108
logging.error(f"The config file '{config_path}' contains validation errors. Please fix:\n{pretty_errors}")
@@ -107,8 +126,6 @@ def get_stylesheet(show_error_dialog=False) -> Union[str, None]:
107126
try:
108127
css_processor = CSSProcessor(styles_path)
109128
css_content = css_processor.process()
110-
#parser = CSSParser(raiseExceptions=True, parseComments=False)
111-
#css_final = parser.parseString(css_content).cssText.decode('utf-8')
112129
return css_content
113130

114131
except SyntaxErr as e:

0 commit comments

Comments
 (0)