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

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""" 

26 

27from collections import OrderedDict 

28 

29import sqlalchemy as sa 

30 

31from wuttjamaican.util import parse_list 

32 

33 

34def get_engines(config, prefix): 

35 """ 

36 Construct and return all database engines defined for a given 

37 config prefix. 

38 

39 For instance if you have a config file with: 

40 

41 .. code-block:: ini 

42 

43 [wutta.db] 

44 keys = default, host 

45 default.url = sqlite:///tmp/default.sqlite 

46 host.url = sqlite:///tmp/host.sqlite 

47 

48 And then you call this function to get those DB engines:: 

49 

50 get_engines(config, 'wutta.db') 

51 

52 The result of that will be like:: 

53 

54 {'default': Engine(bind='sqlite:///tmp/default.sqlite'), 

55 'host': Engine(bind='sqlite:///tmp/host.sqlite')} 

56 

57 :param config: App config object. 

58 

59 :param prefix: Prefix for the config "section" which contains DB 

60 connection info. 

61 

62 :returns: A dictionary of SQLAlchemy engines, with keys matching 

63 those found in config. 

64 """ 

65 app = config.get_app() 

66 

67 keys = config.get(f'{prefix}.keys', usedb=False) 

68 if keys: 

69 keys = parse_list(keys) 

70 else: 

71 keys = ['default'] 

72 

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 

86 

87 

88def get_setting(session, name): 

89 """ 

90 Get a setting value from the DB. 

91 

92 Note that this assumes (for now?) the DB contains a table named 

93 ``setting`` with ``(name, value)`` columns. 

94 

95 :param session: App DB session. 

96 

97 :param name: Name of the setting to get. 

98 

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()