9
9
10
10
"""Invenio environment configuration."""
11
11
12
- from __future__ import absolute_import , print_function
13
-
14
12
import ast
15
13
import os
16
14
@@ -48,12 +46,21 @@ def init_app(self, app):
48
46
app .config [varname ] = value
49
47
50
48
51
- def _get_env_var (prefix , keys ) :
49
+ def _get_env_var (prefix : str , keys : list ) -> dict :
52
50
"""Retrieve environment variables with a given prefix."""
53
51
return {k : os .environ .get (f"{ prefix } _{ k .upper ()} " ) for k in keys }
54
52
55
53
56
- def build_db_uri ():
54
+ def _check_prefix (prefix : str ) -> str :
55
+ """Check if prefix ends with an underscore and remove it."""
56
+ if not prefix :
57
+ return "INVENIO"
58
+ if prefix .endswith ("_" ):
59
+ prefix = prefix [:- 1 ]
60
+ return prefix
61
+
62
+
63
+ def build_db_uri (prefix = "INVENIO" ):
57
64
"""
58
65
Build database URI from environment variables or use default.
59
66
@@ -66,14 +73,15 @@ def build_db_uri():
66
73
Note: For option 3, to assert that the INVENIO_DB_* settings take effect,
67
74
you need to set SQLALCHEMY_DATABASE_URI="" in your environment.
68
75
"""
76
+ prefix = _check_prefix (prefix )
69
77
default_uri = "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm"
70
78
71
- for key in ["INVENIO_SQLALCHEMY_DATABASE_URI " , "SQLALCHEMY_DATABASE_URI" ]:
79
+ for key in [f" { prefix } _SQLALCHEMY_DATABASE_URI " , "SQLALCHEMY_DATABASE_URI" ]:
72
80
if uri := os .environ .get (key ):
73
81
return uri
74
82
75
83
db_params = _get_env_var (
76
- "INVENIO_DB " , ["user" , "password" , "host" , "port" , "name" , "protocol" ]
84
+ f" { prefix } _DB " , ["user" , "password" , "host" , "port" , "name" , "protocol" ]
77
85
)
78
86
if all (db_params .values ()):
79
87
uri = f"{ db_params ['protocol' ]} ://{ db_params ['user' ]} :{ db_params ['password' ]} @{ db_params ['host' ]} :{ db_params ['port' ]} /{ db_params ['name' ]} "
@@ -82,7 +90,7 @@ def build_db_uri():
82
90
return default_uri
83
91
84
92
85
- def build_broker_url ():
93
+ def build_broker_url (prefix = "INVENIO" ):
86
94
"""
87
95
Build broker URL from environment variables or use default.
88
96
@@ -93,23 +101,25 @@ def build_broker_url():
93
101
4. Default URL
94
102
Note: see: https://docs.celeryq.dev/en/stable/userguide/configuration.html#new-lowercase-settings
95
103
"""
104
+ prefix = _check_prefix (prefix )
96
105
default_url = "amqp://guest:guest@localhost:5672/"
97
106
98
- for key in ["INVENIO_BROKER_URL " , "BROKER_URL" ]:
107
+ for key in [f" { prefix } _BROKER_URL " , "BROKER_URL" ]:
99
108
if broker_url := os .environ .get (key ):
100
109
return broker_url
101
110
102
111
broker_params = _get_env_var (
103
- "INVENIO_AMQP_BROKER " , ["user" , "password" , "host" , "port" , "protocol" ]
112
+ f" { prefix } _AMQP_BROKER " , ["user" , "password" , "host" , "port" , "protocol" ]
104
113
)
105
114
if all (broker_params .values ()):
106
- vhost = f"{ os .environ .get ('INVENIO_AMQP_BROKER_VHOST' ).removeprefix ('/' )} "
115
+ broker_env_var = f"{ prefix } _AMQP_BROKER_VHOST"
116
+ vhost = f"{ os .environ .get (broker_env_var ).removeprefix ('/' )} "
107
117
broker_url = f"{ broker_params ['protocol' ]} ://{ broker_params ['user' ]} :{ broker_params ['password' ]} @{ broker_params ['host' ]} :{ broker_params ['port' ]} /{ vhost } "
108
118
return broker_url
109
119
return default_url
110
120
111
121
112
- def build_redis_url (db = None ):
122
+ def build_redis_url (db = None , prefix = "INVENIO" ):
113
123
"""
114
124
Build Redis URL from environment variables or use default.
115
125
@@ -119,16 +129,17 @@ def build_redis_url(db=None):
119
129
3. INVENIO_KV_CACHE_* specific environment variables
120
130
4. Default URL
121
131
"""
132
+ prefix = _check_prefix (prefix )
122
133
db = db if db is not None else 0
123
134
default_url = f"redis://localhost:6379/{ db } "
124
135
125
- for key in ["INVENIO_CACHE_REDIS_URL " , "CACHE_REDIS_URL" ]:
136
+ for key in [f" { prefix } _CACHE_REDIS_URL " , "CACHE_REDIS_URL" ]:
126
137
if cache_url := os .environ .get (key ):
127
138
if cache_url .startswith (("redis://" , "rediss://" , "unix://" )):
128
139
return cache_url
129
140
130
141
redis_params = _get_env_var (
131
- "INVENIO_KV_CACHE " , ["host" , "port" , "password" , "protocol" ]
142
+ f" { prefix } _KV_CACHE " , ["host" , "port" , "password" , "protocol" ]
132
143
)
133
144
134
145
if redis_params ["host" ] and redis_params ["port" ]:
0 commit comments