summaryrefslogtreecommitdiff
path: root/iniparse
diff options
context:
space:
mode:
Diffstat (limited to 'iniparse')
-rw-r--r--iniparse/__init__.py20
-rw-r--r--iniparse/compat.py30
-rw-r--r--iniparse/config.py16
-rw-r--r--iniparse/configparser.py7
-rw-r--r--iniparse/ini.py20
-rw-r--r--iniparse/utils.py4
6 files changed, 55 insertions, 42 deletions
diff --git a/iniparse/__init__.py b/iniparse/__init__.py
index 8de756f..7193f92 100644
--- a/iniparse/__init__.py
+++ b/iniparse/__init__.py
@@ -3,17 +3,17 @@
# Copyright (c) 2007 Tim Lauridsen <tla@rasmil.dk>
# All Rights Reserved. See LICENSE-PSF & LICENSE for details.
-from ini import INIConfig, change_comment_syntax
-from config import BasicConfig, ConfigNamespace
-from compat import RawConfigParser, ConfigParser, SafeConfigParser
-from utils import tidy
+from .ini import INIConfig, change_comment_syntax
+from .config import BasicConfig, ConfigNamespace
+from .compat import RawConfigParser, ConfigParser, SafeConfigParser
+from .utils import tidy
-from ConfigParser import DuplicateSectionError, \
- NoSectionError, NoOptionError, \
- InterpolationMissingOptionError, \
- InterpolationDepthError, \
- InterpolationSyntaxError, \
- DEFAULTSECT, MAX_INTERPOLATION_DEPTH
+from .configparser import DuplicateSectionError, \
+ NoSectionError, NoOptionError, \
+ InterpolationMissingOptionError, \
+ InterpolationDepthError, \
+ InterpolationSyntaxError, \
+ DEFAULTSECT, MAX_INTERPOLATION_DEPTH
__all__ = [
'BasicConfig', 'ConfigNamespace',
diff --git a/iniparse/compat.py b/iniparse/compat.py
index db89ed8..f95c25c 100644
--- a/iniparse/compat.py
+++ b/iniparse/compat.py
@@ -12,19 +12,21 @@ The underlying INIConfig object can be accessed as cfg.data
"""
import re
-from ConfigParser import DuplicateSectionError, \
- NoSectionError, NoOptionError, \
- InterpolationMissingOptionError, \
- InterpolationDepthError, \
- InterpolationSyntaxError, \
- DEFAULTSECT, MAX_INTERPOLATION_DEPTH
+from .configparser import DuplicateSectionError, \
+ NoSectionError, NoOptionError, \
+ InterpolationMissingOptionError, \
+ InterpolationDepthError, \
+ InterpolationSyntaxError, \
+ DEFAULTSECT, MAX_INTERPOLATION_DEPTH
# These are imported only for compatiability.
# The code below does not reference them directly.
-from ConfigParser import Error, InterpolationError, \
- MissingSectionHeaderError, ParsingError
+from .configparser import Error, InterpolationError, \
+ MissingSectionHeaderError, ParsingError
-import ini
+import six
+
+from . import ini
class RawConfigParser(object):
def __init__(self, defaults=None, dict_type=dict):
@@ -56,7 +58,7 @@ class RawConfigParser(object):
# The default section is the only one that gets the case-insensitive
# treatment - so it is special-cased here.
if section.lower() == "default":
- raise ValueError, 'Invalid section name: %s' % section
+ raise ValueError('Invalid section name: %s' % section)
if self.has_section(section):
raise DuplicateSectionError(section)
@@ -88,7 +90,7 @@ class RawConfigParser(object):
filename may also be given.
"""
files_read = []
- if isinstance(filenames, basestring):
+ if isinstance(filenames, six.string_types):
filenames = [filenames]
for filename in filenames:
try:
@@ -143,7 +145,7 @@ class RawConfigParser(object):
def getboolean(self, section, option):
v = self.get(section, option)
if v.lower() not in self._boolean_states:
- raise ValueError, 'Not a boolean: %s' % v
+ raise ValueError('Not a boolean: %s' % v)
return self._boolean_states[v.lower()]
def has_option(self, section, option):
@@ -234,7 +236,7 @@ class ConfigParser(RawConfigParser):
if "%(" in value:
try:
value = value % vars
- except KeyError, e:
+ except KeyError as e:
raise InterpolationMissingOptionError(
option, section, rawval, e.args[0])
else:
@@ -283,7 +285,7 @@ class SafeConfigParser(ConfigParser):
_badpercent_re = re.compile(r"%[^%]|%$")
def set(self, section, option, value):
- if not isinstance(value, basestring):
+ if not isinstance(value, six.string_types):
raise TypeError("option values must be strings")
# check for bad percent signs:
# first, replace all "good" interpolations
diff --git a/iniparse/config.py b/iniparse/config.py
index 5cfa2ea..3b28549 100644
--- a/iniparse/config.py
+++ b/iniparse/config.py
@@ -143,7 +143,7 @@ class BasicConfig(ConfigNamespace):
>>> n.aaa = 42
>>> del n.x
- >>> print n
+ >>> print(n)
aaa = 42
name.first = paramjit
name.last = oberoi
@@ -152,7 +152,7 @@ class BasicConfig(ConfigNamespace):
>>> isinstance(n.name, ConfigNamespace)
True
- >>> print n.name
+ >>> print(n.name)
first = paramjit
last = oberoi
>>> sorted(list(n.name))
@@ -160,7 +160,7 @@ class BasicConfig(ConfigNamespace):
Finally, values can be read from a file as follows:
- >>> from StringIO import StringIO
+ >>> from six import StringIO
>>> sio = StringIO('''
... # comment
... ui.height = 100
@@ -171,7 +171,7 @@ class BasicConfig(ConfigNamespace):
... ''')
>>> n = BasicConfig()
>>> n._readfp(sio)
- >>> print n
+ >>> print(n)
complexity = medium
data.secret.password = goodness=gracious me
have_python
@@ -199,7 +199,7 @@ class BasicConfig(ConfigNamespace):
def __str__(self, prefix=''):
lines = []
- keys = self._data.keys()
+ keys = list(self._data.keys())
keys.sort()
for name in keys:
value = self._data[name]
@@ -258,7 +258,7 @@ def update_config(target, source):
>>> n.ui.display_clock = True
>>> n.ui.display_qlength = True
>>> n.ui.width = 150
- >>> print n
+ >>> print(n)
playlist.expand_playlist = True
ui.display_clock = True
ui.display_qlength = True
@@ -267,7 +267,7 @@ def update_config(target, source):
>>> from iniparse import ini
>>> i = ini.INIConfig()
>>> update_config(i, n)
- >>> print i
+ >>> print(i)
[playlist]
expand_playlist = True
<BLANKLINE>
@@ -277,7 +277,7 @@ def update_config(target, source):
width = 150
"""
- for name in source:
+ for name in sorted(source):
value = source[name]
if isinstance(value, ConfigNamespace):
if name in target:
diff --git a/iniparse/configparser.py b/iniparse/configparser.py
new file mode 100644
index 0000000..c543d50
--- /dev/null
+++ b/iniparse/configparser.py
@@ -0,0 +1,7 @@
+try:
+ from ConfigParser import *
+ # not all objects get imported with __all__
+ from ConfigParser import Error, InterpolationMissingOptionError
+except ImportError:
+ from configparser import *
+ from configparser import Error, InterpolationMissingOptionError
diff --git a/iniparse/ini.py b/iniparse/ini.py
index 408354d..052d9e9 100644
--- a/iniparse/ini.py
+++ b/iniparse/ini.py
@@ -7,7 +7,7 @@
Example:
- >>> from StringIO import StringIO
+ >>> from six import StringIO
>>> sio = StringIO('''# configure foo-application
... [foo]
... bar1 = qualia
@@ -16,14 +16,14 @@ Example:
... special = 1''')
>>> cfg = INIConfig(sio)
- >>> print cfg.foo.bar1
+ >>> print(cfg.foo.bar1)
qualia
- >>> print cfg['foo-ext'].special
+ >>> print(cfg['foo-ext'].special)
1
>>> cfg.foo.newopt = 'hi!'
>>> cfg.baz.enabled = 0
- >>> print cfg
+ >>> print(cfg)
# configure foo-application
[foo]
bar1 = qualia
@@ -42,9 +42,11 @@ Example:
# Backward-compatiable with ConfigParser
import re
-from ConfigParser import DEFAULTSECT, ParsingError, MissingSectionHeaderError
+from .configparser import DEFAULTSECT, ParsingError, MissingSectionHeaderError
-import config
+import six
+
+from . import config
class LineType(object):
line = None
@@ -278,6 +280,8 @@ class LineContainer(object):
value = property(get_value, set_value)
def __str__(self):
+ for c in self.contents:
+ pass#print(c.__str__())
s = [x.__str__() for x in self.contents]
return '\n'.join(s)
@@ -465,7 +469,7 @@ class INIConfig(config.ConfigNamespace):
self._sections = {}
if defaults is None: defaults = {}
self._defaults = INISection(LineContainer(), optionxformsource=self)
- for name, value in defaults.iteritems():
+ for name, value in defaults.items():
self._defaults[name] = value
if fp is not None:
self._readfp(fp)
@@ -551,7 +555,7 @@ class INIConfig(config.ConfigNamespace):
for line in readline_iterator(fp):
# Check for BOM on first line
- if linecount == 0 and isinstance(line, unicode):
+ if linecount == 0 and isinstance(line, six.text_type):
if line[0] == u'\ufeff':
line = line[1:]
self._bom = True
diff --git a/iniparse/utils.py b/iniparse/utils.py
index 829fc28..f8b773a 100644
--- a/iniparse/utils.py
+++ b/iniparse/utils.py
@@ -1,5 +1,5 @@
-import compat
-from ini import LineContainer, EmptyLine
+from . import compat
+from .ini import LineContainer, EmptyLine
def tidy(cfg):
"""Clean up blank lines.