env read failure reminder
Browse files- toolbox.py +28 -20
toolbox.py
CHANGED
@@ -536,28 +536,36 @@ def read_env_variable(arg, default_value):
|
|
536 |
"""
|
537 |
from colorful import print亮红, print亮绿
|
538 |
arg_with_prefix = "GPT_ACADEMIC_" + arg
|
539 |
-
if arg_with_prefix in os.environ:
|
540 |
-
|
|
|
|
|
541 |
else:
|
542 |
raise KeyError
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
561 |
print亮绿(f"[ENV_VAR] 成功读取环境变量{arg}")
|
562 |
return r
|
563 |
|
|
|
536 |
"""
|
537 |
from colorful import print亮红, print亮绿
|
538 |
arg_with_prefix = "GPT_ACADEMIC_" + arg
|
539 |
+
if arg_with_prefix in os.environ:
|
540 |
+
env_arg = os.environ[arg_with_prefix]
|
541 |
+
elif arg in os.environ:
|
542 |
+
env_arg = os.environ[arg]
|
543 |
else:
|
544 |
raise KeyError
|
545 |
+
print(f"[ENV_VAR] 尝试加载{arg},默认值:{default_value} --> 修正值:{env_arg}")
|
546 |
+
try:
|
547 |
+
if isinstance(default_value, bool):
|
548 |
+
r = bool(env_arg)
|
549 |
+
elif isinstance(default_value, int):
|
550 |
+
r = int(env_arg)
|
551 |
+
elif isinstance(default_value, float):
|
552 |
+
r = float(env_arg)
|
553 |
+
elif isinstance(default_value, str):
|
554 |
+
r = env_arg
|
555 |
+
elif isinstance(default_value, dict):
|
556 |
+
r = eval(env_arg)
|
557 |
+
elif isinstance(default_value, list):
|
558 |
+
r = eval(env_arg)
|
559 |
+
elif default_value is None:
|
560 |
+
assert arg == "proxies"
|
561 |
+
r = eval(env_arg)
|
562 |
+
else:
|
563 |
+
print亮红(f"[ENV_VAR] 环境变量{arg}不支持通过环境变量设置! ")
|
564 |
+
raise KeyError
|
565 |
+
except:
|
566 |
+
print亮红(f"[ENV_VAR] 环境变量{arg}加载失败! ")
|
567 |
+
raise KeyError(f"[ENV_VAR] 环境变量{arg}加载失败! ")
|
568 |
+
|
569 |
print亮绿(f"[ENV_VAR] 成功读取环境变量{arg}")
|
570 |
return r
|
571 |
|