配置读取模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import ConfigParser import os
class Config: def __init__(self, config_path=os.path.join(os.path.dirname(os.path.realpath(__file__)), "argogo.config")): self.config_path = config_path
def read_config(self): config = ConfigParser.ConfigParser() config.read(self.config_path) return config
def get_value(self, section, key): config = ConfigParser.ConfigParser() config.read(self.config_path) return config.get(section, key)
|
配置文件
1 2 3 4 5 6 7
| # config file
[section1] key1 = value1
[section2] key2 = value 2
|
使用标准config file格式,注意=
周围的空格会被忽略。
使用
1 2 3 4 5 6 7 8
| import config.config_parser conf = config.config_parser.Config().read_config()
import config.global_config as gc gc.key1 = conf.get("section1", "key1") gc.key2 = config.config_parser.Config().get_value("section2", "key2") log.info("key1 = " + gc.key1) log.info("key2 = " + gc.key2)
|