代码拉取完成,页面将自动刷新
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@software: PyCharm
@time: 2022/9/3 13:03
"""
import os
import yaml
class conf(object):
pass
class Configuration:
config = conf()
def __init__(self, conf_file="config.yaml"):
self._file = conf_file
self.conf_dict = dict()
if not os.path.exists(self._file):
raise FileNotFoundError("配置文件未找到!")
else:
self.read()
def read(self):
with open(self._file, 'r', encoding='utf8') as f:
self.conf_dict = yaml.load(f, yaml.FullLoader)
@classmethod
def get_config(cls, conf_file="config.yaml"):
conf_obj = cls(conf_file)
for key, value in conf_obj.conf_dict.items():
convert_obj(cls.config, key, value)
return cls.config
def convert_obj(obj, key, value):
if isinstance(value, list):
setattr(obj, key, [])
for item in value:
if not isinstance(item, dict) and not isinstance(item, list):
getattr(obj, key).append(item)
elif isinstance(item, dict):
item_obj = conf()
getattr(obj, key).append(item_obj)
for sub_key, sub_value in item.items():
convert_obj(item_obj, sub_key, sub_value)
elif not isinstance(value, dict):
setattr(obj, key, value)
else:
setattr(obj, key, conf())
for sub_key, sub_value in value.items():
convert_obj(getattr(obj, key), sub_key, sub_value)
return obj
configuration = Configuration.get_config()
print(configuration.log.maxBytes)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。