in mysql-connector-python/lib/mysql/connector/optionfiles.py [0:0]
def read_option_files(**config: Union[str, List[str]]) -> Dict[str, Any]:
"""
Read option files for connection parameters.
Checks if connection arguments contain option file arguments, and then
reads option files accordingly.
"""
if "option_files" in config:
try:
if isinstance(config["option_groups"], str):
config["option_groups"] = [config["option_groups"]]
groups = config["option_groups"]
del config["option_groups"]
except KeyError:
groups = ["client", "connector_python"]
if isinstance(config["option_files"], str):
config["option_files"] = [config["option_files"]]
option_parser = MySQLOptionsParser(
list(config["option_files"]), keep_dashes=False
)
del config["option_files"]
config_from_file: Dict[str, Any] = (
option_parser.get_groups_as_dict_with_priority(*groups)
)
config_options: Dict[str, Tuple[str, int, str]] = {}
for group in groups:
try:
for option, value in config_from_file[group].items():
value += (group,)
try:
if option == "socket":
option = "unix_socket"
option_parser.set(group, "unix_socket", value[0])
if option not in CNX_POOL_ARGS and option != "failover":
_ = DEFAULT_CONFIGURATION[option]
if (
option not in config_options
or config_options[option][1] <= value[1]
):
config_options[option] = value
except KeyError:
if group == "connector_python":
raise AttributeError(
f"Unsupported argument '{option}'"
) from None
except KeyError:
continue
for option, values in config_options.items():
value, _, section = values
if (
option not in config
and option_parser.has_section(section)
and option_parser.has_option(section, option)
):
if option in ("password", "passwd"): # keep the value as string
config[option] = str(value)
else:
try:
config[option] = ast.literal_eval(value)
except (ValueError, TypeError, SyntaxError):
config[option] = value
if "socket" in config:
config["unix_socket"] = config.pop("socket")
return config