Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Allow non-strings to be used for interpolation #169

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
16 changes: 10 additions & 6 deletions src/configobj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ def recursive_interpolate(key, value, section, backtrail):
replacement = v
else:
# Further interpolation may be needed to obtain final value
replacement = recursive_interpolate(k, v, s, backtrail)
replacement = recursive_interpolate(
k, s.main._write_value(v), s, backtrail)
# Replace the matched string with its final value
start, end = match.span()
value = ''.join((value[:start], replacement, value[end:]))
Expand Down Expand Up @@ -1943,17 +1944,20 @@ def _set_configspec(self, section, copy):

def _write_line(self, indent_string, entry, this_entry, comment):
"""Write an individual line, for the write method"""
# NOTE: the calls to self._quote here handles non-StringType values.
if not self.unrepr:
val = self._decode_element(self._quote(this_entry))
else:
val = repr(this_entry)
val = self._write_value(this_entry)
return '%s%s%s%s%s' % (indent_string,
self._decode_element(self._quote(entry, multiline=False)),
self._a_to_u(' = '),
val,
self._decode_element(comment))

def _write_value(self, this_entry):
# NOTE: the calls to self._quote here handles non-StringType values.
if not self.unrepr:
val = self._decode_element(self._quote(this_entry))
else:
val = repr(this_entry)
return val

def _write_marker(self, indent_string, depth, entry, comment):
"""Write a section marker line"""
Expand Down
29 changes: 28 additions & 1 deletion src/tests/test_configobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,14 +916,21 @@ def config_parser_cfg(self):
'b': 'goodbye',
'userdir': r'c:\\home',
'c': '%(d)s',
'd': '%(c)s'
'd': '%(c)s',
'f': 45.5,
'list1': [1, 2, 3],
'list2': ['foo', 'bar', 'baz']
}
cfg['section'] = {
'a': r'%(datadir)s\\some path\\file.py',
'b': r'%(userdir)s\\some path\\file.py',
'c': 'Yo %(a)s',
'd': '%(not_here)s',
'e': '%(e)s',
'ff': 'Some float: %(f)s',
'report1': 'list 1: %(list1)s',
'report2': '%(report1)s and list 2: %(list2)s',
'listreference': '%(list1)s'
}
cfg['section']['DEFAULT'] = {
'datadir': r'c:\\silly_test',
Expand All @@ -939,6 +946,10 @@ def template_cfg(self, cfg_contents):
'keyword 2' = 'value 2'
reference = ${keyword1}
foo = 123
f = 45.5
list1 = 1, 2, 3
list2 = foo, bar, baz


[ section ]
templatebare = $keyword1/foo
Expand All @@ -950,6 +961,10 @@ def template_cfg(self, cfg_contents):
with_several = $keyword1/$reference/$keyword1
configparsersample = %(keyword 2)sconfig
deep = ${reference}
ff = Some float: ${f}
report1 = list 1: ${list1}
report2 = $report1 and list 2: $list2
listreference = $list1

[[DEFAULT]]
baz = $foo
Expand All @@ -967,6 +982,12 @@ def test_interpolation(self, config_parser_cfg):
assert test_section['a'] == r'c:\\silly_test\\some path\\file.py'
assert test_section['b'] == r'c:\\home\\some path\\file.py'
assert test_section['c'] == r'Yo c:\\silly_test\\some path\\file.py'
assert test_section['ff'] == r'Some float: 45.5'
assert test_section['report1'] == r'list 1: 1, 2, 3'
assert test_section['report2'] == r'list 1: 1, 2, 3 and list 2: foo, bar, baz'
# FIXME: Hmmm, what should this do, actually?
# Could be '1, 2, 3'? or ['1', '2', '3']? or something else?
assert test_section['listreference'] == '1, 2, 3'

def test_interpolation_turned_off(self, config_parser_cfg):
config_parser_cfg.interpolation = False
Expand Down Expand Up @@ -1000,6 +1021,12 @@ def test_template_interpolation(self, template_cfg):
assert test_sec['sub-section']['quux'] == '123 + $foo + 123'
assert (test_sec['sub-section']['sub-sub-section']['convoluted'] ==
'$foo + 123 + 123 + $foo + 123 + $foo')
assert test_sec['ff'] == r'Some float: 45.5'
assert test_sec['report1'] == r'list 1: 1, 2, 3'
assert test_sec['report2'] == r'list 1: 1, 2, 3 and list 2: foo, bar, baz'
# FIXME: Hmmm, what should this do, actually?
# Could be '1, 2, 3'? or ['1', '2', '3']? or something else?
assert test_sec['listreference'] == '1, 2, 3'


class TestQuotes(object):
Expand Down