Add config parser to handle quotes in default file.
This commit is contained in:
@@ -31,6 +31,7 @@ from .converters import escape_dict, escape_sequence, escape_string
|
||||
from .err import Warning, Error, InterfaceError, DataError, \
|
||||
DatabaseError, OperationalError, IntegrityError, InternalError, \
|
||||
NotSupportedError, ProgrammingError, MySQLError
|
||||
from .parsers import DefaultFileParser
|
||||
from .times import Date, Time, Timestamp, \
|
||||
DateFromTicks, TimeFromTicks, TimestampFromTicks
|
||||
|
||||
@@ -119,7 +120,7 @@ def install_as_MySQLdb():
|
||||
__all__ = [
|
||||
'BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'Date',
|
||||
'Time', 'Timestamp', 'DateFromTicks', 'TimeFromTicks', 'TimestampFromTicks',
|
||||
'DataError', 'DatabaseError', 'Error', 'FIELD_TYPE', 'IntegrityError',
|
||||
'DataError', 'DatabaseError', 'DefaultFileParser', 'Error', 'FIELD_TYPE', 'IntegrityError',
|
||||
'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER',
|
||||
'NotSupportedError', 'DBAPISet', 'OperationalError', 'ProgrammingError',
|
||||
'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Warning', 'apilevel', 'connect',
|
||||
|
||||
@@ -17,11 +17,12 @@ import traceback
|
||||
import warnings
|
||||
|
||||
from .charset import MBLENGTH, charset_by_name, charset_by_id
|
||||
from .cursors import Cursor
|
||||
from .constants import CLIENT, COMMAND, FIELD_TYPE, SERVER_STATUS
|
||||
from .util import byte2int, int2byte
|
||||
from .converters import (
|
||||
escape_item, encoders, decoders, escape_string, through)
|
||||
from .cursors import Cursor
|
||||
from .parsers import DefaultFileParser
|
||||
from .util import byte2int, int2byte
|
||||
from . import err
|
||||
|
||||
try:
|
||||
@@ -31,11 +32,6 @@ except ImportError:
|
||||
ssl = None
|
||||
SSL_ENABLED = False
|
||||
|
||||
if PY2:
|
||||
import ConfigParser as configparser
|
||||
else:
|
||||
import configparser
|
||||
|
||||
try:
|
||||
import getpass
|
||||
DEFAULT_USER = getpass.getuser()
|
||||
@@ -596,7 +592,7 @@ class Connection(object):
|
||||
if not read_default_group:
|
||||
read_default_group = "client"
|
||||
|
||||
cfg = configparser.RawConfigParser()
|
||||
cfg = DefaultFileParser()
|
||||
cfg.read(os.path.expanduser(read_default_file))
|
||||
|
||||
def _config(key, arg):
|
||||
|
||||
20
pymysql/parsers.py
Normal file
20
pymysql/parsers.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from ._compat import PY2
|
||||
|
||||
if PY2:
|
||||
import ConfigParser as configparser
|
||||
else:
|
||||
import configparser
|
||||
|
||||
|
||||
class DefaultFileParser(configparser.RawConfigParser):
|
||||
|
||||
def __remove_quotes(self, value):
|
||||
quotes = ["'", "\""]
|
||||
for quote in quotes:
|
||||
if value[0] == quote and value[-1] == quote:
|
||||
return value.strip(quote)
|
||||
return value
|
||||
|
||||
def get(self, section, option):
|
||||
value = configparser.RawConfigParser.get(self, section, option)
|
||||
return self.__remove_quotes(value)
|
||||
Reference in New Issue
Block a user