def config_write_data(file, section, option, value: str, reset=False): # 先创建文件(如果文件存在,则不修改) create_file(file) parser = configparser.ConfigParser() if not reset: # 先读取原有的配置,否则写的时候会直接将原内容清空 parser.read(file, encoding="utf-8-sig") if section not in parser.sections(): try: # 增加Section,如果原来有section ,则不会可能返回错误 parser.add_section(section) except Exception as err: LogUtils.info_print(f"write config file:{file}, section={section},option={option},result:{err}") if option != '': try: # 写入value parser.set(section, option, value) except Exception as err: LogUtils.error_print(f"write config file:{file}, section={section},option={option},result:{err}") return False with open(file, "w+", encoding="utf-8-sig") as fd: parser.write(fd) return True
def pinch_out(self, percent=30, steps=100): try: LogUtils.info_print(f"放大{percent}", 4) self.dev().pinch_out(percent=percent, steps=steps) return True except Exception as error: LogUtils.debug_print(error) return False
def mkdir(path, reset=False): """ 创建文件夹 :param path:文件夹路径 :param reset:如果存在,需要先删除 :return: """ # 去除首位空格 path = path.strip() # 去除尾部 \ 符号 path = path.rstrip("\\").rstrip('/') # 需要复位,则先删除 if reset: try: rmdir(path) time.sleep(1) os.makedirs(path) LogUtils.info_print(f"{path} 创建成功!", 5) except Exception as err: LogUtils.error_print(err) return # 判断路径是否存在 # 存在 True # 不存在 False is_exist = os.path.exists(path) # 判断结果 if not is_exist: # 如果不存在则创建目录 # 创建目录操作函数 os.makedirs(path) LogUtils.info_print(f" {path} 创建成功!", 5) return True else: # 如果目录存在则不创建,并提示目录已存在 LogUtils.debug_print(f" {path} 目录已存在!", 5) return False
def config_read_options(file, section): """ 获取配置某个Fields下的全部Options :param file:文件路径 :param section:在配置文件中的章节 :return: list 该section下的所有章节 """ option_list = [] if not os.path.isfile(file): # 无文件时,返回空 LogUtils.error_print(f"no file: {file}") return option_list parser = configparser.ConfigParser() LogUtils.info_print(f"read cfg file = {file}, section = {section}", 4) parser.read(file, encoding="utf-8-sig") try: option_list = parser.options(section=section) except Exception as err: LogUtils.error_print(f"get config file:{file}, section:{section},result:{err}") return option_list
def config_read_data(file, section, option): """ 获取配置文件中的内容 :param file:文件路径 :param section:在配置文件中的章节 :param option:在配置的文件中option名称 :return: """ if not os.path.isfile(file): # 无文件时,返回空 LogUtils.warning_print(f"no file,{file}") return '' parser = configparser.ConfigParser() LogUtils.info_print(f"read cfg file = {file}, section = {section}, option = {option}", 4) parser.read(file, encoding="utf-8-sig") raw_data = '' try: raw_data = parser.get(section=section, option=option) except Exception as err: LogUtils.error_print(f"get config file:{file}, section={section},option={option},result:{err}") return raw_data