# MySQL Connector/Python - MySQL driver written in Python. # Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. # MySQL Connector/Python is licensed under the terms of the GPLv2 # , like most # MySQL Connectors. There are special exceptions to the terms and # conditions of the GPLv2 as it is applied to this software, see the # FOSS License Exception # . # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation. # # 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 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, CNX_FABRIC_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 = {} fabric_options = {} for group in groups: try: for option, value in config_from_file[group].items(): try: if option == 'socket': option = 'unix_socket' if option in CNX_FABRIC_ARGS: if (option not in fabric_options or fabric_options[option][1] <= value[1]): fabric_options[option] = value continue if (option not in CNX_POOL_ARGS and option not in ['fabric', '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 is '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] if fabric_options: config['fabric'] = {} for option, value in fabric_options.items(): try: # pylint: disable=W0123 config['fabric'][option.split('_', 1)[1]] = eval(value[0]) # pylint: enable=W0123 except (NameError, SyntaxError): config['fabric'][option.split('_', 1)[1]] = 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