zoukankan      html  css  js  c++  java
  • 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"))
  • 相关阅读:
    HtmlEncode 和 HtmlDecode
    Visual Studio .Net 的一些小技巧(2)
    Array和ArrayList的区别
    c#中 ?? 是什么意思?
    带有图片预览功能的上传表单 上传预览
    Js实现Repeater全选/反选 功能 终极解决方案
    处理URL重写后postback重写失效的问题 .browser文件
    在TreeView中使用CheckBox(c#)
    SQL操作全集
    智能仓库管理系统方案(一)
  • 原文地址:https://www.cnblogs.com/python-kp/p/15323983.html
Copyright © 2011-2022 走看看