Coverage for .tox/coverage/lib/python3.11/site-packages/wuttjamaican/db/conf.py: 100%
26 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-19 20:49 -0600
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-19 20:49 -0600
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# WuttJamaican -- Base package for Wutta Framework
5# Copyright © 2023 Lance Edgar
6#
7# This file is part of Wutta Framework.
8#
9# Wutta Framework is free software: you can redistribute it and/or modify it
10# under the terms of the GNU General Public License as published by the Free
11# Software Foundation, either version 3 of the License, or (at your option) any
12# later version.
13#
14# Wutta Framework is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17# more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24WuttJamaican - database configuration
25"""
27from collections import OrderedDict
29import sqlalchemy as sa
31from wuttjamaican.util import parse_list
34def get_engines(config, prefix):
35 """
36 Construct and return all database engines defined for a given
37 config prefix.
39 For instance if you have a config file with:
41 .. code-block:: ini
43 [wutta.db]
44 keys = default, host
45 default.url = sqlite:///tmp/default.sqlite
46 host.url = sqlite:///tmp/host.sqlite
48 And then you call this function to get those DB engines::
50 get_engines(config, 'wutta.db')
52 The result of that will be like::
54 {'default': Engine(bind='sqlite:///tmp/default.sqlite'),
55 'host': Engine(bind='sqlite:///tmp/host.sqlite')}
57 :param config: App config object.
59 :param prefix: Prefix for the config "section" which contains DB
60 connection info.
62 :returns: A dictionary of SQLAlchemy engines, with keys matching
63 those found in config.
64 """
65 app = config.get_app()
67 keys = config.get(f'{prefix}.keys', usedb=False)
68 if keys:
69 keys = parse_list(keys)
70 else:
71 keys = ['default']
73 engines = OrderedDict()
74 cfg = config.get_dict(prefix)
75 for key in keys:
76 key = key.strip()
77 try:
78 engines[key] = app.make_engine_from_config(cfg, prefix=f'{key}.')
79 except KeyError:
80 if key == 'default':
81 try:
82 engines[key] = app.make_engine_from_config(cfg, prefix='sqlalchemy.')
83 except KeyError:
84 pass
85 return engines
88def get_setting(session, name):
89 """
90 Get a setting value from the DB.
92 Note that this assumes (for now?) the DB contains a table named
93 ``setting`` with ``(name, value)`` columns.
95 :param session: App DB session.
97 :param name: Name of the setting to get.
99 :returns: Setting value as string, or ``None``.
100 """
101 sql = sa.text("select value from setting where name = :name")
102 return session.execute(sql, params={'name': name}).scalar()