gateway/gwserver/configload.py
2025-03-25 00:05:01 +01:00

31 lines
No EOL
1.1 KiB
Python

import configparser
import os
import re
def load_config():
config = configparser.ConfigParser(
comment_prefixes=('#', ';'), # Define comment prefixes
inline_comment_prefixes=('#', ';') # Enable inline comments
)
config_path = os.path.join(os.path.dirname(__file__), 'config.ini')
if not os.path.exists(config_path):
exit("No config file")
config.read(config_path)
# Clean up any comment text that might have been included in values
for section in config.sections():
for key in config[section]:
# Get the original value
value = config[section][key]
# Remove inline comments if they exist
# This regex looks for the first occurrence of a comment character that's not escaped
comment_match = re.search(r'(?<!\\)[;#]', value)
if comment_match:
# Keep only the part before the comment
clean_value = value[:comment_match.start()].strip()
config[section][key] = clean_value
return config