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