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
~                       

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值