Created
July 16, 2014 07:46
-
-
Save huyx/06fdeb5f4a7af5a0dca4 to your computer and use it in GitHub Desktop.
parseValue, parseMysqlURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ast import literal_eval | |
import urlparse | |
def parseValue(value): | |
try: | |
return literal_eval(value) | |
except: | |
pass | |
try: | |
return literal_eval('"%s"' % value) | |
except: | |
pass | |
return value | |
def parseMySQLUrl(url): | |
url = urlparse.urlparse(url) | |
database = url.path.strip('/') | |
config = { | |
'user': url.username, | |
'passwd': url.password, | |
'host': url.hostname or 'localhost', | |
'port': url.port or 3306, | |
'database': database, | |
} | |
query = urlparse.parse_qs(url.query) | |
for name, value in query.iteritems(): | |
config[name] = parseValue(value[0]) | |
return config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment