# coding=utf-8 import configparser import os import codecs from base_framework.base_config.current_pth import * from base_framework.public_tools import log obj_log = log.get_logger() class ReadConfig: """ 专门读取配置文件的,.ini文件格式 """ def __init__(self, filename=config_file_path): self.config_path = filename fd = open(self.config_path, encoding='utf-8') data = fd.read() if data[:3] == codecs.BOM_UTF8: data = data[3:] files = codecs.open(self.config_path, "w") files.write(data) files.close() fd.close() self.cf = configparser.SafeConfigParser(allow_no_value=True) self.cf.read(self.config_path, encoding='utf-8') def get_value(self, sections, options): """ 获取config文件数据 """ return self.cf.get(sections, options) def read_cfg(self): """ 读取配置文件路径 """ return self.cf.read(filenames=self.config_path, encoding='utf-8') def get_sections(self): """ 读取配置文件中所有的section(可以理解为组名) """ return self.cf.sections() def get_options(self, section): """ 读取该section下所有的option(可以理解成读取该组下的所有key) """ return self.cf.options(section) def get_items(self, section): """ 读取该section下的所有值,并以键值对形式输出 """ return self.cf.items(section) def add_section(self, section): """ 添加一个section,参数为section的名称 """ self.cf.add_section(section) with open(self.config_path, 'w') as fw: # 循环写入 self.cf.write(fw) def set_section(self, section, option, value): """ 在section下面添加一条数据(key=value),需要调用write()将内容写入文件 """ self.cf.set(section, option, value) with open(self.config_path, 'w') as fw: # 循环写入 self.cf.write(fw) def get_all_cfg(self, section=None): all_config = {} for key in self.get_sections(): all_values = {} for value_key in self.get_options(key): all_values[value_key] = self.get_value(key, value_key) all_config[key] = all_values if section: if section in all_config: return all_config[section] else: raise Exception("配置节点:{}在文件中不存在,请检查....".format(section)) else: return all_config def get_current_config(file_path=env_choose_path, section=None, key=None): """ 功能:读取env_choose.ini中的配置并返回 """ rd = ReadConfig(file_path) if key in rd.get_options(section=section): return rd.get_value(sections=section, options=key) else: return "server not exist" def get_zhyy_config(file_path=config_choose_path, section=None, key=None): """ 功能:读取env_choose.ini中的配置并返回 """ rd = ReadConfig(file_path) if key in rd.get_options(section=section): return rd.get_value(sections=section, options=key) else: return "server not exist" def get_current_env(): """ 功能:读取env_choose.ini中的当前环境配置并返回 """ rd = ReadConfig(env_choose_path) env = rd.get_value(sections="run_jira_id", options="huohua-podenv") if not env: env = rd.get_value(sections="run_evn_name", options="current_evn") return env def get_uat_config(website, page, key=None, section='page_element'): """ | 功能说明: | 从uat_config文件夹下的对应文件中读取key | | 输入参数: | website | 站点名,public_business/uat/uat_config目录下的文件名 | | | page | web页面名称,website文件夹下的文件名,不用.ini的文件后缀名 | | | section | 配置文件中的节点名,默认读取页面元素 | | | key | 配置文件中的key,如果不输入,将以当前构建环境为key,如qa,sim | | 作者信息: | 吴勇刚 | 2022.06.22 | """ config_path = os.path.join(uat_config_path, website, "{}.ini".format(page)) if not key: key = get_current_config(section="run_evn_name", key="current_evn").lower() rd = ReadConfig(config_path) return rd.get_value(sections=section, options=key) class InitConfig: def __init__(self, run_user_name=None, current_evn=None): self.cfg_rtn = ReadConfig() self.evn_rtn = ReadConfig(env_choose_path) self.db_rtn = ReadConfig(db_config_path) self.config_rtn = ReadConfig(config_choose_path) self.all_cfg = self.cfg_rtn.get_all_cfg() self.config_cfg = self.config_rtn.get_all_cfg() self.evn_cfg = self.evn_rtn.get_all_cfg() self.db_cfg = self.db_rtn.get_all_cfg() try: self.astwb_cfg = ReadConfig(astwb_config) self.astwb_all_cfg = self.astwb_cfg.get_all_cfg() # 合并配置文件 self.all_cfg.update(self.astwb_all_cfg) except Exception as e: pass if current_evn is None: self.current_evn = self.evn_cfg['run_evn_name']['current_evn'] if self.evn_cfg['run_evn_name'][ 'current_evn'] != '' else 'QA' else: self.current_evn = current_evn if run_user_name: self.current_user = run_user_name else: self.current_user = self.evn_cfg['run_user_name']['default_user'] self.zhyy_host = self.get_current_env_info(self.config_cfg, self.current_evn, 'zhyy_login') def get_current_env_info(self, cfg_obj, current_env, key): """ config.ini中环境处理专用 :param cfg_obj: self.cfg_rtn = ReadConfig() self.all_cfg = self.cfg_rtn.get_all_cfg() cfg_obj = self.all_cfg :param current_env: QA SIM PRODUCT :param key: options :return: """ try: value = cfg_obj[current_env][key] except KeyError: value = cfg_obj['QA'][key] if current_env == 'SIM': value = value.replace('.qa.', '.sim.') return value elif current_env == 'PRODUCT': value = value.replace('.qa.', '.') return value return value if __name__ == '__main__': pass # init_cfg = InitConfig(curt_evn='QA', curt_user='lrq') print(ReadConfig().add_section(section='temp_teacher')) print(ReadConfig().set_section(section='temp_teacher', option='show_username', value=11))