Skip to content

Commit

Permalink
fix: dev: replicate cleanup commits for six and py2, add flake8 cfg
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Arnold <[email protected]>
  • Loading branch information
sarnold committed Apr 6, 2024
1 parent 4b37e8c commit 18bc5a5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
9 changes: 4 additions & 5 deletions src/bmaptool/BmapCopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@
import datetime
from typing import List, Optional

from six import reraise
from six.moves import queue as Queue
from six.moves import _thread as thread
import queue
import _thread as thread
from defusedxml import DefusedXmlException
from defusedxml.ElementTree import parse

Expand Down Expand Up @@ -687,7 +686,7 @@ def copy(self, sync=True, verify=True):

# Create the queue for block batches and start the reader thread, which
# will read the image in batches and put the results to '_batch_queue'.
self._batch_queue = Queue.Queue(self._batch_queue_len)
self._batch_queue = queue.Queue(self._batch_queue_len)
thread.start_new_thread(self._get_data, (verify,))

blocks_written = 0
Expand Down Expand Up @@ -717,7 +716,7 @@ def copy(self, sync=True, verify=True):
# The reader thread encountered an error and passed us the
# exception.
exc_info = batch[1]
reraise(exc_info[0], exc_info[1], exc_info[2])
raise exc_info[1]

(start, end, buf) = batch[1:4]

Expand Down
39 changes: 15 additions & 24 deletions src/bmaptool/TransRead.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
import threading
import subprocess
import netrc
from six.moves.urllib import parse as urlparse
from bmaptool import BmapHelpers
import http.client
import urllib.error
import urllib.parse
import urllib.request

from . import BmapHelpers

_log = logging.getLogger(__name__) # pylint: disable=C0103

Expand Down Expand Up @@ -578,13 +582,7 @@ def _print_warning(timeout):
"proxy configured correctly? Keep trying ..." % timeout
)

import socket

from six.moves import http_client as httplib
from six.moves.urllib import request as urllib
from six.moves.urllib.error import URLError

parsed_url = urlparse.urlparse(url)
parsed_url = urllib.parse.urlparse(url)

if parsed_url.scheme == "ssh":
# Unfortunately, urllib2 does not handle "ssh://" URLs
Expand Down Expand Up @@ -615,15 +613,15 @@ def _print_warning(timeout):
new_url[1] = "%s:%s" % (parsed_url.hostname, parsed_url.port)
else:
new_url[1] = parsed_url.hostname
url = urlparse.urlunparse(new_url)
url = urllib.parse.urlunparse(new_url)

# Build an URL opener which will do the authentication
password_manager = urllib.HTTPPasswordMgrWithDefaultRealm()
password_manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, url, username, password)
auth_handler = urllib.HTTPBasicAuthHandler(password_manager)
opener = urllib.build_opener(auth_handler)
auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
opener = urllib.request.build_opener(auth_handler)
else:
opener = urllib.build_opener()
opener = urllib.request.build_opener()

opener.addheaders = [("User-Agent", "Mozilla/5.0")]
urllib.install_opener(opener)
Expand All @@ -639,21 +637,14 @@ def _print_warning(timeout):
for timeout in (10, None):
try:
f_obj = opener.open(url, timeout=timeout)
# Handling the timeout case in Python 2.7
except socket.timeout as err:
except urllib.error.URLError as err:
if timeout is not None:
_print_warning(timeout)
else:
raise Error("cannot open URL '%s': %s" % (url, err))
except URLError as err:
# Handling the timeout case in Python 2.6
if timeout is not None and isinstance(err.reason, socket.timeout):
_print_warning(timeout)
else:
raise Error("cannot open URL '%s': %s" % (url, err))
except (IOError, ValueError, httplib.InvalidURL) as err:
except (IOError, ValueError, http.client.InvalidURL) as err:
raise Error("cannot open URL '%s': %s" % (url, err))
except httplib.BadStatusLine:
except http.client.BadStatusLine:
raise Error(
"cannot open URL '%s': server responds with an "
"HTTP status code that we don't understand" % url
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import tempfile
import filecmp
import subprocess
from six.moves import zip_longest
from itertools import zip_longest
from tests import helpers
from bmaptool import BmapHelpers, BmapCreate, Filemap

Expand Down
2 changes: 1 addition & 1 deletion tests/test_filemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import random
import itertools
import tests.helpers
from six.moves import zip_longest
from itertools import zip_longest
from bmaptool import Filemap

import pytest
Expand Down
11 changes: 11 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,14 @@ deps =

commands =
python -m isort src/bmaptool/

[flake8]
extend-ignore = E203
max-line-length = 88
exclude =
.git,
__pycache__,
debian,
build,
dist
max-complexity = 20

0 comments on commit 18bc5a5

Please sign in to comment.