Skip to content

Commit

Permalink
Make blocklister python 2.7 compatible again
Browse files Browse the repository at this point in the history
  • Loading branch information
flazzarini committed Jan 4, 2017
1 parent 4780457 commit aacbe9c
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 147 deletions.
3 changes: 2 additions & 1 deletion blocklister/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import logging
from future.standard_library import install_aliases
install_aliases() # noqa

from configparser import ConfigParser, NoSectionError, NoOptionError
from os.path import exists

install_aliases()
LOG = logging.getLogger(__name__)
DEFAULT_PATHS = ['/etc/blocklister/', '~/.', '']

Expand Down
14 changes: 11 additions & 3 deletions blocklister/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import re

from os.path import join
from datetime import timedelta
from ipaddress import (
Expand All @@ -8,6 +9,13 @@
summarize_address_range
)

# Remove the following if python 2.7 support is omitted
try:
UNICODE_EXISTS = bool(type(unicode))
except NameError:
def unicode(s):
return str(s)

from blocklister.fetcher import Fetcher


Expand Down Expand Up @@ -73,15 +81,15 @@ def get_ips(self, cidr_notation=False):

if len(res.groups()) == 1:
if cidr_notation:
entry = IPv4Network(res.groups()[0])
entry = IPv4Network(unicode(res.groups()[0]))
results.append(entry)
else:
results.append(res.groups()[0])

elif len(res.groups()) > 1:
if cidr_notation:
start = IPv4Address(res.groups()[0])
end = IPv4Address(res.groups()[1])
start = IPv4Address(unicode(res.groups()[0]))
end = IPv4Address(unicode(res.groups()[1]))
networks = list(summarize_address_range(start, end))
results += networks
else:
Expand Down
23 changes: 19 additions & 4 deletions blocklister/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sys
import unittest
from unittest.mock import patch

if sys.version_info[0] == 3: # noqa
from unittest.mock import patch
else:
from mock import patch

from textwrap import dedent

from tempfile import NamedTemporaryFile
Expand Down Expand Up @@ -62,18 +68,27 @@ def test_get_default(self):
def test_get_list(self):
result = self.config.get_list('testing', 'list')
expected = ['item1', 'item2', 'item3']
self.assertCountEqual(result, expected)
if sys.version_info[0] == 3: # noqa
self.assertCountEqual(result, expected)
else:
self.assertItemsEqual(result, expected)

def test_get_list_single_item(self):
result = self.config.get_list('testing', 'list_single')
expected = ['item1']
self.assertCountEqual(result, expected)
if sys.version_info[0] == 3: # noqa
self.assertCountEqual(result, expected)
else:
self.assertItemsEqual(result, expected)

def test_get_list_default(self):
result = self.config.get_list(
'testing', 'listNotFound', default=['1', '2'])
expected = ['1', '2']
self.assertCountEqual(result, expected)
if sys.version_info[0] == 3: # noqa
self.assertCountEqual(result, expected)
else:
self.assertItemsEqual(result, expected)

def test_get_int(self):
result = self.config.get_int('testing', 'foo')
Expand Down
8 changes: 7 additions & 1 deletion blocklister/tests/test_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sys
import unittest
from unittest.mock import MagicMock, patch

if sys.version_info[0] == 3: # noqa
from unittest.mock import MagicMock, patch
else:
from mock import MagicMock, patch

from datetime import datetime, timedelta
from tempfile import NamedTemporaryFile
from io import BytesIO
Expand Down
8 changes: 7 additions & 1 deletion blocklister/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import sys
import unittest
from textwrap import dedent
from unittest.mock import MagicMock, patch, PropertyMock

if sys.version_info[0] == 3: # noqa
from unittest.mock import MagicMock, patch, PropertyMock
else:
from mock import MagicMock, patch, PropertyMock

from datetime import datetime
from blocklister.main import app
from blocklister.models import Blocklist
Expand Down
Loading

0 comments on commit aacbe9c

Please sign in to comment.