Skip to content

Commit b2a71dc

Browse files
committed
Introduce common/config_utils.py
Signed-off-by: Jin Hai <haijin.chn@gmail.com>
1 parent 121d3fd commit b2a71dc

File tree

10 files changed

+165
-126
lines changed

10 files changed

+165
-126
lines changed

admin/server/admin_server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from routes import admin_bp
2626
from api.utils.log_utils import init_root_logger
2727
from common.contants import SERVICE_CONF
28+
from common.config_utils import show_configs
2829
from api import settings
2930
from config import load_configurations, SERVICE_CONFIGS
3031
from auth import init_default_admin, setup_auth
@@ -51,6 +52,7 @@
5152
os.environ.get("MAX_CONTENT_LENGTH", 1024 * 1024 * 1024)
5253
)
5354
Session(app)
55+
show_configs()
5456
login_manager = LoginManager()
5557
login_manager.init_app(app)
5658
settings.init_settings()

admin/server/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from pydantic import BaseModel
2323
from typing import Any
24-
from api.utils.configs import read_config
24+
from common.config_utils import read_config
2525
from urllib.parse import urlparse
2626

2727

api/ragflow_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from api.db.db_models import init_database_tables as init_web_db
4242
from api.db.init_data import init_web_data
4343
from api.versions import get_ragflow_version
44-
from api.utils.configs import show_configs
44+
from common.config_utils import show_configs
4545
from rag.settings import print_rag_settings
4646
from rag.utils.mcp_tool_call_conn import shutdown_all_mcp_sessions
4747
from rag.utils.redis_conn import RedisDistributedLock

api/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import rag.utils.infinity_conn
2525
import rag.utils.opensearch_conn
2626
from api.constants import RAG_FLOW_SERVICE_NAME
27-
from api.utils.configs import decrypt_database_config, get_base_config
27+
from common.config_utils import decrypt_database_config, get_base_config
2828
from common.file_utils import get_project_base_directory
2929
from rag.nlp import search
3030

api/utils/configs.py

Lines changed: 1 addition & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -14,129 +14,11 @@
1414
# limitations under the License.
1515
#
1616

17-
import os
1817
import io
19-
import copy
20-
import logging
2118
import base64
2219
import pickle
23-
import importlib
24-
25-
from api.utils import file_utils
26-
from common.file_utils import get_project_base_directory
27-
from filelock import FileLock
2820
from api.utils.common import bytes_to_string, string_to_bytes
29-
from common.contants import SERVICE_CONF
30-
31-
32-
def conf_realpath(conf_name):
33-
conf_path = f"conf/{conf_name}"
34-
return os.path.join(get_project_base_directory(), conf_path)
35-
36-
37-
def read_config(conf_name=SERVICE_CONF):
38-
local_config = {}
39-
local_path = conf_realpath(f'local.{conf_name}')
40-
41-
# load local config file
42-
if os.path.exists(local_path):
43-
local_config = file_utils.load_yaml_conf(local_path)
44-
if not isinstance(local_config, dict):
45-
raise ValueError(f'Invalid config file: "{local_path}".')
46-
47-
global_config_path = conf_realpath(conf_name)
48-
global_config = file_utils.load_yaml_conf(global_config_path)
49-
50-
if not isinstance(global_config, dict):
51-
raise ValueError(f'Invalid config file: "{global_config_path}".')
52-
53-
global_config.update(local_config)
54-
return global_config
55-
56-
57-
CONFIGS = read_config()
58-
59-
60-
def show_configs():
61-
msg = f"Current configs, from {conf_realpath(SERVICE_CONF)}:"
62-
for k, v in CONFIGS.items():
63-
if isinstance(v, dict):
64-
if "password" in v:
65-
v = copy.deepcopy(v)
66-
v["password"] = "*" * 8
67-
if "access_key" in v:
68-
v = copy.deepcopy(v)
69-
v["access_key"] = "*" * 8
70-
if "secret_key" in v:
71-
v = copy.deepcopy(v)
72-
v["secret_key"] = "*" * 8
73-
if "secret" in v:
74-
v = copy.deepcopy(v)
75-
v["secret"] = "*" * 8
76-
if "sas_token" in v:
77-
v = copy.deepcopy(v)
78-
v["sas_token"] = "*" * 8
79-
if "oauth" in k:
80-
v = copy.deepcopy(v)
81-
for key, val in v.items():
82-
if "client_secret" in val:
83-
val["client_secret"] = "*" * 8
84-
if "authentication" in k:
85-
v = copy.deepcopy(v)
86-
for key, val in v.items():
87-
if "http_secret_key" in val:
88-
val["http_secret_key"] = "*" * 8
89-
msg += f"\n\t{k}: {v}"
90-
logging.info(msg)
91-
92-
93-
def get_base_config(key, default=None):
94-
if key is None:
95-
return None
96-
if default is None:
97-
default = os.environ.get(key.upper())
98-
return CONFIGS.get(key, default)
99-
100-
101-
def decrypt_database_password(password):
102-
encrypt_password = get_base_config("encrypt_password", False)
103-
encrypt_module = get_base_config("encrypt_module", False)
104-
private_key = get_base_config("private_key", None)
105-
106-
if not password or not encrypt_password:
107-
return password
108-
109-
if not private_key:
110-
raise ValueError("No private key")
111-
112-
module_fun = encrypt_module.split("#")
113-
pwdecrypt_fun = getattr(
114-
importlib.import_module(
115-
module_fun[0]),
116-
module_fun[1])
117-
118-
return pwdecrypt_fun(private_key, password)
119-
120-
121-
def decrypt_database_config(
122-
database=None, passwd_key="password", name="database"):
123-
if not database:
124-
database = get_base_config(name, {})
125-
126-
database[passwd_key] = decrypt_database_password(database[passwd_key])
127-
return database
128-
129-
130-
def update_config(key, value, conf_name=SERVICE_CONF):
131-
conf_path = conf_realpath(conf_name=conf_name)
132-
if not os.path.isabs(conf_path):
133-
conf_path = os.path.join(get_project_base_directory(), conf_path)
134-
135-
with FileLock(os.path.join(os.path.dirname(conf_path), ".lock")):
136-
config = file_utils.load_yaml_conf(conf_path=conf_path) or {}
137-
config[key] = value
138-
file_utils.rewrite_yaml_conf(conf_path=conf_path, config=config)
139-
21+
from common.config_utils import get_base_config
14022

