# -*- coding: utf-8 -*- # @Time : 2021/8/24 11:11 # @Author : jiafeng.li # @File : Config.py from configparser import ConfigParser from Common import Log import os class Config: # titles: TITLE_EMAIL = "mail" #[mail] VALUE_SMTP_SERVER = "smtpserver" VALUE_SENDER = "sender" VALUE_RECEIVER = "receiver" VALUE_USERNAME = "username" VALUE_PASSWORD = "password" #path path_dir = str(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) def __init__(self): self.config = ConfigParser() self.log = Log.MyLog() self.conf_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.ini') self.result_path = Config.path_dir+'/Report/result' self.report_path = Config.path_dir+'/Report/report' if not os.path.exists(self.conf_path): raise FileNotFoundError("请确保配置文件存在!") self.config.read(self.conf_path, encoding='utf-8') self.smtpserver = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_SMTP_SERVER) self.sender = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_SENDER) self.receiver = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_RECEIVER) self.username = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_USERNAME) self.password = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_PASSWORD) def get_conf(self, title, value): ''' 读取配置文件 :param title: :param value: :return: ''' return self.config.get(title,value) def set_config(self, title, value, text): ''' 配置文件修改 :param title: :param value: :param text: :return: ''' self.config.set(title, value, text) with open(self.conf_path, 'w+') as f: return self.config.write(f) def add_config(self, title): ''' 配置文件添加 :param title: :return: ''' self.config.add_section(title) with open(self.conf_path, 'w+') as f: return self.config.write(f)