| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | """Configuration file parser.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A setup file consists of sections, lead by a "[section]" header, | 
					
						
							|  |  |  | and followed by "name: value" entries, with continuations and such in | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  | the style of RFC 822. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The option values can contain format strings which refer to other values in | 
					
						
							|  |  |  | the same section, or values in a special [DEFAULT] section. | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | For example: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     something: %(dir)s/whatever | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | would resolve the "%(dir)s" to the value of dir.  All reference | 
					
						
							|  |  |  | expansions are done late, on demand. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Intrinsic defaults can be specified by passing them into the | 
					
						
							|  |  |  | ConfigParser constructor as a dictionary. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ConfigParser -- responsible for for parsing a list of | 
					
						
							|  |  |  |                 configuration files, and managing the parsed database. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     methods: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-26 22:01:37 +00:00
										 |  |  |     __init__(defaults=None) | 
					
						
							|  |  |  |         create the parser and specify a dictionary of intrinsic defaults.  The | 
					
						
							|  |  |  |         keys must be strings, the values must be appropriate for %()s string | 
					
						
							|  |  |  |         interpolation.  Note that `__name__' is always an intrinsic default; | 
					
						
							|  |  |  |         it's value is the section's name. | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-26 22:01:37 +00:00
										 |  |  |     sections() | 
					
						
							|  |  |  |         return all the configuration section names, sans DEFAULT | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-10-04 19:58:22 +00:00
										 |  |  |     has_section(section) | 
					
						
							|  |  |  |         return whether the given section exists | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  |     has_option(section, option) | 
					
						
							|  |  |  |         return whether the given option exists in the given section | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-26 22:01:37 +00:00
										 |  |  |     options(section) | 
					
						
							|  |  |  |         return list of configuration options for the named section | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-30 04:35:47 +00:00
										 |  |  |     read(filenames) | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  |         read and parse the list of named configuration files, given by | 
					
						
							|  |  |  |         name.  A single filename is also allowed.  Non-existing files | 
					
						
							|  |  |  |         are ignored. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     readfp(fp, filename=None) | 
					
						
							|  |  |  |         read and parse one configuration file, given as a file object. | 
					
						
							|  |  |  |         The filename defaults to fp.name; it is only used in error | 
					
						
							| 
									
										
										
										
											1999-10-12 16:12:48 +00:00
										 |  |  |         messages (if fp has no `name' attribute, the string `<???>' is used). | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-26 22:01:37 +00:00
										 |  |  |     get(section, option, raw=0, vars=None) | 
					
						
							|  |  |  |         return a string value for the named option.  All % interpolations are | 
					
						
							|  |  |  |         expanded in the return values, based on the defaults passed into the | 
					
						
							|  |  |  |         constructor and the DEFAULT section.  Additional substitutions may be | 
					
						
							|  |  |  |         provided using the `vars' argument, which must be a dictionary whose | 
					
						
							|  |  |  |         contents override any pre-existing defaults. | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-26 22:01:37 +00:00
										 |  |  |     getint(section, options) | 
					
						
							|  |  |  |         like get(), but convert value to an integer | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-26 22:01:37 +00:00
										 |  |  |     getfloat(section, options) | 
					
						
							|  |  |  |         like get(), but convert value to a float | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-26 22:01:37 +00:00
										 |  |  |     getboolean(section, options) | 
					
						
							| 
									
										
										
										
											2001-10-04 19:58:46 +00:00
										 |  |  |         like get(), but convert value to a boolean (currently case | 
					
						
							|  |  |  |         insensitively defined as 0, false, no, off for 0, and 1, true, | 
					
						
							|  |  |  |         yes, on for 1).  Returns 0 or 1. | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-27 15:49:56 +00:00
										 |  |  |     items(section, raw=0, vars=None) | 
					
						
							|  |  |  |         return a list of tuples with (name, value) for each option | 
					
						
							|  |  |  |         in the section. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  |     remove_section(section) | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  |         remove the given file section and all its options | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     remove_option(section, option) | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  |         remove the given option from the given section | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     set(section, option, value) | 
					
						
							|  |  |  |         set the given option | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     write(fp) | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  |         write the configuration state in .ini format | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-20 19:54:20 +00:00
										 |  |  | __all__ = ["NoSectionError","DuplicateSectionError","NoOptionError", | 
					
						
							|  |  |  |            "InterpolationError","InterpolationDepthError","ParsingError", | 
					
						
							|  |  |  |            "MissingSectionHeaderError","ConfigParser", | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |            "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"] | 
					
						
							| 
									
										
										
										
											2001-01-20 19:54:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | DEFAULTSECT = "DEFAULT" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-27 22:43:54 +00:00
										 |  |  | MAX_INTERPOLATION_DEPTH = 10 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | # exception classes | 
					
						
							| 
									
										
										
										
											2000-12-11 18:13:19 +00:00
										 |  |  | class Error(Exception): | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  |     def __init__(self, msg=''): | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |         self._msg = msg | 
					
						
							| 
									
										
										
										
											2000-12-11 18:13:19 +00:00
										 |  |  |         Exception.__init__(self, msg) | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |         return self._msg | 
					
						
							| 
									
										
										
										
											2000-12-11 18:13:19 +00:00
										 |  |  |     __str__ = __repr__ | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class NoSectionError(Error): | 
					
						
							|  |  |  |     def __init__(self, section): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Error.__init__(self, 'No section: %s' % section) | 
					
						
							|  |  |  |         self.section = section | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DuplicateSectionError(Error): | 
					
						
							|  |  |  |     def __init__(self, section): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Error.__init__(self, "Section %s already exists" % section) | 
					
						
							|  |  |  |         self.section = section | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class NoOptionError(Error): | 
					
						
							|  |  |  |     def __init__(self, option, section): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Error.__init__(self, "No option `%s' in section: %s" % | 
					
						
							|  |  |  |                        (option, section)) | 
					
						
							|  |  |  |         self.option = option | 
					
						
							|  |  |  |         self.section = section | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class InterpolationError(Error): | 
					
						
							| 
									
										
										
										
											1998-08-06 18:48:41 +00:00
										 |  |  |     def __init__(self, reference, option, section, rawval): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Error.__init__(self, | 
					
						
							| 
									
										
										
										
											1998-08-06 18:48:41 +00:00
										 |  |  |                        "Bad value substitution:\n" | 
					
						
							|  |  |  |                        "\tsection: [%s]\n" | 
					
						
							|  |  |  |                        "\toption : %s\n" | 
					
						
							|  |  |  |                        "\tkey    : %s\n" | 
					
						
							|  |  |  |                        "\trawval : %s\n" | 
					
						
							|  |  |  |                        % (section, option, reference, rawval)) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         self.reference = reference | 
					
						
							|  |  |  |         self.option = option | 
					
						
							|  |  |  |         self.section = section | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-27 22:43:54 +00:00
										 |  |  | class InterpolationDepthError(Error): | 
					
						
							|  |  |  |     def __init__(self, option, section, rawval): | 
					
						
							|  |  |  |         Error.__init__(self, | 
					
						
							|  |  |  |                        "Value interpolation too deeply recursive:\n" | 
					
						
							|  |  |  |                        "\tsection: [%s]\n" | 
					
						
							|  |  |  |                        "\toption : %s\n" | 
					
						
							|  |  |  |                        "\trawval : %s\n" | 
					
						
							|  |  |  |                        % (section, option, rawval)) | 
					
						
							|  |  |  |         self.option = option | 
					
						
							|  |  |  |         self.section = section | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ParsingError(Error): | 
					
						
							|  |  |  |     def __init__(self, filename): | 
					
						
							|  |  |  |         Error.__init__(self, 'File contains parsing errors: %s' % filename) | 
					
						
							|  |  |  |         self.filename = filename | 
					
						
							|  |  |  |         self.errors = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def append(self, lineno, line): | 
					
						
							|  |  |  |         self.errors.append((lineno, line)) | 
					
						
							|  |  |  |         self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-27 22:43:54 +00:00
										 |  |  | class MissingSectionHeaderError(ParsingError): | 
					
						
							|  |  |  |     def __init__(self, filename, lineno, line): | 
					
						
							|  |  |  |         Error.__init__( | 
					
						
							|  |  |  |             self, | 
					
						
							|  |  |  |             'File contains no section headers.\nfile: %s, line: %d\n%s' % | 
					
						
							|  |  |  |             (filename, lineno, line)) | 
					
						
							|  |  |  |         self.filename = filename | 
					
						
							|  |  |  |         self.lineno = lineno | 
					
						
							|  |  |  |         self.line = line | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  | class RawConfigParser: | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  |     def __init__(self, defaults=None): | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         self._sections = {} | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         if defaults is None: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             self._defaults = {} | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             self._defaults = defaults | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def defaults(self): | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         return self._defaults | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def sections(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Return a list of section names, excluding [DEFAULT]""" | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         # self._sections will never have [DEFAULT] in it | 
					
						
							|  |  |  |         return self._sections.keys() | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def add_section(self, section): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Create a new section in the configuration.
 | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Raise DuplicateSectionError if a section by the specified name | 
					
						
							|  |  |  |         already exists. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         if section in self._sections: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             raise DuplicateSectionError(section) | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         self._sections[section] = {} | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def has_section(self, section): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Indicate whether the named section is present in the configuration.
 | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         The DEFAULT section is not acknowledged. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         return section in self._sections | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def options(self, section): | 
					
						
							| 
									
										
										
										
											1999-10-04 19:58:22 +00:00
										 |  |  |         """Return a list of option names for the given section name.""" | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             opts = self._sections[section].copy() | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         except KeyError: | 
					
						
							|  |  |  |             raise NoSectionError(section) | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         opts.update(self._defaults) | 
					
						
							| 
									
										
										
										
											2002-06-01 14:18:47 +00:00
										 |  |  |         if '__name__' in opts: | 
					
						
							| 
									
										
										
										
											2000-09-27 22:43:54 +00:00
										 |  |  |             del opts['__name__'] | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         return opts.keys() | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def read(self, filenames): | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  |         """Read and parse a filename or a list of filenames.
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  |         Files that cannot be opened are silently ignored; this is | 
					
						
							| 
									
										
										
										
											1999-10-12 16:12:48 +00:00
										 |  |  |         designed so that you can specify a list of potential | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  |         configuration file locations (e.g. current directory, user's | 
					
						
							|  |  |  |         home directory, systemwide directory), and all existing | 
					
						
							|  |  |  |         configuration files in the list will be read.  A single | 
					
						
							|  |  |  |         filename may also be given. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
											
												Remove uses of the string and types modules:
x in string.whitespace => x.isspace()
type(x) in types.StringTypes => isinstance(x, basestring)
isinstance(x, types.StringTypes) => isinstance(x, basestring)
type(x) is types.StringType => isinstance(x, str)
type(x) == types.StringType => isinstance(x, str)
string.split(x, ...) => x.split(...)
string.join(x, y) => y.join(x)
string.zfill(x, ...) => x.zfill(...)
string.count(x, ...) => x.count(...)
hasattr(types, "UnicodeType") => try: unicode except NameError:
type(x) != types.TupleTuple => not isinstance(x, tuple)
isinstance(x, types.TupleType) => isinstance(x, tuple)
type(x) is types.IntType => isinstance(x, int)
Do not mention the string module in the rlcompleter docstring.
This partially applies SF patch http://www.python.org/sf/562373
(with basestring instead of string). (It excludes the changes to
unittest.py and does not change the os.stat stuff.)
											
										 
											2002-06-03 15:58:32 +00:00
										 |  |  |         if isinstance(filenames, basestring): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             filenames = [filenames] | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  |         for filename in filenames: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 fp = open(filename) | 
					
						
							|  |  |  |             except IOError: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             self._read(fp, filename) | 
					
						
							| 
									
										
										
										
											1999-10-04 18:11:56 +00:00
										 |  |  |             fp.close() | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  |     def readfp(self, fp, filename=None): | 
					
						
							|  |  |  |         """Like read() but the argument must be a file-like object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The `fp' argument must have a `readline' method.  Optional | 
					
						
							|  |  |  |         second argument is the `filename', which if not given, is | 
					
						
							|  |  |  |         taken from fp.name.  If fp has no `name' attribute, `<???>' is | 
					
						
							|  |  |  |         used. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if filename is None: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 filename = fp.name | 
					
						
							|  |  |  |             except AttributeError: | 
					
						
							|  |  |  |                 filename = '<???>' | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         self._read(fp, filename) | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |     def get(self, section, option): | 
					
						
							|  |  |  |         opt = self.optionxform(option) | 
					
						
							|  |  |  |         if section not in self._sections: | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |             if section != DEFAULTSECT: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |                 raise NoSectionError(section) | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             if opt in self._defaults: | 
					
						
							|  |  |  |                 return self._defaults[opt] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise NoOptionError(option, section) | 
					
						
							|  |  |  |         elif opt in self._sections[section]: | 
					
						
							|  |  |  |             return self._sections[section][opt] | 
					
						
							|  |  |  |         elif opt in self._defaults: | 
					
						
							|  |  |  |             return self._defaults[opt] | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             raise NoOptionError(option, section) | 
					
						
							| 
									
										
										
										
											2000-09-27 22:43:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |     def items(self, section): | 
					
						
							| 
									
										
										
										
											2002-09-27 15:49:56 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             d2 = self._sections[section] | 
					
						
							| 
									
										
										
										
											2002-09-27 15:49:56 +00:00
										 |  |  |         except KeyError: | 
					
						
							|  |  |  |             if section != DEFAULTSECT: | 
					
						
							|  |  |  |                 raise NoSectionError(section) | 
					
						
							| 
									
										
										
										
											2002-10-25 20:41:30 +00:00
										 |  |  |             d2 = {} | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         d = self._defaults.copy() | 
					
						
							|  |  |  |         d.update(d2) | 
					
						
							| 
									
										
										
										
											2002-10-25 20:41:30 +00:00
										 |  |  |         if "__name__" in d: | 
					
						
							|  |  |  |             del d["__name__"] | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         return d.items() | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |     def _get(self, section, conv, option): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         return conv(self.get(section, option)) | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def getint(self, section, option): | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         return self._get(section, int, option) | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def getfloat(self, section, option): | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         return self._get(section, float, option) | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |     _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True, | 
					
						
							|  |  |  |                        '0': False, 'no': False, 'false': False, 'off': False} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  |     def getboolean(self, section, option): | 
					
						
							| 
									
										
										
										
											2001-10-18 21:57:37 +00:00
										 |  |  |         v = self.get(section, option) | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         if v.lower() not in self._boolean_states: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             raise ValueError, 'Not a boolean: %s' % v | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         return self._boolean_states[v.lower()] | 
					
						
							| 
									
										
										
										
											1997-12-09 16:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-17 18:41:42 +00:00
										 |  |  |     def optionxform(self, optionstr): | 
					
						
							| 
									
										
										
										
											2001-02-09 05:19:09 +00:00
										 |  |  |         return optionstr.lower() | 
					
						
							| 
									
										
										
										
											1999-06-17 18:41:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-10 18:11:00 +00:00
										 |  |  |     def has_option(self, section, option): | 
					
						
							|  |  |  |         """Check for the existence of a given option in a given section.""" | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         if not section or section == DEFAULTSECT: | 
					
						
							|  |  |  |             option = self.optionxform(option) | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             return option in self._defaults | 
					
						
							|  |  |  |         elif section not in self._sections: | 
					
						
							| 
									
										
										
										
											2000-07-10 18:11:00 +00:00
										 |  |  |             return 0 | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2001-02-26 21:55:34 +00:00
										 |  |  |             option = self.optionxform(option) | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             return (option in self._sections[section] | 
					
						
							|  |  |  |                     or option in self._defaults) | 
					
						
							| 
									
										
										
										
											2000-07-10 18:11:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def set(self, section, option, value): | 
					
						
							|  |  |  |         """Set an option.""" | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         if not section or section == DEFAULTSECT: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             sectdict = self._defaults | 
					
						
							| 
									
										
										
										
											2000-07-10 18:11:00 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |                 sectdict = self._sections[section] | 
					
						
							| 
									
										
										
										
											2000-07-10 18:11:00 +00:00
										 |  |  |             except KeyError: | 
					
						
							|  |  |  |                 raise NoSectionError(section) | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         sectdict[self.optionxform(option)] = value | 
					
						
							| 
									
										
										
										
											2000-07-10 18:11:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def write(self, fp): | 
					
						
							|  |  |  |         """Write an .ini-format representation of the configuration state.""" | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         if self._defaults: | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |             fp.write("[%s]\n" % DEFAULTSECT) | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             for (key, value) in self._defaults.items(): | 
					
						
							| 
									
										
										
										
											2002-03-08 18:08:47 +00:00
										 |  |  |                 fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) | 
					
						
							| 
									
										
										
										
											2000-07-10 18:11:00 +00:00
										 |  |  |             fp.write("\n") | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         for section in self._sections: | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |             fp.write("[%s]\n" % section) | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             for (key, value) in self._sections[section].items(): | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |                 if key != "__name__": | 
					
						
							|  |  |  |                     fp.write("%s = %s\n" % | 
					
						
							|  |  |  |                              (key, str(value).replace('\n', '\n\t'))) | 
					
						
							| 
									
										
										
										
											2000-07-10 18:11:00 +00:00
										 |  |  |             fp.write("\n") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-21 05:19:59 +00:00
										 |  |  |     def remove_option(self, section, option): | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  |         """Remove an option.""" | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         if not section or section == DEFAULTSECT: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             sectdict = self._defaults | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |                 sectdict = self._sections[section] | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  |             except KeyError: | 
					
						
							|  |  |  |                 raise NoSectionError(section) | 
					
						
							| 
									
										
										
										
											2001-02-26 21:55:34 +00:00
										 |  |  |         option = self.optionxform(option) | 
					
						
							| 
									
										
										
										
											2002-06-01 14:18:47 +00:00
										 |  |  |         existed = option in sectdict | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  |         if existed: | 
					
						
							| 
									
										
										
										
											2000-12-04 16:29:13 +00:00
										 |  |  |             del sectdict[option] | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  |         return existed | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-21 05:19:59 +00:00
										 |  |  |     def remove_section(self, section): | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  |         """Remove a file section.""" | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         existed = section in self._sections | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         if existed: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |             del self._sections[section] | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         return existed | 
					
						
							| 
									
										
										
										
											2000-07-14 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |     # Regular expressions for parsing section headers and options. | 
					
						
							|  |  |  |     # | 
					
						
							| 
									
										
										
										
											1999-06-17 18:41:42 +00:00
										 |  |  |     SECTCRE = re.compile( | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |         r'\['                                 # [ | 
					
						
							| 
									
										
										
										
											2001-02-14 15:24:17 +00:00
										 |  |  |         r'(?P<header>[^]]+)'                  # very permissive! | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |         r'\]'                                 # ] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											1999-06-17 18:41:42 +00:00
										 |  |  |     OPTCRE = re.compile( | 
					
						
							| 
									
										
										
										
											2002-09-27 16:21:18 +00:00
										 |  |  |         r'(?P<option>[^:=\s][^:=]*)'          # very permissive! | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |         r'\s*(?P<vi>[:=])\s*'                 # any number of space/tab, | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                                               # followed by separator | 
					
						
							|  |  |  |                                               # (either : or =), followed | 
					
						
							|  |  |  |                                               # by any # space/tab | 
					
						
							|  |  |  |         r'(?P<value>.*)$'                     # everything up to eol | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |     def _read(self, fp, fpname): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Parse a sectioned setup file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The sections in setup file contains a title line at the top, | 
					
						
							|  |  |  |         indicated by a name in square brackets (`[]'), plus key/value | 
					
						
							|  |  |  |         options lines, indicated by `name: value' format lines. | 
					
						
							| 
									
										
										
										
											2002-11-06 14:51:20 +00:00
										 |  |  |         Continuations are represented by an embedded newline then | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         leading whitespace.  Blank lines, lines beginning with a '#', | 
					
						
							| 
									
										
										
										
											2002-11-06 14:51:20 +00:00
										 |  |  |         and just about everything else are ignored. | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |         cursect = None                            # None, or a dictionary | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         optname = None | 
					
						
							|  |  |  |         lineno = 0 | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |         e = None                                  # None, or an exception | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         while 1: | 
					
						
							|  |  |  |             line = fp.readline() | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             lineno = lineno + 1 | 
					
						
							|  |  |  |             # comment or blank line? | 
					
						
							| 
									
										
										
										
											2001-02-09 05:19:09 +00:00
										 |  |  |             if line.strip() == '' or line[0] in '#;': | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2002-09-27 16:21:18 +00:00
										 |  |  |             if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR": | 
					
						
							|  |  |  |                 # no leading whitespace | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             # continuation line? | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |             if line[0].isspace() and cursect is not None and optname: | 
					
						
							| 
									
										
										
										
											2001-02-09 05:19:09 +00:00
										 |  |  |                 value = line.strip() | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |                 if value: | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |                     cursect[optname] = "%s\n%s" % (cursect[optname], value) | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |             # a section header or option header? | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                 # is it a section header? | 
					
						
							| 
									
										
										
										
											1999-06-17 18:41:42 +00:00
										 |  |  |                 mo = self.SECTCRE.match(line) | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                 if mo: | 
					
						
							|  |  |  |                     sectname = mo.group('header') | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |                     if sectname in self._sections: | 
					
						
							|  |  |  |                         cursect = self._sections[sectname] | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                     elif sectname == DEFAULTSECT: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |                         cursect = self._defaults | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                     else: | 
					
						
							| 
									
										
										
										
											1998-08-06 18:48:41 +00:00
										 |  |  |                         cursect = {'__name__': sectname} | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |                         self._sections[sectname] = cursect | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                     # So sections can't start with a continuation line | 
					
						
							|  |  |  |                     optname = None | 
					
						
							|  |  |  |                 # no section header in the file? | 
					
						
							|  |  |  |                 elif cursect is None: | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  |                     raise MissingSectionHeaderError(fpname, lineno, `line`) | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                 # an option line? | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											1999-06-17 18:41:42 +00:00
										 |  |  |                     mo = self.OPTCRE.match(line) | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                     if mo: | 
					
						
							| 
									
										
										
										
											2000-02-28 20:59:03 +00:00
										 |  |  |                         optname, vi, optval = mo.group('option', 'vi', 'value') | 
					
						
							| 
									
										
										
										
											2000-03-03 20:43:57 +00:00
										 |  |  |                         if vi in ('=', ':') and ';' in optval: | 
					
						
							| 
									
										
										
										
											2000-02-28 20:59:03 +00:00
										 |  |  |                             # ';' is a comment delimiter only if it follows | 
					
						
							|  |  |  |                             # a spacing character | 
					
						
							| 
									
										
										
										
											2001-02-09 05:19:09 +00:00
										 |  |  |                             pos = optval.find(';') | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |                             if pos != -1 and optval[pos-1].isspace(): | 
					
						
							| 
									
										
										
										
											2000-02-28 20:59:03 +00:00
										 |  |  |                                 optval = optval[:pos] | 
					
						
							| 
									
										
										
										
											2001-02-09 05:19:09 +00:00
										 |  |  |                         optval = optval.strip() | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                         # allow empty values | 
					
						
							|  |  |  |                         if optval == '""': | 
					
						
							|  |  |  |                             optval = '' | 
					
						
							| 
									
										
										
										
											2002-09-27 16:21:18 +00:00
										 |  |  |                         optname = self.optionxform(optname.rstrip()) | 
					
						
							| 
									
										
										
										
											2002-09-27 15:33:11 +00:00
										 |  |  |                         cursect[optname] = optval | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                     else: | 
					
						
							|  |  |  |                         # a non-fatal parsing error occurred.  set up the | 
					
						
							|  |  |  |                         # exception but keep going. the exception will be | 
					
						
							|  |  |  |                         # raised at the end of the file and will contain a | 
					
						
							|  |  |  |                         # list of all bogus lines | 
					
						
							|  |  |  |                         if not e: | 
					
						
							| 
									
										
										
										
											1999-10-04 18:57:27 +00:00
										 |  |  |                             e = ParsingError(fpname) | 
					
						
							| 
									
										
										
										
											1998-07-01 20:41:12 +00:00
										 |  |  |                         e.append(lineno, `line`) | 
					
						
							|  |  |  |         # if any parsing errors occurred, raise an exception | 
					
						
							|  |  |  |         if e: | 
					
						
							|  |  |  |             raise e | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ConfigParser(RawConfigParser): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get(self, section, option, raw=0, vars=None): | 
					
						
							|  |  |  |         """Get an option value for a given section.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         All % interpolations are expanded in the return values, based on the | 
					
						
							|  |  |  |         defaults passed into the constructor, unless the optional argument | 
					
						
							|  |  |  |         `raw' is true.  Additional substitutions may be provided using the | 
					
						
							|  |  |  |         `vars' argument, which must be a dictionary whose contents overrides | 
					
						
							|  |  |  |         any pre-existing defaults. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The section DEFAULT is special. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         d = self._defaults.copy() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             d.update(self._sections[section]) | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             if section != DEFAULTSECT: | 
					
						
							|  |  |  |                 raise NoSectionError(section) | 
					
						
							|  |  |  |         # Update with the entry specific variables | 
					
						
							|  |  |  |         if vars is not None: | 
					
						
							|  |  |  |             d.update(vars) | 
					
						
							|  |  |  |         option = self.optionxform(option) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             value = d[option] | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             raise NoOptionError(option, section) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if raw: | 
					
						
							|  |  |  |             return value | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return self._interpolate(section, option, value, d) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def items(self, section, raw=0, vars=None): | 
					
						
							|  |  |  |         """Return a list of tuples with (name, value) for each option
 | 
					
						
							|  |  |  |         in the section. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         All % interpolations are expanded in the return values, based on the | 
					
						
							|  |  |  |         defaults passed into the constructor, unless the optional argument | 
					
						
							|  |  |  |         `raw' is true.  Additional substitutions may be provided using the | 
					
						
							|  |  |  |         `vars' argument, which must be a dictionary whose contents overrides | 
					
						
							|  |  |  |         any pre-existing defaults. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The section DEFAULT is special. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         d = self._defaults.copy() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             d.update(self._sections[section]) | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             if section != DEFAULTSECT: | 
					
						
							|  |  |  |                 raise NoSectionError(section) | 
					
						
							|  |  |  |         # Update with the entry specific variables | 
					
						
							|  |  |  |         if vars: | 
					
						
							|  |  |  |             d.update(vars) | 
					
						
							| 
									
										
										
										
											2002-10-25 20:41:30 +00:00
										 |  |  |         options = d.keys() | 
					
						
							|  |  |  |         if "__name__" in options: | 
					
						
							|  |  |  |             options.remove("__name__") | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |         if raw: | 
					
						
							| 
									
										
										
										
											2002-10-25 20:41:30 +00:00
										 |  |  |             for option in options: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |                 yield (option, d[option]) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2002-10-25 20:41:30 +00:00
										 |  |  |             for option in options: | 
					
						
							| 
									
										
										
										
											2002-10-25 18:08:18 +00:00
										 |  |  |                 yield (option, | 
					
						
							|  |  |  |                        self._interpolate(section, option, d[option], d)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _interpolate(self, section, option, rawval, vars): | 
					
						
							|  |  |  |         # do the string interpolation | 
					
						
							|  |  |  |         value = rawval | 
					
						
							|  |  |  |         depth = MAX_INTERPOLATION_DEPTH  | 
					
						
							|  |  |  |         while depth:                    # Loop through this until it's done | 
					
						
							|  |  |  |             depth -= 1 | 
					
						
							|  |  |  |             if value.find("%(") != -1: | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     value = value % vars | 
					
						
							|  |  |  |                 except KeyError, key: | 
					
						
							|  |  |  |                     raise InterpolationError(key, option, section, rawval) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |         if value.find("%(") != -1: | 
					
						
							|  |  |  |             raise InterpolationDepthError(option, section, rawval) | 
					
						
							|  |  |  |         return value | 
					
						
							| 
									
										
										
										
											2002-10-25 21:52:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SafeConfigParser(ConfigParser): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _interpolate(self, section, option, rawval, vars): | 
					
						
							|  |  |  |         # do the string interpolation | 
					
						
							|  |  |  |         L = [] | 
					
						
							|  |  |  |         self._interpolate_some(option, L, rawval, section, vars, 1) | 
					
						
							|  |  |  |         return ''.join(L) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _interpvar_match = re.compile(r"%\(([^)]+)\)s").match | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _interpolate_some(self, option, accum, rest, section, map, depth): | 
					
						
							|  |  |  |         if depth > MAX_INTERPOLATION_DEPTH: | 
					
						
							|  |  |  |             raise InterpolationDepthError(option, section, rest) | 
					
						
							|  |  |  |         while rest: | 
					
						
							|  |  |  |             p = rest.find("%") | 
					
						
							|  |  |  |             if p < 0: | 
					
						
							|  |  |  |                 accum.append(rest) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |             if p > 0: | 
					
						
							|  |  |  |                 accum.append(rest[:p]) | 
					
						
							|  |  |  |                 rest = rest[p:] | 
					
						
							|  |  |  |             # p is no longer used | 
					
						
							|  |  |  |             c = rest[1:2] | 
					
						
							|  |  |  |             if c == "%": | 
					
						
							|  |  |  |                 accum.append("%") | 
					
						
							|  |  |  |                 rest = rest[2:] | 
					
						
							|  |  |  |             elif c == "(": | 
					
						
							|  |  |  |                 m = self._interpvar_match(rest) | 
					
						
							|  |  |  |                 if m is None: | 
					
						
							|  |  |  |                     raise InterpolationSyntaxError( | 
					
						
							|  |  |  |                         "bad interpolation variable syntax at: %r" % rest) | 
					
						
							|  |  |  |                 var = m.group(1) | 
					
						
							|  |  |  |                 rest = rest[m.end():] | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     v = map[var] | 
					
						
							|  |  |  |                 except KeyError: | 
					
						
							|  |  |  |                     raise InterpolationError( | 
					
						
							|  |  |  |                         "no value found for %r" % var) | 
					
						
							|  |  |  |                 if "%" in v: | 
					
						
							|  |  |  |                     self._interpolate_some(option, accum, v, | 
					
						
							|  |  |  |                                            section, map, depth + 1) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     accum.append(v) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise InterpolationSyntaxError( | 
					
						
							|  |  |  |                     "'%' must be followed by '%' or '('") |