Skip to content

Commit

Permalink
Initializing file with comment before interface section is handled pr…
Browse files Browse the repository at this point in the history
…operly again
  • Loading branch information
Henri committed Nov 10, 2020
1 parent 3d4098a commit cd018e1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ All notable changes to this project are documented in this file.

- n/a

## [0.2.1] - 2020-11-11

### Fixed

- Initializing file with comment before interface section is handled properly again.

## [0.2.0] - 2020-11-10

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup_kwargs = {
'name': 'wgconfig',
'version': '0.2.0',
'version': '0.2.1',
'author': 'Dirk Henrici',
'author_email': '[email protected]',
'description': 'parsing and writing WireGuard configuration files',
Expand Down
6 changes: 4 additions & 2 deletions src/wgconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ def close_section(section, section_data):
section_data = dict()
section = line[1:].partition(']')[0].lower()
if last_empty_line_in_section is None:
section_data[self.SECTION_FIRSTLINE] = section_data[self.SECTION_LASTLINE] = [i]
section_data[self.SECTION_FIRSTLINE] = [i]
else:
section_data[self.SECTION_FIRSTLINE] = section_data[self.SECTION_LASTLINE] = [last_empty_line_in_section + 1]
section_data[self.SECTION_FIRSTLINE] = [last_empty_line_in_section + 1]
last_empty_line_in_section = None
section_data[self.SECTION_LASTLINE] = [i]
if not section in ['interface', 'peer']:
raise ValueError(f'Unsupported section [{section}] in line {i}')
elif line.startswith('#'):
Expand Down Expand Up @@ -180,6 +181,7 @@ def get_sectioninfo(self, key):
def add_attr(self, key, attr, value, leading_comment=None, append_as_line=False):
"""Adds an attribute/value pair to the given peer ("None" for adding an interface attribute)"""
section_firstline, section_lastline = self.get_sectioninfo(key)
print(key, section_firstline, section_lastline, dict(enumerate(self.lines))) # ***
if leading_comment is not None:
if leading_comment.strip()[0] != '#':
raise ValueError('A comment needs to start with a "#"')
Expand Down
2 changes: 1 addition & 1 deletion test/test_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_initialize_file_with_comment(setup_testconfig1):
wc.initialize_file('# Leading comment for interface section')
output_data(wc)
interface = {'_index_firstline': 0,
'_index_lastline': 0}
'_index_lastline': 1}
del wc.interface['_rawdata']
assert wc.interface == interface
for peer in wc.peers.values():
Expand Down

0 comments on commit cd018e1

Please sign in to comment.