-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend Python version compatibility (now Python2.7+)
- Loading branch information
Henri
committed
Nov 15, 2020
1 parent
cd018e1
commit f5a41bf
Showing
5 changed files
with
37 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
|
||
setup_kwargs = { | ||
'name': 'wgconfig', | ||
'version': '0.2.1', | ||
'version': '0.2.2', | ||
'author': 'Dirk Henrici', | ||
'author_email': '[email protected]', | ||
'description': 'parsing and writing WireGuard configuration files', | ||
|
@@ -17,15 +17,18 @@ | |
'packages': setuptools.find_packages(), | ||
'classifiers': [ | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 3', | ||
'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)', | ||
'Operating System :: POSIX :: Linux', | ||
'Development Status :: 5 - Production/Stable', | ||
'Development Status :: 4 - Beta', | ||
#'Development Status :: 5 - Production/Stable', | ||
'Intended Audience :: System Administrators', | ||
'Intended Audience :: Developers', | ||
'Intended Audience :: Information Technology' | ||
], | ||
'python_requires': '>=3.5', | ||
'python_requires': '>=2.7', | ||
'extras_require': { | ||
':python_version == "2.7"': ['future'] | ||
}, | ||
'keywords': 'WireGuard configuration config wg', | ||
'project_urls': { | ||
'Repository': 'https://www.github.com/towalink/wgconfig', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,15 @@ | |
|
||
"""wgconfig.py: A class for parsing and writing Wireguard configuration files.""" | ||
|
||
# The following imports are for Python2 support only | ||
from __future__ import with_statement | ||
from __future__ import absolute_import | ||
from __future__ import print_function | ||
from builtins import str | ||
from builtins import range | ||
from io import open | ||
|
||
|
||
__author__ = "Dirk Henrici" | ||
__license__ = "AGPL" # + author has right to release in parallel under different licenses | ||
__email__ = "[email protected]" | ||
|
@@ -10,7 +19,7 @@ | |
import os | ||
|
||
|
||
class WGConfig(object): | ||
class WGConfig(): | ||
"""A class for parsing and writing Wireguard configuration files""" | ||
SECTION_FIRSTLINE = '_index_firstline' | ||
SECTION_LASTLINE = '_index_lastline' | ||
|
@@ -61,6 +70,7 @@ def parse_line(line): | |
attr = attr.strip() | ||
parts = value.partition('#') | ||
value = parts[0].strip() # strip comments and whitespace | ||
value = str(value) # this line is for Python2 support only | ||
comment = parts[1] + parts[2] | ||
if value.isnumeric(): | ||
value = [int(value)] | ||
|
@@ -111,7 +121,7 @@ def close_section(section, section_data): | |
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}') | ||
raise ValueError('Unsupported section [{0}] in line {1}'.format(section, i)) | ||
elif line.startswith('#'): | ||
section_data[self.SECTION_LASTLINE] = [i] | ||
else: # regular line | ||
|
@@ -143,7 +153,7 @@ def add_peer(self, key, leading_comment=None): | |
self.handle_leading_comment(leading_comment) # add leading comment if needed | ||
# Append peer with key attribute | ||
self.lines.append('[Peer]') | ||
self.lines.append(f'{self.keyattr} = {key}') | ||
self.lines.append('{0} = {1}'.format(self.keyattr, key)) | ||
# Invalidate data cache | ||
self.invalidate_data() | ||
|
||
|
@@ -181,7 +191,6 @@ 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 "#"') | ||
|
@@ -195,7 +204,7 @@ def add_attr(self, key, attr, value, leading_comment=None, append_as_line=False) | |
if (line_found is None) or append_as_line: | ||
line_found = section_lastline if (line_found is None) else line_found | ||
line_found += 1 | ||
self.lines.insert(line_found, f'{attr} = {value}') | ||
self.lines.insert(line_found, '{0} = {1}'.format(attr, value)) | ||
else: | ||
line_attr, line_value, line_comment = self.parse_line(self.lines[line_found]) | ||
line_value.append(value) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters