From 60b2e5ebf0b074436aaec91409d9863d015b9a73 Mon Sep 17 00:00:00 2001 From: Ludovico Cavedon Date: Sun, 18 Apr 2010 23:36:58 -0700 Subject: Imported Upstream version 0.3.2 --- iniparse/__init__.py | 15 ++++++++++++-- iniparse/ini.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) (limited to 'iniparse') diff --git a/iniparse/__init__.py b/iniparse/__init__.py index 350b7a7..1a267e6 100644 --- a/iniparse/__init__.py +++ b/iniparse/__init__.py @@ -3,11 +3,22 @@ # Copyright (c) 2007 Tim Lauridsen # All Rights Reserved. See LICENSE-PSF & LICENSE for details. -from ini import INIConfig +from ini import INIConfig, tidy, change_comment_syntax from config import BasicConfig, ConfigNamespace from compat import RawConfigParser, ConfigParser, SafeConfigParser +from ConfigParser import DuplicateSectionError, \ + NoSectionError, NoOptionError, \ + InterpolationMissingOptionError, \ + InterpolationDepthError, \ + InterpolationSyntaxError, \ + DEFAULTSECT, MAX_INTERPOLATION_DEPTH + __all__ = [ - 'INIConfig', 'BasicConfig', 'ConfigNamespace', + 'BasicConfig', 'ConfigNamespace', + 'INIConfig', 'tidy', 'change_comment_syntax', 'RawConfigParser', 'ConfigParser', 'SafeConfigParser', + 'DuplicateSectionError', 'NoSectionError', 'NoOptionError', + 'InterpolationMissingOptionError', 'InterpolationDepthError', + 'InterpolationSyntaxError', 'DEFAULTSECT', 'MAX_INTERPOLATION_DEPTH', ] diff --git a/iniparse/ini.py b/iniparse/ini.py index d58c38f..f0e7ec2 100644 --- a/iniparse/ini.py +++ b/iniparse/ini.py @@ -21,6 +21,7 @@ Example: >>> print cfg['foo-ext'].special 1 >>> cfg.foo.newopt = 'hi!' + >>> cfg.baz.enabled = 0 >>> print cfg # configure foo-application @@ -30,6 +31,9 @@ Example: newopt = hi! [foo-ext] special = 1 + + [baz] + enabled = 0 """ @@ -41,6 +45,7 @@ import re from ConfigParser import DEFAULTSECT, ParsingError, MissingSectionHeaderError import config +import compat class LineType(object): line = None @@ -158,6 +163,14 @@ class OptionLine(LineType): parse = classmethod(parse) +def change_comment_syntax(comment_chars='%;#', allow_rem=False): + comment_chars = re.sub(r'([\]\-\^])', r'\\\1', comment_chars) + regex = r'^(?P[%s]' % comment_chars + if allow_rem: + regex += '|[rR][eE][mM]' + regex += r')(?P.*)$' + CommentLine.regex = re.compile(regex) + class CommentLine(LineType): regex = re.compile(r'^(?P[;#]|[rR][eE][mM])' r'(?P.*)$') @@ -628,3 +641,48 @@ class INIConfig(config.ConfigNamespace): if exc: raise exc + +def tidy(cfg): + """Clean up blank lines. + + This functions makes the configuration look clean and + handwritten - consecutive empty lines and empty lines at + the start of the file are removed, and one is guaranteed + to be at the end of the file. + """ + + if isinstance(cfg, compat.RawConfigParser): + cfg = cfg.data + cont = cfg._data.contents + i = 1 + while i < len(cont): + if isinstance(cont[i], LineContainer): + tidy_section(cont[i]) + i += 1 + elif (isinstance(cont[i-1], EmptyLine) and + isinstance(cont[i], EmptyLine)): + del cont[i] + else: + i += 1 + + # Remove empty first line + if cont and isinstance(cont[0], EmptyLine): + del cont[0] + + # Ensure a last line + if cont and not isinstance(cont[-1], EmptyLine): + cont.append(EmptyLine()) + +def tidy_section(lc): + cont = lc.contents + i = 1 + while i < len(cont): + if (isinstance(cont[i-1], EmptyLine) and + isinstance(cont[i], EmptyLine)): + del cont[i] + else: + i += 1 + + # Remove empty first line + if len(cont) > 1 and isinstance(cont[1], EmptyLine): + del cont[1] -- cgit v1.2.3