qingxu99 commited on
Commit
a69ef7f
1 Parent(s): 8960770

env read failure reminder

Browse files
Files changed (1) hide show
  1. 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: env_arg = os.environ[arg_with_prefix]
540
- elif arg in os.environ: env_arg = os.environ[arg]
 
 
541
  else:
542
  raise KeyError
543
- if isinstance(default_value, bool):
544
- r = bool(env_arg)
545
- elif isinstance(default_value, int):
546
- r = int(env_arg)
547
- elif isinstance(default_value, float):
548
- r = float(env_arg)
549
- elif isinstance(default_value, str):
550
- r = env_arg
551
- elif isinstance(default_value, dict):
552
- r = eval(env_arg)
553
- elif isinstance(default_value, list):
554
- r = eval(env_arg)
555
- elif default_value is None:
556
- assert arg == "proxies"
557
- r = eval(env_arg)
558
- else:
559
- print亮红(f"[ENV_VAR] 环境变量{arg}不支持通过环境变量设置!{default_value}")
560
- raise KeyError
 
 
 
 
 
 
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