|
|
@@ -10,14 +10,17 @@ from loguru import logger
|
|
|
|
|
|
from magic_pdf.libs.commons import parse_bucket_key
|
|
|
|
|
|
+# 定义配置文件名常量
|
|
|
+CONFIG_FILE_NAME = "magic-pdf.json"
|
|
|
+
|
|
|
|
|
|
def read_config():
|
|
|
home_dir = os.path.expanduser("~")
|
|
|
|
|
|
- config_file = os.path.join(home_dir, "magic-pdf.json")
|
|
|
+ config_file = os.path.join(home_dir, CONFIG_FILE_NAME)
|
|
|
|
|
|
if not os.path.exists(config_file):
|
|
|
- raise Exception(f"{config_file} not found")
|
|
|
+ raise FileNotFoundError(f"{config_file} not found")
|
|
|
|
|
|
with open(config_file, "r") as f:
|
|
|
config = json.load(f)
|
|
|
@@ -37,7 +40,7 @@ def get_s3_config(bucket_name: str):
|
|
|
access_key, secret_key, storage_endpoint = bucket_info[bucket_name]
|
|
|
|
|
|
if access_key is None or secret_key is None or storage_endpoint is None:
|
|
|
- raise Exception("ak, sk or endpoint not found in magic-pdf.json")
|
|
|
+ raise Exception(f"ak, sk or endpoint not found in {CONFIG_FILE_NAME}")
|
|
|
|
|
|
# logger.info(f"get_s3_config: ak={access_key}, sk={secret_key}, endpoint={storage_endpoint}")
|
|
|
|
|
|
@@ -58,7 +61,7 @@ def get_local_dir():
|
|
|
config = read_config()
|
|
|
local_dir = config.get("temp-output-dir")
|
|
|
if local_dir is None:
|
|
|
- logger.warning("'temp-output-dir' not found in magic-pdf.json, use '/tmp' as default")
|
|
|
+ logger.warning(f"'temp-output-dir' not found in {CONFIG_FILE_NAME}, use '/tmp' as default")
|
|
|
return "/tmp"
|
|
|
else:
|
|
|
return local_dir
|
|
|
@@ -68,7 +71,7 @@ def get_local_models_dir():
|
|
|
config = read_config()
|
|
|
models_dir = config.get("models-dir")
|
|
|
if models_dir is None:
|
|
|
- logger.warning("'models-dir' not found in magic-pdf.json, use '/tmp/models' as default")
|
|
|
+ logger.warning(f"'models-dir' not found in {CONFIG_FILE_NAME}, use '/tmp/models' as default")
|
|
|
return "/tmp/models"
|
|
|
else:
|
|
|
return models_dir
|
|
|
@@ -78,7 +81,7 @@ def get_device():
|
|
|
config = read_config()
|
|
|
device = config.get("device-mode")
|
|
|
if device is None:
|
|
|
- logger.warning("'device-mode' not found in magic-pdf.json, use 'cpu' as default")
|
|
|
+ logger.warning(f"'device-mode' not found in {CONFIG_FILE_NAME}, use 'cpu' as default")
|
|
|
return "cpu"
|
|
|
else:
|
|
|
return device
|