24
24
"""Implementing communication with MySQL servers.
25
25
"""
26
26
27
+ from io import IOBase
27
28
import os
28
- import time
29
29
import re
30
- from io import IOBase
30
+ import time
31
31
32
- from .network import MySQLUnixSocket , MySQLTCPSocket
32
+ from . import errors
33
+ from .authentication import get_auth_plugin
34
+ from .catch23 import PY2 , isstr
33
35
from .constants import (
34
36
ClientFlag , ServerCmd , CharacterSet , ServerFlag ,
35
37
flag_is_set , ShutdownType , NET_BUFFER_LENGTH
36
38
)
37
39
from .conversion import MySQLConverterBase , MySQLConverter
38
- from .protocol import MySQLProtocol
39
- from . import errors
40
- from .utils import int4store
41
40
from .cursor import (
42
41
CursorBase , MySQLCursor , MySQLCursorRaw ,
43
42
MySQLCursorBuffered , MySQLCursorBufferedRaw , MySQLCursorPrepared ,
44
43
MySQLCursorDict , MySQLCursorBufferedDict , MySQLCursorNamedTuple ,
45
44
MySQLCursorBufferedNamedTuple )
46
- from .authentication import get_auth_plugin
47
- from .catch23 import PY2 , isstr
45
+ from .network import MySQLUnixSocket , MySQLTCPSocket
46
+ from .optionfiles import MySQLOptionsParser
47
+ from .protocol import MySQLProtocol
48
+ from .utils import int4store
48
49
49
50
DEFAULT_CONFIGURATION = {
50
51
'database' : None ,
@@ -233,6 +234,59 @@ def _auth_switch_request(self, username=None, password=None):
233
234
elif packet [4 ] == 255 :
234
235
raise errors .get_exception (packet )
235
236
237
+ def _read_option_files (self , config ):
238
+ """
239
+ Read option files for connection parameters.
240
+
241
+ Checks if connection arguments contain option file arguments, and then
242
+ reads option files accordingly.
243
+ """
244
+ if 'option_files' in config :
245
+ try :
246
+ if isinstance (config ['option_groups' ], str ):
247
+ config ['option_groups' ] = [config ['option_groups' ]]
248
+ groups = config ['option_groups' ]
249
+ del config ['option_groups' ]
250
+ except KeyError :
251
+ groups = ['client' , 'connector_python' ]
252
+
253
+ if isinstance (config ['option_files' ], str ):
254
+ config ['option_files' ] = [config ['option_files' ]]
255
+ option_parser = MySQLOptionsParser (list (config ['option_files' ]),
256
+ keep_dashes = False )
257
+ del config ['option_files' ]
258
+
259
+ config_from_file = option_parser .get_groups_as_dict_with_priority (
260
+ * groups )
261
+ config_options = {}
262
+ for group in groups :
263
+ try :
264
+ for option , value in config_from_file [group ].items ():
265
+ try :
266
+ if option == 'socket' :
267
+ option = 'unix_socket'
268
+ # pylint: disable=W0104
269
+ DEFAULT_CONFIGURATION [option ]
270
+ # pylint: enable=W0104
271
+
272
+ if (option not in config_options or
273
+ config_options [option ][1 ] <= value [1 ]):
274
+ config_options [option ] = value
275
+ except KeyError :
276
+ if group is 'connector_python' :
277
+ raise AttributeError ("Unsupported argument "
278
+ "'{0}'" .format (option ))
279
+ except KeyError :
280
+ continue
281
+
282
+ for option , value in config_options .items ():
283
+ if option not in config :
284
+ try :
285
+ config [option ] = eval (value [0 ]) # pylint: disable=W0123
286
+ except (NameError , SyntaxError ):
287
+ config [option ] = value [0 ]
288
+ return config
289
+
236
290
def config (self , ** kwargs ):
237
291
"""Configure the MySQL Connection
238
292
@@ -244,6 +298,9 @@ def config(self, **kwargs):
244
298
if 'dsn' in config :
245
299
raise errors .NotSupportedError ("Data source name is not supported" )
246
300
301
+ # Read option files
302
+ self ._read_option_files (config )
303
+
247
304
# Configure how we handle MySQL warnings
248
305
try :
249
306
self .get_warnings = config ['get_warnings' ]
0 commit comments