Skip to content

Commit

Permalink
Extend Python version compatibility (now Python2.7+)
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri committed Nov 15, 2020
1 parent cd018e1 commit f5a41bf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 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.2]

### Added

- Support for older Python versions; now 2.7+ is supported; thanks to Marc (https://github.com/marcfiu) for this

## [0.2.1] - 2020-11-11

### Fixed
Expand Down
11 changes: 7 additions & 4 deletions 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.1',
'version': '0.2.2',
'author': 'Dirk Henrici',
'author_email': '[email protected]',
'description': 'parsing and writing WireGuard configuration files',
Expand All @@ -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',
Expand Down
19 changes: 14 additions & 5 deletions src/wgconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand All @@ -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'
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 "#"')
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/wgconfig/wgexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

"""Simple wrapper around WireGuard commands"""

# The following imports are for Python2 support only
from __future__ import absolute_import
from __future__ import print_function

import logging
import shlex
import subprocess
Expand Down
8 changes: 6 additions & 2 deletions test/test_1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import contextlib
# The following imports are for Python2 support only
from __future__ import absolute_import
from __future__ import print_function

import filecmp
import os
import pprint
Expand All @@ -19,7 +22,8 @@ def setup_testconfig1(scope='module'):
wc = wgconfig.WGConfig(file=TESTFILE1)
wc.read_file()
yield wc
with contextlib.suppress(FileNotFoundError):
#with contextlib.suppress(FileNotFoundError): # not used due to availability in newer Python versions only
if os.path.exists(TESTFILE1_SAVED):
os.unlink(TESTFILE1_SAVED)

def output_data(wc):
Expand Down

0 comments on commit f5a41bf

Please sign in to comment.