`
shaojiashuai123456
  • 浏览: 265969 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
社区版块
存档分类
最新评论

python tornado 框架使用 (3)

 
阅读更多

(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
~                       

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics