Skip to content

Commit

Permalink
Merge pull request #170 from endlessm/unsorted-option-merge
Browse files Browse the repository at this point in the history
ImageConfigParser: Don't sort merged values
  • Loading branch information
wjt authored Nov 25, 2024
2 parents ff6c562 + 59980bc commit 057c13c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
12 changes: 5 additions & 7 deletions lib/eib.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,25 +242,23 @@ def _merge_option(self, section_pattern, option):
opt)
yield (section, opt)
else:
add_vals = Counter()
values = Counter()
for opt in add_opts:
logger.debug('Adding %s %s values from %s', section,
option, opt)
add_vals.update(sect[opt].split())
values.update(sect[opt].split())
yield (section, opt)

del_vals = Counter()
for opt in del_opts:
logger.debug('Removing %s %s values from %s', section,
option, opt)
del_vals.update(sect[opt].split())
values.subtract(sect[opt].split())
yield (section, opt)

# Set the option to the difference of the counters.
# Set the option to the keys that have positive values.
# Merge the values together with newlines like they were
# in the original configuration.
vals = add_vals - del_vals
sect[option] = '\n'.join(sorted(vals.keys()))
sect[option] = '\n'.join(k for k, v in values.items() if v > 0)

def copy(self):
"""Create a new instance from this one"""
Expand Down
8 changes: 4 additions & 4 deletions tests/eib/test_image_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def test_merged_option(config):

assert set(sect) == {'opt'}

# The values will be sorted and newline separated
assert sect['opt'] == 'baz\nfoo'
# The values will be newline separated in the order they appeared.
assert sect['opt'] == 'foo\nbaz'

# Now that the merged option exists, it will override any further
# add/del.
Expand All @@ -129,7 +129,7 @@ def test_merged_option(config):
config.merge()

assert set(sect) == {'opt'}
assert sect['opt'] == 'baz\nfoo'
assert sect['opt'] == 'foo\nbaz'


def test_merged_option_interpolation(config):
Expand Down Expand Up @@ -277,7 +277,7 @@ def test_merged_files(tmp_path, config):
config.merge()

assert set(config['sect']) == {'opt'}
assert config['sect']['opt'] == 'baz\nfoo'
assert config['sect']['opt'] == 'foo\nbaz'
assert set(config['sect-a']) == {'opt'}
assert config['sect-a']['opt'] == ''
assert set(config['sect-b']) == {'opt'}
Expand Down

0 comments on commit 057c13c

Please sign in to comment.