Skip to content

Commit

Permalink
Refactor configuration validation in config.py by introducing check_c…
Browse files Browse the repository at this point in the history
…onfig and check_is_valid methods for improved clarity and maintainability
  • Loading branch information
chengazhen committed Jan 13, 2025
1 parent 15ffc5e commit fa313d6
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ def __init__(self):
self.temp_mail = os.getenv("TEMP_MAIL", "").strip()
self.domain = os.getenv("DOMAIN", "").strip()

if not self.temp_mail:
raise ValueError("临时邮箱未配置,请在 .env 文件中设置 TEMP_MAIL")
if not self.domain:
raise ValueError("域名未配置,请在 .env 文件中设置 DOMAIN")
self.check_config()

def get_temp_mail(self):
logging.info(f"\033[32m临时邮箱: {self.temp_mail}\033[0m")
Expand All @@ -39,6 +36,15 @@ def get_domain(self):
logging.info(f"\033[32m域名: {self.domain}\033[0m")
return self.domain

def check_config(self):
if not self.check_is_valid(self.temp_mail):
raise ValueError("临时邮箱未配置,请在 .env 文件中设置 TEMP_MAIL")
if not self.check_is_valid(self.domain):
raise ValueError("域名未配置,请在 .env 文件中设置 DOMAIN")

def check_is_valid(self, str):
return len(str.strip()) > 0


# 使用示例
if __name__ == "__main__":
Expand Down

0 comments on commit fa313d6

Please sign in to comment.