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"))
  • 相关阅读:
    重写(Overriding)与重载(Overloading)的区别
    A Guide to setup SQL Server Reporting Services (SSRS) with Dynamics AX
    date2Str Function in Dynamics AX 2009
    浅谈程序员加薪问题(转)
    消息队列设计精要
    Redis集群模式原理探究
    SpringBoot内置tomcat原理分析
    Mybatis整体设计探究
    MapStruct 使用详解
    Zookeeper快速领导者选举原理
  • 原文地址:https://www.cnblogs.com/python-kp/p/15323983.html
Copyright © 2011-2022 走看看