00001 """Configuration file parsing utilities, layered on top of stock 00002 Python ConfigParser module. 00003 00004 $Id: config.py 1873 2008-06-12 02:49:41Z sra $ 00005 00006 Copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN") 00007 00008 Permission to use, copy, modify, and distribute this software for any 00009 purpose with or without fee is hereby granted, provided that the above 00010 copyright notice and this permission notice appear in all copies. 00011 00012 THE SOFTWARE IS PROVIDED "AS IS" AND ARIN DISCLAIMS ALL WARRANTIES WITH 00013 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 00014 AND FITNESS. IN NO EVENT SHALL ARIN BE LIABLE FOR ANY SPECIAL, DIRECT, 00015 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 00016 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 00017 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00018 PERFORMANCE OF THIS SOFTWARE. 00019 """ 00020 00021 import ConfigParser 00022 00023 class parser(ConfigParser.RawConfigParser): 00024 00025 def __init__(self, file = None, section = None): 00026 """Initialize this parser.""" 00027 ConfigParser.RawConfigParser.__init__(self) 00028 if file: 00029 self.read(file) 00030 self.default_section = section 00031 00032 def multiget(self, option, section = None): 00033 """Parse OpenSSL-style foo.0, foo.1, ... subscripted options. 00034 00035 Returns a list of values matching the specified option name. 00036 """ 00037 matches = [] 00038 if section is None: 00039 section = self.default_section 00040 if self.has_option(section, option): 00041 matches.append((-1, self.get(option, section = section))) 00042 for key, value in self.items(section): 00043 s = key.rsplit(".", 1) 00044 if len(s) == 2 and s[0] == option and s[1].isdigit(): 00045 matches.append((int(s[1]), value)) 00046 matches.sort() 00047 return [match[1] for match in matches] 00048 00049 def get(self, option, default = None, section = None): 00050 """Get an option, perhaps with a default value.""" 00051 if section is None: 00052 section = self.default_section 00053 if default is None or self.has_option(section, option): 00054 return ConfigParser.RawConfigParser.get(self, section, option) 00055 else: 00056 return default