yaml封装
#!/usr/bin/python # -*- coding: UTF-8 -*- import yaml import os proDir = os.path.split(os.path.realpath(__file__))[0] #返回文件的路径和文件名 configPath = os.path.join(proDir, "config.yaml") class ReadConfig: def __init__(self): self.path = configPath with open(self.path,encoding="utf-8") as fp: self.yaml_info = yaml.full_load(fp) def get_http(self,name): value = self.yaml_info["HTTP"][name] return value def get_headers(self,name): value = self.yaml_info["HEADERS"][name] return value def set_headers(self,name,value): with open(self.path,"a+") as fp: self.yaml_info["HEADERS"][name] = value yaml.dump(self.yaml_info,fp) def get_var(self,name): value = self.yaml_info["VARIABLES"][name] return value def set_var(self,name,value): with open(self.path,"a+") as fp: self.yaml_info["VARIABLES"][name] = value yaml.dump(self.yaml_info,fp) def get_url(self, name): value = self.yaml_info["URL"][name] return value def set_url(self,name,value): with open(self.path,"a+") as fp: self.yaml_info["URL"][name] = value yaml.dump(self.yaml_info,fp) # ## 读取yaml文件 # def readyaml(file): # with open(file,encoding='utf-8') as fp: # yaml_info = yaml.full_load(fp) # return yaml_info # # ## 向yaml文件中写入配置 # def writeyaml(file, data): # fr = open(file, 'w') # yaml.dump(data, fr) # fr.close() # # def main(): # yaml_file = "config.yaml" # yamlinfo =readyaml(yaml_file) # print(yamlinfo["HTTP"]) # print(yamlinfo["HTTP"]["schema"]) # yamlinfo["HTTP"]["schema"] = "http" # yamlinfo["HTTP"]["test"] = "test" # writeyaml(yaml_file,yamlinfo) if __name__ == '__main__': localConfig = ReadConfig() url = localConfig.get_http("schema") + "://" + localConfig.get_http("host") + localConfig.get_url("userLogin") print(localConfig.yaml_info) # # http.set_headers("token","TOKEN") print(localConfig.get_url("userLogin"))