# Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License, version 2.0, as # published by the Free Software Foundation. # # This program is also distributed with certain software (including # but not limited to OpenSSL) that is licensed under separate terms, # as designated in a particular file or component or in included license # documentation. The authors of MySQL hereby grant you an # additional permission to link the program and your derivative works # with the separately licensed software that they have included with # MySQL. # # Without limiting anything contained in the foregoing, this file, # which is part of MySQL Connector/Python, is also subject to the # Universal FOSS Exception, version 1.0, a copy of which can be found at # http://oss.oracle.com/licenses/universal-foss-exception. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License, version 2.0, for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """Implements parser to parse MySQL option files. """ import codecs import io import os import re from .catch23 import PY2 from .constants import DEFAULT_CONFIGURATION, CNX_POOL_ARGS # pylint: disable=F0401 if PY2: from ConfigParser import SafeConfigParser, MissingSectionHeaderError else: from configparser import (ConfigParser as SafeConfigParser, MissingSectionHeaderError) # pylint: enable=F0401 DEFAULT_EXTENSIONS = { 'nt': ('ini', 'cnf'), 'posix': ('cnf',) } def read_option_files(**config): """ 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 = option_parser.get_groups_as_dict_with_priority( *groups) config_options = {} for group in groups: try: for option, value in config_from_file[group].items(): try: if option == 'socket': option = 'unix_socket' if (option not in CNX_POOL_ARGS and option != 'failover'): # pylint: disable=W0104 DEFAULT_CONFIGURATION[option] # pylint: enable=W0104 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("Unsupported argument " "'{0}'".format(option)) except KeyError: continue not_evaluate = ('password', 'passwd') for option, value in config_options.items(): if option not in config: try: if option in not_evaluate: config[option] = value[0] else: config[option] = eval(value[0]) # pylint: disable=W0123 except (NameError, SyntaxError): config[option] = value[0] return config class MySQLOptionsParser(SafeConfigParser): # pylint: disable=R0901 """This class implements methods to parse MySQL option files""" def __init__(self, files=None, keep_dashes=True): # pylint: disable=W0231 """Initialize If defaults is True, default option files are read first Raises ValueError if defaults is set to True but defaults files cannot be found. """ # Regular expression to allow options with no value(For Python v2.6) self.OPTCRE = re.compile( # pylint: disable=C0103 r'(?P