forked from cfperez/ipython-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
28 lines (25 loc) · 862 Bytes
/
Copy pathparse.py
File metadata and controls
28 lines (25 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import six
from six.moves import configparser as CP
from sqlalchemy.engine.url import URL
def parse(cell, config):
parts = [part.strip() for part in cell.split(None, 1)]
if not parts:
return {'connection': '', 'sql': ''}
if parts[0].startswith('[') and parts[0].endswith(']'):
section = parts[0].lstrip('[').rstrip(']')
parser = CP.ConfigParser()
parser.read(config.dsn_filename)
cfg_dict = dict(parser.items(section))
connection = str(URL(**cfg_dict))
sql = parts[1] if len(parts) > 1 else ''
elif '@' in parts[0] or '://' in parts[0]:
connection = parts[0]
if len(parts) > 1:
sql = parts[1]
else:
sql = ''
else:
connection = ''
sql = cell
return {'connection': connection.strip(),
'sql': sql.strip()}