(3)自定义配置
common_conf.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import json
import time
import logging
import common_logging
import common_urllib
from unipath import Path
import traceback
import ConfigParser
logger = logging.getLogger(__name__)
class CommonConf:
def __init__(self, conf_name):
self.conf_path = Path(Path(__file__).absolute().ancestor(1), conf_name)
self.hight_conf_path = Path(Path(__file__).absolute().ancestor(1), "./conf/hight.conf")
self.conf = {}
self.list_conf_item = [ \
("server", "port"), \
("server", "thread_num") \
]
self.read_conf(self.conf_path)
self.read_conf(self.hight_conf_path)
self.print_conf()
def print_conf(self):
for (key, value) in self.conf.items():
logger.error("[%s]:[%s]" % (key, value))
def read_conf(self, my_conf):
hcf = ConfigParser.ConfigParser()
try:
hcf.read(my_conf)
for (section, option) in self.list_conf_item:
if hcf.has_option(section, option):
self.conf["%s_%s" % (section, option)] = hcf.get(section, option)
except:
logger.error("%s" % traceback.format_exc())
g_conf = CommonConf("./conf/my_conf.conf")
if __name__ == '__main__':
print g_conf.conf_path
print g_conf.hight_conf_path
conf 文件夹下的配置:
my_conf.conf
[server]
port = 8008
thread_num = 0
~