]> scripts.mit.edu Git - wizard.git/blob - wizard/sql.py
Properly register salt values as random.
[wizard.git] / wizard / sql.py
1 import sqlalchemy
2 import os
3 import pkg_resources
4 import copy
5
6 from wizard import shell
7
8 # We're going to use sqlalchemy.engine.url.URL as our database
9 # info intermediate object
10
11 def connect(url):
12     """Convenience method for connecting to a MySQL database."""
13     engine = sqlalchemy.create_engine(url)
14     meta = sqlalchemy.MetaData()
15     meta.bind = engine
16     meta.reflect()
17     return meta
18
19 def auth(url):
20     """
21     If the URL has a database name but no other values, it will
22     use the global configuration, and then try the database name.
23
24     This function implements a plugin interface named
25     :ref:`wizard.sql.auth`.
26     """
27     if not url:
28         return None
29     if not url.database:
30         # it's hopeless
31         return url
32     # omitted port and query
33     if any((url.host, url.username, url.password)):
34         # don't try for defaults if a few of these were set
35         return url
36     for entry in pkg_resources.iter_entry_points("wizard.sql.auth"):
37         func = entry.load()
38         r = func(copy.copy(url))
39         print r
40         if r is not None:
41             return r
42     env_dsn = os.getenv("WIZARD_DSN")
43     if env_dsn:
44         old_url = url
45         url = sqlalchemy.engine.url.make_url(env_dsn)
46         url.database = old_url.database
47     return url