diff options
Diffstat (limited to 'dodai/config/databases/sa.py')
-rw-r--r-- | dodai/config/databases/sa.py | 80 |
1 files changed, 49 insertions, 31 deletions
diff --git a/dodai/config/databases/sa.py b/dodai/config/databases/sa.py index a5965a9..21999c2 100644 --- a/dodai/config/databases/sa.py +++ b/dodai/config/databases/sa.py | |||
@@ -17,31 +17,49 @@ | |||
17 | 17 | ||
18 | 18 | ||
19 | class Sa(object): | 19 | class Sa(object): |
20 | """Callable object that will wire up sqlalchemy engine and | ||
21 | session objects | ||
20 | 22 | ||
23 | Attributes: | ||
24 | create_engine: The sqlalchemy create_engine object | ||
25 | session_maker: The sqlalchemy session_maker object | ||
26 | result_object: The object that will be populated with | ||
27 | all the connection information along with | ||
28 | the sqlalchemy objects | ||
29 | |||
30 | """ | ||
21 | def __init__(self, create_engine, session_maker, result_object): | 31 | def __init__(self, create_engine, session_maker, result_object): |
22 | self._create_engine = create_engine | 32 | self._create_engine = create_engine |
23 | self._session_maker = session_maker | 33 | self._session_maker = session_maker |
24 | self._result_object = result_object | 34 | self._result_object = result_object |
25 | 35 | ||
26 | def __call__(self, name, obj): | 36 | def __call__(self, section, name): |
27 | db = self._result_object() | 37 | db = self._result_object() |
28 | db.name = name | 38 | db.name = name |
29 | db.protocol = obj.protocol | 39 | db.protocol = section['protocol'] |
30 | if hasattr(obj, 'protocol_extra') and obj.protocol_extra: | 40 | |
31 | db.protocol_extra = obj.protocol_extra | 41 | if section.has_key('protocol_extra') and section['protocol_extra']: |
32 | if hasattr(obj, 'filename') and obj.filename: | 42 | db.protocol_extra = section['protocol_extra'] |
33 | db.filename = obj.filename | 43 | |
34 | if hasattr(obj, 'port') and obj.port: | 44 | if section.has_key('filename') and section['filename']: |
35 | db.port = int(obj.port) | 45 | db.filename = section['filename'] |
36 | if hasattr(obj, 'database') and obj.database: | 46 | |
37 | db.database = obj.database | 47 | if section.has_key('port') and section['port']: |
38 | if hasattr(obj, 'schema') and obj.schema: | 48 | db.port = int(section['port']) |
39 | db.schema = obj.schema | 49 | |
40 | if hasattr(obj, 'username') and obj.username: | 50 | if section.has_key('database') and section['database']: |
41 | db.username = obj.username | 51 | db.database = section['database'] |
42 | if hasattr(obj, 'hostname') and obj.hostname: | 52 | |
43 | db.hostname = obj.hostname | 53 | if section.has_key('schema') and section['schema']: |
44 | db.engine = self._build_engine(obj) | 54 | db.schema = section['schema'] |
55 | |||
56 | if section.has_key('username') and section['username']: | ||
57 | db.username = section['username'] | ||
58 | |||
59 | if section.has_key('hostname') and section['hostname']: | ||
60 | db.hostname = section['hostname'] | ||
61 | |||
62 | db.engine = self._build_engine(section) | ||
45 | db.session = self._build_session(db.engine) | 63 | db.session = self._build_session(db.engine) |
46 | return db | 64 | return db |
47 | 65 | ||
@@ -49,25 +67,25 @@ class Sa(object): | |||
49 | session = self._session_maker(bind=engine) | 67 | session = self._session_maker(bind=engine) |
50 | return session() | 68 | return session() |
51 | 69 | ||
52 | def _build_connection_string(self, obj): | 70 | def _build_connection_string(self, section): |
53 | out = [] | 71 | out = [] |
54 | out.append('{db.protocol}') | 72 | out.append('{section[protocol]}') |
55 | if hasattr(obj, 'protocol_extra') and obj.protocol_extra: | 73 | if section.has_key('protocol_extra') and section['protocol_extra']: |
56 | out.append('+{db.protocol_extra}') | 74 | out.append('+{section[protocol_extra]}') |
57 | out.append('://') | 75 | out.append('://') |
58 | if hasattr(obj, 'filename') and obj.filename: | 76 | if section.has_key('filename') and section['filename']: |
59 | out.append('{db.filename}') | 77 | out.append('{section[filename]}') |
60 | else: | 78 | else: |
61 | out.append('{db.username}:{db.password}@') | 79 | out.append('{section[username]}:{section[password]}@') |
62 | out.append('{db.hostname}') | 80 | out.append('{section[hostname]}') |
63 | if hasattr(obj, 'port') and obj.port: | 81 | if section.has_key('port') and section['port']: |
64 | out.append(':{db.port}') | 82 | out.append(':{section[port]}') |
65 | out.append('/{db.database}') | 83 | out.append('/{section[database]}') |
66 | out = ''.join(out) | 84 | out = ''.join(out) |
67 | out = out.format(db=obj) | 85 | out = out.format(section=section) |
68 | return out | 86 | return out |
69 | 87 | ||
70 | def _build_engine(self, obj): | 88 | def _build_engine(self, section): |
71 | connection_string = self._build_connection_string(obj) | 89 | connection_string = self._build_connection_string(section) |
72 | db_obj = self._create_engine(connection_string) | 90 | db_obj = self._create_engine(connection_string) |
73 | return db_obj | 91 | return db_obj |