|
- # -*- coding: utf-8 -*-
- # filename: utils.py
-
- import json
- import logging
- import time
-
- def config_reader(config_path):
- config_dict = dict()
- with open(config_path, "r") as rfile:
- content = rfile.read()
- json_content = json.loads(content)
- for config_key in json_content:
- # print(config_key)
- config_value = json_content[config_key]["value"]
- config_dict[config_key] = config_value
-
- return config_dict
-
-
- # 针对临时素材的返回报文头,将header信息转换为dict格式
- def headers2dict(headers_info):
- # headers_info应该是列表格式
- # [('Connection', 'close'), ('Cache-Control', 'no-cache, must-revalidate'), ('Date', 'Fri, 17 Mar 2023 06:37:36 GMT'), ('Content-disposition', 'attachment; filename="kPgEOhTjuRnsAmNrpywmbfERYXGZKK2EX4IwoWRIF5GKyeLPEHxlEibhQVlJZE5c.amr"'), ('Content-Type', 'audio/amr'), ('Content-Length', '1700')]
- headers_dict = dict()
- if not isinstance(headers_info, list):
- return headers_dict
- for sub_item in headers_info:
- item_key, item_value = sub_item
- headers_dict[item_key] = item_value
- return headers_dict
-
- # 使用函数装饰器实现单例
- def Singleton(cls):
- _instance = {}
-
- def inner():
- if cls not in _instance:
- _instance[cls] = cls()
- return _instance[cls]
- return inner
-
- @Singleton
- class Logger(object):
- def __init__(self):
- self.logger = logging.getLogger()
- time_str = time.strftime("%Y-%m-%d_%H:%M:%S" ,time.localtime())
- file_name = "log/request%s.log" % time_str
- file_hanlder = logging.FileHandler(filename=file_name, encoding='utf-8', mode='w')
- formatter = logging.Formatter("%(asctime)s - %(name)s-%(levelname)s %(message)s")
- file_hanlder.setFormatter(formatter)
- self.logger.addHandler(file_hanlder)
- self.logger.setLevel(logging.DEBUG)
-
- def refresh(self):
- self.__init__()
-
- def info(self, msg):
- logging.info(f"\n{msg}\n")
-
- def error(self, msg):
- logging.error(msg)
-
-
- if __name__ == "__main__":
- config_file_path = "config/config.json"
- config_dict = config_reader(config_file_path)
- print(config_dict)
- time_str = time.strftime("%Y-%m-%d_%H:%M:%S" ,time.localtime())
-
- print(time_str, type(time_str))
|