in stk-sample/lambda/stk-player-events-loader-mysql/package/mysql/connector/abstracts.py [0:0]
def config(self, **kwargs):
"""Configure the MySQL Connection
This method allows you to configure the MySQLConnection instance.
Raises on errors.
"""
config = kwargs.copy()
if 'dsn' in config:
raise errors.NotSupportedError("Data source name is not supported")
# Read option files
self._read_option_files(config)
# Configure how we handle MySQL warnings
try:
self.get_warnings = config['get_warnings']
del config['get_warnings']
except KeyError:
pass # Leave what was set or default
try:
self.raise_on_warnings = config['raise_on_warnings']
del config['raise_on_warnings']
except KeyError:
pass # Leave what was set or default
# Configure client flags
try:
default = ClientFlag.get_default()
self.set_client_flags(config['client_flags'] or default)
del config['client_flags']
except KeyError:
pass # Missing client_flags-argument is OK
try:
if config['compress']:
self._compress = True
self.set_client_flags([ClientFlag.COMPRESS])
except KeyError:
pass # Missing compress argument is OK
allow_local_infile = config.get(
'allow_local_infile', DEFAULT_CONFIGURATION['allow_local_infile'])
if allow_local_infile:
self.set_client_flags([ClientFlag.LOCAL_FILES])
else:
self.set_client_flags([-ClientFlag.LOCAL_FILES])
try:
if not config['consume_results']:
self._consume_results = False
else:
self._consume_results = True
except KeyError:
self._consume_results = False
# Configure auth_plugin
try:
self._auth_plugin = config['auth_plugin']
del config['auth_plugin']
except KeyError:
self._auth_plugin = ''
# Configure character set and collation
if 'charset' in config or 'collation' in config:
try:
charset = config['charset']
del config['charset']
except KeyError:
charset = None
try:
collation = config['collation']
del config['collation']
except KeyError:
collation = None
self._charset_id = CharacterSet.get_charset_info(charset,
collation)[0]
# Set converter class
try:
self.set_converter_class(config['converter_class'])
except KeyError:
pass # Using default converter class
except TypeError:
raise AttributeError("Converter class should be a subclass "
"of conversion.MySQLConverterBase.")
# Compatible configuration with other drivers
compat_map = [
# (<other driver argument>,<translates to>)
('db', 'database'),
('username', 'user'),
('passwd', 'password'),
('connect_timeout', 'connection_timeout'),
('read_default_file', 'option_files'),
]
for compat, translate in compat_map:
try:
if translate not in config:
config[translate] = config[compat]
del config[compat]
except KeyError:
pass # Missing compat argument is OK
# Configure login information
if 'user' in config or 'password' in config:
try:
user = config['user']
del config['user']
except KeyError:
user = self._user
try:
password = config['password']
del config['password']
except KeyError:
password = self._password
self.set_login(user, password)
# Configure host information
if 'host' in config and config['host']:
self._host = config['host']
# Check network locations
try:
self._port = int(config['port'])
del config['port']
except KeyError:
pass # Missing port argument is OK
except ValueError:
raise errors.InterfaceError(
"TCP/IP port number should be an integer")
if "ssl_disabled" in config:
self._ssl_disabled = config.pop("ssl_disabled")
# Other configuration
set_ssl_flag = False
for key, value in config.items():
try:
DEFAULT_CONFIGURATION[key]
except KeyError:
raise AttributeError("Unsupported argument '{0}'".format(key))
# SSL Configuration
if key.startswith('ssl_'):
set_ssl_flag = True
self._ssl.update({key.replace('ssl_', ''): value})
elif key.startswith('tls_'):
set_ssl_flag = True
self._ssl.update({key: value})
else:
attribute = '_' + key
try:
setattr(self, attribute, value.strip())
except AttributeError:
setattr(self, attribute, value)
if set_ssl_flag:
if 'verify_cert' not in self._ssl:
self._ssl['verify_cert'] = \
DEFAULT_CONFIGURATION['ssl_verify_cert']
if 'verify_identity' not in self._ssl:
self._ssl['verify_identity'] = \
DEFAULT_CONFIGURATION['ssl_verify_identity']
# Make sure both ssl_key/ssl_cert are set, or neither (XOR)
if 'ca' not in self._ssl or self._ssl['ca'] is None:
self._ssl['ca'] = ""
if bool('key' in self._ssl) != bool('cert' in self._ssl):
raise AttributeError(
"ssl_key and ssl_cert need to be both "
"specified, or neither."
)
# Make sure key/cert are set to None
elif not set(('key', 'cert')) <= set(self._ssl):
self._ssl['key'] = None
self._ssl['cert'] = None
elif (self._ssl['key'] is None) != (self._ssl['cert'] is None):
raise AttributeError(
"ssl_key and ssl_cert need to be both "
"set, or neither."
)
if "tls_versions" in self._ssl and \
self._ssl["tls_versions"] is not None:
if self._ssl_disabled:
raise AttributeError("The tls_versions option can not be "
"used along with ssl_disabled.")
self._validate_tls_versions()
if "tls_ciphersuites" in self._ssl and self._ssl["tls_ciphersuites"] is not None:
if self._ssl_disabled:
raise AttributeError("The tls_ciphersuites option can not "
"be used along with ssl_disabled.")
self._validate_tls_ciphersuites()
if self._conn_attrs is None:
self._conn_attrs = {}
elif not isinstance(self._conn_attrs, dict):
raise errors.InterfaceError('conn_attrs must be of type dict.')
else:
for attr_name in self._conn_attrs:
if attr_name in CONN_ATTRS_DN:
continue
# Validate name type
if not isinstance(attr_name, STRING_TYPES):
raise errors.InterfaceError(
"Attribute name should be a string, found: '{}' in '{}'"
"".format(attr_name, self._conn_attrs))
# Validate attribute name limit 32 characters
if len(attr_name) > 32:
raise errors.InterfaceError(
"Attribute name '{}' exceeds 32 characters limit size."
"".format(attr_name))
# Validate names in connection attributes cannot start with "_"
if attr_name.startswith("_"):
raise errors.InterfaceError(
"Key names in connection attributes cannot start with "
"'_', found: '{}'".format(attr_name))
# Validate value type
attr_value = self._conn_attrs[attr_name]
if not isinstance(attr_value, STRING_TYPES):
raise errors.InterfaceError(
"Attribute '{}' value: '{}' must be a string type."
"".format(attr_name, attr_value))
# Validate attribute value limit 1024 characters
if len(attr_value) > 1024:
raise errors.InterfaceError(
"Attribute '{}' value: '{}' exceeds 1024 characters "
"limit size".format(attr_name, attr_value))
if self._client_flags & ClientFlag.CONNECT_ARGS:
self._add_default_conn_attrs()