14123
safe_module = {
14224
'numpy',

common/config_utils.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#
2+
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import os
18+
import copy
19+
import logging
20+
import importlib
21+
from filelock import FileLock
22+
23+
from common.file_utils import get_project_base_directory
24+
from common.contants import SERVICE_CONF
25+
from ruamel.yaml import YAML
26+
27+
28+
def load_yaml_conf(conf_path):
29+
if not os.path.isabs(conf_path):
30+
conf_path = os.path.join(get_project_base_directory(), conf_path)
31+
try:
32+
with open(conf_path) as f:
33+
yaml = YAML(typ="safe", pure=True)
34+
return yaml.load(f)
35+
except Exception as e:
36+
raise EnvironmentError("loading yaml file config from {} failed:".format(conf_path), e)
37+
38+
39+
def rewrite_yaml_conf(conf_path, config):
40+
if not os.path.isabs(conf_path):
41+
conf_path = os.path.join(get_project_base_directory(), conf_path)
42+
try:
43+
with open(conf_path, "w") as f:
44+
yaml = YAML(typ="safe")
45+
yaml.dump(config, f)
46+
except Exception as e:
47+
raise EnvironmentError("rewrite yaml file config {} failed:".format(conf_path), e)
48+
49+
50+
def conf_realpath(conf_name):
51+
conf_path = f"conf/{conf_name}"
52+
return os.path.join(get_project_base_directory(), conf_path)
53+
54+
55+
def read_config(conf_name=SERVICE_CONF):
56+
local_config = {}
57+
local_path = conf_realpath(f'local.{conf_name}')
58+
59+
# load local config file
60+
if os.path.exists(local_path):
61+
local_config = load_yaml_conf(local_path)
62+
if not isinstance(local_config, dict):
63+
raise ValueError(f'Invalid config file: "{local_path}".')
64+
65+
global_config_path = conf_realpath(conf_name)
66+
global_config = load_yaml_conf(global_config_path)
67+
68+
if not isinstance(global_config, dict):
69+
raise ValueError(f'Invalid config file: "{global_config_path}".')
70+
71+
global_config.update(local_config)
72+
return global_config
73+
74+
75+
CONFIGS = read_config()
76+
77+
78+
def show_configs():
79+
msg = f"Current configs, from {conf_realpath(SERVICE_CONF)}:"
80+
for k, v in CONFIGS.items():
81+
if isinstance(v, dict):
82+
if "password" in v:
83+
v = copy.deepcopy(v)
84+
v["password"] = "*" * 8
85+
if "access_key" in v:
86+
v = copy.deepcopy(v)
87+
v["access_key"] = "*" * 8
88+
if "secret_key" in v:
89+
v = copy.deepcopy(v)
90+
v["secret_key"] = "*" * 8
91+
if "secret" in v:
92+
v = copy.deepcopy(v)
93+
v["secret"] = "*" * 8
94+
if "sas_token" in v:
95+
v = copy.deepcopy(v)
96+
v["sas_token"] = "*" * 8
97+
if "oauth" in k:
98+
v = copy.deepcopy(v)
99+
for key, val in v.items():
100+
if "client_secret" in val:
101+
val["client_secret"] = "*" * 8
102+
if "authentication" in k:
103+
v = copy.deepcopy(v)
104+
for key, val in v.items():
105+
if "http_secret_key" in val:
106+
val["http_secret_key"] = "*" * 8
107+
msg += f"\n\t{k}: {v}"
108+
logging.info(msg)
109+
110+
111+
def get_base_config(key, default=None):
112+
if key is None:
113+
return None
114+
if default is None:
115+
default = os.environ.get(key.upper())
116+
return CONFIGS.get(key, default)
117+
118+
119+
def decrypt_database_password(password):
120+
encrypt_password = get_base_config("encrypt_password", False)
121+
encrypt_module = get_base_config("encrypt_module", False)
122+
private_key = get_base_config("private_key", None)
123+
124+
if not password or not encrypt_password:
125+
return password
126+
127+
if not private_key:
128+
raise ValueError("No private key")
129+
130+
module_fun = encrypt_module.split("#")
131+
pwdecrypt_fun = getattr(
132+
importlib.import_module(
133+
module_fun[0]),
134+
module_fun[1])
135+
136+
return pwdecrypt_fun(private_key, password)
137+
138+
139+
def decrypt_database_config(database=None, passwd_key="password", name="database"):
140+
if not database:
141+
database = get_base_config(name, {})
142+
143+
database[passwd_key] = decrypt_database_password(database[passwd_key])
144+
return database
145+
146+
147+
def update_config(key, value, conf_name=SERVICE_CONF):
148+
conf_path = conf_realpath(conf_name=conf_name)
149+
if not os.path.isabs(conf_path):
150+
conf_path = os.path.join(get_project_base_directory(), conf_path)
151+
152+
with FileLock(os.path.join(os.path.dirname(conf_path), ".lock")):
153+
config = load_yaml_conf(conf_path=conf_path) or {}
154+
config[key] = value
155+
rewrite_yaml_conf(conf_path=conf_path, config=config)

deepdoc/parser/tcadp_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
3737
from tencentcloud.lkeap.v20240522 import lkeap_client, models
3838

39-
from api.utils.configs import get_base_config
39+
from common.config_utils import get_base_config
4040
from deepdoc.parser.pdf_parser import RAGFlowPdfParser
4141

4242

rag/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
import os
1717
import logging
18-
from api.utils.configs import get_base_config, decrypt_database_config
18+
from common.config_utils import get_base_config, decrypt_database_config
1919
from common.file_utils import get_project_base_directory
2020
from common.misc_utils import pip_install_torch
2121

rag/svr/task_executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from common.base64_image import image2id
3131
from api.utils.log_utils import init_root_logger
3232
from common.file_utils import get_project_base_directory
33-
from api.utils.configs import show_configs
33+
from common.config_utils import show_configs
3434
from graphrag.general.index import run_graphrag_for_kb
3535
from graphrag.utils import get_llm_cache, set_llm_cache, get_tags_from_cache, set_tags_to_cache
3636
from rag.flow.pipeline import Pipeline

rag/utils/opendal_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pymysql
44
from urllib.parse import quote_plus
55

6-
from api.utils.configs import get_base_config
6+
from common.config_utils import get_base_config
77
from common.decorator import singleton
88

99

0 commit comments

Comments
 (0)