Skip to content

Commit 1da2e22

Browse files
committed
refactor: add prefix support
1 parent f03c861 commit 1da2e22

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

invenio_config/env.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
"""Invenio environment configuration."""
1111

12-
from __future__ import absolute_import, print_function
13-
1412
import ast
1513
import os
1614

@@ -48,12 +46,21 @@ def init_app(self, app):
4846
app.config[varname] = value
4947

5048

51-
def _get_env_var(prefix, keys):
49+
def _get_env_var(prefix: str, keys: list) -> dict:
5250
"""Retrieve environment variables with a given prefix."""
5351
return {k: os.environ.get(f"{prefix}_{k.upper()}") for k in keys}
5452

5553

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"):
5764
"""
5865
Build database URI from environment variables or use default.
5966
@@ -66,14 +73,15 @@ def build_db_uri():
6673
Note: For option 3, to assert that the INVENIO_DB_* settings take effect,
6774
you need to set SQLALCHEMY_DATABASE_URI="" in your environment.
6875
"""
76+
prefix = _check_prefix(prefix)
6977
default_uri = "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm"
7078

71-
for key in ["INVENIO_SQLALCHEMY_DATABASE_URI", "SQLALCHEMY_DATABASE_URI"]:
79+
for key in [f"{prefix}_SQLALCHEMY_DATABASE_URI", "SQLALCHEMY_DATABASE_URI"]:
7280
if uri := os.environ.get(key):
7381
return uri
7482

7583
db_params = _get_env_var(
76-
"INVENIO_DB", ["user", "password", "host", "port", "name", "protocol"]
84+
f"{prefix}_DB", ["user", "password", "host", "port", "name", "protocol"]
7785
)
7886
if all(db_params.values()):
7987
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():
8290
return default_uri
8391

8492

85-
def build_broker_url():
93+
def build_broker_url(prefix="INVENIO"):
8694
"""
8795
Build broker URL from environment variables or use default.
8896
@@ -93,23 +101,25 @@ def build_broker_url():
93101
4. Default URL
94102
Note: see: https://docs.celeryq.dev/en/stable/userguide/configuration.html#new-lowercase-settings
95103
"""
104+
prefix = _check_prefix(prefix)
96105
default_url = "amqp://guest:guest@localhost:5672/"
97106

98-
for key in ["INVENIO_BROKER_URL", "BROKER_URL"]:
107+
for key in [f"{prefix}_BROKER_URL", "BROKER_URL"]:
99108
if broker_url := os.environ.get(key):
100109
return broker_url
101110

102111
broker_params = _get_env_var(
103-
"INVENIO_AMQP_BROKER", ["user", "password", "host", "port", "protocol"]
112+
f"{prefix}_AMQP_BROKER", ["user", "password", "host", "port", "protocol"]
104113
)
105114
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('/')}"
107117
broker_url = f"{broker_params['protocol']}://{broker_params['user']}:{broker_params['password']}@{broker_params['host']}:{broker_params['port']}/{vhost}"
108118
return broker_url
109119
return default_url
110120

111121

112-
def build_redis_url(db=None):
122+
def build_redis_url(db=None, prefix="INVENIO"):
113123
"""
114124
Build Redis URL from environment variables or use default.
115125
@@ -119,16 +129,17 @@ def build_redis_url(db=None):
119129
3. INVENIO_KV_CACHE_* specific environment variables
120130
4. Default URL
121131
"""
132+
prefix = _check_prefix(prefix)
122133
db = db if db is not None else 0
123134
default_url = f"redis://localhost:6379/{db}"
124135

125-
for key in ["INVENIO_CACHE_REDIS_URL", "CACHE_REDIS_URL"]:
136+
for key in [f"{prefix}_CACHE_REDIS_URL", "CACHE_REDIS_URL"]:
126137
if cache_url := os.environ.get(key):
127138
if cache_url.startswith(("redis://", "rediss://", "unix://")):
128139
return cache_url
129140

130141
redis_params = _get_env_var(
131-
"INVENIO_KV_CACHE", ["host", "port", "password", "protocol"]
142+
f"{prefix}_KV_CACHE", ["host", "port", "password", "protocol"]
132143
)
133144

134145
if redis_params["host"] and redis_params["port"]:

0 commit comments

Comments
 (0)