Spaces:
Sleeping
Sleeping
File size: 4,883 Bytes
0fd441a a99e5f8 962ef72 a99e5f8 c6fb648 a99e5f8 7757db2 a99e5f8 0fd441a a99e5f8 |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
from configparser import ConfigParser as config
from typing import Union
from pathlib import Path
#from utils.get_arg_name import get_arg_name_as_string
import traceback
'''
##debug
import sys
from pathlib import Path
#base_grandparent = Path(__file__).resolve().parent.parent
grandparent_dir = Path('.').resolve() #.parent.parent ##unreliable
sys.path.insert(0, f"{grandparent_dir}") #\\file_handler")
##end debug
#'''
#import file_handler
from utils.file_utils import find_file
def get_config_value(config_file:Path, section_key:str, parameter:str, fallback:str=None) -> str: # configfile: Union[str, Path]="utils\\config.ini"):
""" Load config file, locate section, read parameter and return value
Args:
section_key: The section key
parameter: The parameter key to read from the configuration file
fallback: The fallback parameter if the parameter value not found
config_file: The configuration file to load.
Returns:
The key parameter value.
Raises:
RuntimeWarning: If the configuration file cannot be loaded or parameter key found.
"""
try:
#config_file = find_config(config_file)
cfg = config()
config_file = config_file if isinstance(config_file, Path) else Path(config_file)
if config_file.is_file():
cfg.read(config_file)
param_value = cfg[section_key].get(option=parameter, fallback=fallback) #"C:\\Dat\\dev\\gtk3-runtime\\bin")
return param_value
else:
raise RuntimeWarning(f"Configuration file not found: {config_file}")
except KeyError as exc:
tb = traceback.format_exc()
raise RuntimeWarning(f"Error loading parameter key: {exc}\n{tb}")
except Exception as exc:
tb = traceback.format_exc()
raise RuntimeWarning(f"Error loading config or parameter key: {exc}\n{tb}")
#pass
##debug
'''
config_file_path = find_file("config.ini") #file_handler.file_utils.
config_value = get_config_value(config_file_path, "LIBRARIES_CAP", "WEASYPRINT_DLL_DIRECTORIES")
print(f"config value: {config_value}")
'''
##SMY: moved to file_handler.file_utils as find_file()
def find_config(config_file_name: str = "config.ini") -> config: #configparser.ConfigParser:
"""
Finds and loads a configuration file named 'config_file_name' from the
same directory or a parent directory of the calling script.
Args:
config_file_name: The name of the configuration file to load.
Returns:
A ConfigParser object with the loaded configuration.
Raises:
FileNotFoundError: If the configuration file cannot be found.
"""
# Start the search from the directory of the file this function is in
search_path = Path(__file__).resolve().parent
# Walk up the directory tree until the config file is found
for parent in [search_path, *search_path.parents]:
config_path = parent / config_file_name
if config_path.is_file():
return config_path
raise FileNotFoundError(f"Configuration file '{config_file_name}' not found.")
def get_config_value_old(section:str, parameter:str, fallback:str=None, configfile: Union[str, Path]="utils\\config.ini"):
""" Load config file, locate section, read parameter and return value """
try:
cfg = config()
cfg.read(configfile)
param_value = cfg[section].get(option=parameter, fallback=fallback) #"C:\\Dat\\dev\\gtk3-runtime\\bin")
return param_value
except Exception as exc:
tb = traceback.format_exc()
raise RuntimeWarning(f"Error loading config: {exc}\n{tb}")
#pass
##TODO: //STOP
# ##SMY: HF Space RuntimeWarning: Error loading config: 'MARKER_CAP'
'''
from pathlib import Path
import configparser
from typing import Optional
def load_config(config_file_name: str = "config.ini") -> configparser.ConfigParser:
"""
Finds and loads a configuration file named 'config_file_name' from the
same directory or a parent directory of the calling script.
Args:
config_file_name: The name of the configuration file to load.
Returns:
A ConfigParser object with the loaded configuration.
Raises:
FileNotFoundError: If the configuration file cannot be found.
"""
# Start the search from the directory of the file this function is in
search_path = Path(__file__).resolve().parent
# Walk up the directory tree until the config file is found
for parent in [search_path, *search_path.parents]:
config_path = parent / config_file_name
if config_path.is_file():
config = configparser.ConfigParser()
config.read(config_path)
return config
raise FileNotFoundError(f"Configuration file '{config_file_name}' not found.")
'''
|