-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_helpers.py
More file actions
36 lines (29 loc) · 1.23 KB
/
config_helpers.py
File metadata and controls
36 lines (29 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import configparser
# Global variable to hold the single, central text configuration
GLOBAL_TEXT_CONFIG = None
def set_global_text_config(config_object):
"""
Sets the global text config object for all modules to use.
This is called from main_experiment.py.
"""
global GLOBAL_TEXT_CONFIG
GLOBAL_TEXT_CONFIG = config_object
def get_text_with_newlines(section, option, default=None):
"""
Retrieves text from the GLOBAL config, converts escaped newlines (\n),
and provides a default if the option is not found.
This version correctly handles special UTF-8 characters.
"""
global GLOBAL_TEXT_CONFIG
if GLOBAL_TEXT_CONFIG is None:
print("Error in config_helpers: GLOBAL_TEXT_CONFIG has not been set.")
return default if default is not None else "CONFIG_ERROR"
try:
text_content = GLOBAL_TEXT_CONFIG.get(section, option, raw=True)
return text_content.replace('\\n', '\n')
except (configparser.NoOptionError, configparser.NoSectionError):
if default is not None:
return default
else:
print(f"Error: Missing config text for [{section}] -> {option}")
return f"MISSING_TEXT: [{section}] {option}"