Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions docs/configobj.html
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ <h1><a class="toc-backref" href="#id37">5&nbsp;&nbsp;&nbsp;ConfigObj specificati
<span class="n">interpolation</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">raise_errors</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">list_values</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">create_empty</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">file_error</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">stringify</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">indent_type</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">default_encoding</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">unrepr</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
<span class="n">write_empty_values</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">_inspec</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">write_empty_values</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">_inspec</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">duplicate_values</span><span class="o">=</span><span class="bp">DUPLICATE_VALUES_THROW_ERROR</span><span class="p">)</span>
</pre></div>
<p>Many of the keyword arguments are available as attributes after the config file has been
parsed.</p>
Expand Down Expand Up @@ -586,6 +586,14 @@ <h1><a class="toc-backref" href="#id37">5&nbsp;&nbsp;&nbsp;ConfigObj specificati
for this argument as well as <tt class="docutils literal">list_values=False</tt>.</p>
</blockquote>
</li>
<li><p class="first">'duplicate_values': <tt class="docutils literal">DUPLICATE_VALUES_THROW_ERROR</tt></p>
<blockquote>
<p>Used internally by ConfigObj when handling duplicate keys within config file. Default value is DUPLICATE_VALUES_THROW_ERROR which
raises exception. To allow processing files with duplicate keys use one of configuration values
<tt class="docutils literal">DUPLICATE_VALUES_FIRST</tt> or <tt class="docutils literal">DUPLICATE_VALUES_LAST</tt> where only the
first or last (respectivelly) occurence of the duplicated key will be taken into account.</p>
</blockquote>
</li>
</ul>
<div class="section" id="methods">
<h2><a class="toc-backref" href="#id38">5.1&nbsp;&nbsp;&nbsp;Methods</a></h2>
Expand Down Expand Up @@ -1541,7 +1549,8 @@ <h1><a class="toc-backref" href="#id66">8&nbsp;&nbsp;&nbsp;Exceptions</a></h1>
</li>
<li><p class="first"><tt class="docutils literal">DuplicateError</tt></p>
<blockquote>
<p>The keyword or section specified already exists.</p>
<p>The keyword or section specified already exists. Raising of this exception can be supressed by setting
<tt class="docutils literal">duplicate_values</tt>. </p>
</blockquote>
</li>
<li><p class="first"><tt class="docutils literal">ConfigspecError</tt></p>
Expand Down
27 changes: 20 additions & 7 deletions src/configobj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def match_utf8(encoding):
DEFAULT_INTERPOLATION = 'configparser'
DEFAULT_INDENT_TYPE = ' '
MAX_INTERPOL_DEPTH = 10
DUPLICATE_VALUES_THROW_ERROR = 0
DUPLICATE_VALUES_FIRST = 1
DUPLICATE_VALUES_LAST = 2

OPTION_DEFAULTS = {
'interpolation': True,
Expand All @@ -125,6 +128,7 @@ def match_utf8(encoding):
'default_encoding': None,
'unrepr': False,
'write_empty_values': False,
'duplicate_values' : DUPLICATE_VALUES_THROW_ERROR,
}

# this could be replaced if six is used for compatibility, or there are no
Expand Down Expand Up @@ -1145,7 +1149,7 @@ def __init__(self, infile=None, options=None, configspec=None, encoding=None,
interpolation=True, raise_errors=False, list_values=True,
create_empty=False, file_error=False, stringify=True,
indent_type=None, default_encoding=None, unrepr=False,
write_empty_values=False, _inspec=False):
write_empty_values=False, _inspec=False, duplicate_values=DUPLICATE_VALUES_THROW_ERROR):
"""
Parse a config file or create a config file object.

Expand All @@ -1167,7 +1171,7 @@ def __init__(self, infile=None, options=None, configspec=None, encoding=None,
'create_empty': create_empty, 'file_error': file_error,
'stringify': stringify, 'indent_type': indent_type,
'default_encoding': default_encoding, 'unrepr': unrepr,
'write_empty_values': write_empty_values}
'write_empty_values': write_empty_values, 'duplicate_values': duplicate_values}

if options is None:
options = _options
Expand Down Expand Up @@ -1315,6 +1319,7 @@ def _initialise(self, options=None):
self.indent_type = options['indent_type']
self.encoding = options['encoding']
self.default_encoding = options['default_encoding']
self.duplicate_values = options['duplicate_values']
self.BOM = False
self.newlines = None
self.write_empty_values = options['write_empty_values']
Expand Down Expand Up @@ -1655,11 +1660,19 @@ def _parse(self, infile):
#
key = self._unquote(key)
if key in this_section:
self._handle_error(
'Duplicate keyword name',
DuplicateError, infile, cur_index)
continue
# add the key.
if self.duplicate_values == DUPLICATE_VALUES_THROW_ERROR :
self._handle_error(
'Duplicate keyword name',
DuplicateError, infile, cur_index)
continue
if self.duplicate_values == DUPLICATE_VALUES_FIRST :
continue
if self.duplicate_values == DUPLICATE_VALUES_LAST :
this_section.__setitem__(key, value, unrepr=True)
this_section.inline_comments[key] = comment
this_section.comments[key] = comment_list
continue
# add the key.
# we set unrepr because if we have got this far we will never
# be creating a new section
this_section.__setitem__(key, value, unrepr=True)
Expand Down