Mercurial > hg > Lemuriformes
comparison lemuriformes/url2sql.py @ 17:4793f99b73e0
[lemuriformes] utility functions
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 17:42:52 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
16:9b1bb9eee962 | 17:4793f99b73e0 |
---|---|
1 """ | |
2 instantiate a SQL interface from a URL | |
3 """ | |
4 | |
5 from urlparse import urlparse | |
6 | |
7 # Local imports of types; messy, currently | |
8 from .db import MySQLConnection | |
9 from .cli import ConfigurationParser | |
10 from .csv2sqlite import SQLiteConnection | |
11 | |
12 sql_types = {'mysql': MySQLConnection, | |
13 'sqlite': SQLiteConnection | |
14 } | |
15 | |
16 ports = {'mysql': 3306, | |
17 } | |
18 | |
19 | |
20 def url2sql(url): | |
21 """instantiate SQL connection based on URL""" | |
22 | |
23 # parse URL | |
24 parsed = urlparse(url) | |
25 | |
26 if parsed.scheme not in sql_types: | |
27 raise AssertionError("Unsupported SQL connector type: '{}'".format(parsed.scheme)) | |
28 | |
29 default_port = ports.get(parsed.scheme) | |
30 | |
31 # instantiate MySQL connection | |
32 if parsed.scheme == 'mysql': | |
33 conn_data = dict(host=parsed.hostname, | |
34 user=parsed.username, | |
35 password=parsed.password, | |
36 port=parsed.port or default_port, | |
37 db=parsed.path.strip('/')) | |
38 elif parsed.scheme == 'sqlite': | |
39 conn_data = dict(db_file=parsed.path) | |
40 return sql_types[parsed.scheme](**conn_data) |