~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: William Grant
  • Date: 2009-04-28 05:06:00 UTC
  • Revision ID: grantw@unimelb.edu.au-20090428050600-hogd9d6wo7ksyqy8
ivle.database.get_store() now takes a configuration object.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
                         Reference, ReferenceSet, Bool, Storm, Desc
32
32
from storm.exceptions import NotOneError, IntegrityError
33
33
 
34
 
import ivle.conf
35
34
from ivle.worksheet.rst import rst
36
35
 
37
36
__all__ = ['get_store',
51
50
                % (self.__class__.__name__, k))
52
51
        setattr(self, k, v)
53
52
 
54
 
def get_conn_string():
55
 
    """
56
 
    Returns the Storm connection string, generated from the conf file.
 
53
def get_conn_string(config):
 
54
    """Create a Storm connection string to the IVLE database
 
55
 
 
56
    @param config: The IVLE configuration.
57
57
    """
58
58
 
59
59
    clusterstr = ''
60
 
    if ivle.conf.db_user:
61
 
        clusterstr += ivle.conf.db_user
62
 
        if ivle.conf.db_password:
63
 
            clusterstr += ':' + ivle.conf.db_password
 
60
    if config['database']['username']:
 
61
        clusterstr += config['database']['username']
 
62
        if config['database']['password']:
 
63
            clusterstr += ':' + config['database']['password']
64
64
        clusterstr += '@'
65
65
 
66
 
    host = ivle.conf.db_host or 'localhost'
67
 
    port = ivle.conf.db_port or 5432
 
66
    host = config['database']['host'] or 'localhost'
 
67
    port = config['database']['port'] or 5432
68
68
 
69
69
    clusterstr += '%s:%d' % (host, port)
70
70
 
71
 
    return "postgres://%s/%s" % (clusterstr, ivle.conf.db_dbname)
72
 
 
73
 
def get_store():
74
 
    """
75
 
    Open a database connection and transaction. Return a storm.store.Store
76
 
    instance connected to the configured IVLE database.
77
 
    """
78
 
    return Store(create_database(get_conn_string()))
 
71
    return "postgres://%s/%s" % (clusterstr, config['database']['name'])
 
72
 
 
73
def get_store(config):
 
74
    """Create a Storm store connected to the IVLE database.
 
75
 
 
76
    @param config: The IVLE configuration.
 
77
    """
 
78
    return Store(create_database(get_conn_string(config)))
79
79
 
80
80
# USERS #
81
81