summaryrefslogtreecommitdiff
path: root/iniparse
diff options
context:
space:
mode:
authorLudovico Cavedon <ludovico.cavedon@gmail.com>2010-04-18 23:36:58 -0700
committerLudovico Cavedon <ludovico.cavedon@gmail.com>2010-04-18 23:36:58 -0700
commit60b2e5ebf0b074436aaec91409d9863d015b9a73 (patch)
treea1bc02ee02e081bf37b30ff43c9628832b9afc52 /iniparse
parent8cbb16c4830d341deb19d77d8ff9c10813460e46 (diff)
Imported Upstream version 0.3.2
Diffstat (limited to 'iniparse')
-rw-r--r--iniparse/__init__.py15
-rw-r--r--iniparse/ini.py58
2 files changed, 71 insertions, 2 deletions
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 <tla@rasmil.dk>
# 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
+ <BLANKLINE>
+ [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<csep>[%s]' % comment_chars
+ if allow_rem:
+ regex += '|[rR][eE][mM]'
+ regex += r')(?P<comment>.*)$'
+ CommentLine.regex = re.compile(regex)
+
class CommentLine(LineType):
regex = re.compile(r'^(?P<csep>[;#]|[rR][eE][mM])'
r'(?P<comment>.*)$')
@@ -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]