From bfa3f6b673d80de48c2bb9a85cf942a958b97374 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Wed, 1 Jul 1998 20:41:12 +0000 Subject: Several changes: 1. Convert to using re module 2. Added two new exception classes a. MissingSectionHeaderError which signals an early parsing exception when options appear in the file before any section header. Previously a bogus TypeError was thrown deeper down. b. ParsingError which collates any non-fatal parsing errors. ConfigParser.read() will raise this after the entire file was parsed if any errors occurred during parsing (client could just catch the exception and continue, because the ConfigParser instance would still be initialized with the valid data). (small note: Error.__msg => Error._msg) 3. ConfigParser.__read() now uses re which has the following minor semantic change: underscore is now allowed in section header and option name. Also, because of the old regexps, theoretically. Fixed continuation line bug reported by F. Lundh. 4. It seemed that the old ConfigParser automatically added the option `name' to every section, which contained the name of the section. This seemed bogus to me so I took it out. --- Lib/ConfigParser.py | 130 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 90 insertions(+), 40 deletions(-) diff --git a/Lib/ConfigParser.py b/Lib/ConfigParser.py index 957222c..f22306a 100644 --- a/Lib/ConfigParser.py +++ b/Lib/ConfigParser.py @@ -2,10 +2,11 @@ A setup file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in -the style of rfc822. +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. -The option values can contain format strings which refer to other -values in the same section, or values in a special [DEFAULT] section. For example: something: %(dir)s/whatever @@ -55,14 +56,7 @@ ConfigParser -- responsible for for parsing a list of import sys import string -import regex -from types import ListType - - -SECTHEAD_RE = "^\[\([-A-Za-z0-9]*\)\][" + string.whitespace + "]*$" -secthead_cre = regex.compile(SECTHEAD_RE) -OPTION_RE = "^\([-A-Za-z0-9.]+\)\(:\|[" + string.whitespace + "]*=\)\(.*\)$" -option_cre = regex.compile(OPTION_RE) +import re DEFAULTSECT = "DEFAULT" @@ -71,9 +65,9 @@ DEFAULTSECT = "DEFAULT" # exception classes class Error: def __init__(self, msg=''): - self.__msg = msg + self._msg = msg def __repr__(self): - return self.__msg + return self._msg class NoSectionError(Error): def __init__(self, section): @@ -101,6 +95,26 @@ class InterpolationError(Error): self.option = option self.section = section +class MissingSectionHeaderError(Error): + 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 + +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) + class ConfigParser: @@ -159,7 +173,8 @@ class ConfigParser: """Get an option value for a given section. All % interpolations are expanded in the return values, based - on the defaults passed into the constructor. + on the defaults passed into the constructor, unless the optional + argument `raw' is true. The section DEFAULT is special. """ @@ -201,6 +216,24 @@ class ConfigParser: raise ValueError, 'Not a boolean: %s' % v return val + # + # Regular expressions for parsing section headers and options. Note a + # slight semantic change from the previous version, because of the use + # of \w, _ is allowed in section header names. + __SECTCRE = re.compile( + r'\[' # [ + r'(?P
[-\w]+)' # `-', `_' or any alphanum + r'\]' # ] + ) + __OPTCRE = re.compile( + r'(?P