Skip to content

Commit

Permalink
Merge pull request #2415 from johannaengland/python-2
Browse files Browse the repository at this point in the history
Remove python2 code
  • Loading branch information
johannaengland authored May 18, 2022
2 parents e591961 + 1821eda commit d14c74a
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 41 deletions.
24 changes: 0 additions & 24 deletions python/nav/mibs/entity_mib.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ def get_chassis_of(self, entity):
def clean(self):
"""Cleans the table data"""

if sys.version_info[0] == 2: # Python 2 only
self._clean_unicode()
self._parse_mfg_date()
self._strip_whitespace()
self._fix_broken_chassis_relative_positions()
Expand Down Expand Up @@ -351,28 +349,6 @@ def _get_non_chassis_duplicates(self):
dupes = dict((key, value) for key, value in dupes.items() if len(value) > 1)
return dupes

def _clean_unicode(self, encoding="utf-8"):
"""Decodes every string attribute of every entity as UTF-8.
Strings that cannot be successfully decoded as UTF-8 will instead be
encoded as a Python string repr (and debug logged).
"""
for entity in self.values():
for key, value in entity.items():
if isinstance(value, bytes):
try:
new_value = value.decode(encoding)
except UnicodeDecodeError:
new_value = str(repr(value))
_logger.debug(
"cannot decode %s value as %s, using python "
"string repr instead: %s",
key,
encoding,
new_value,
)
entity[key] = new_value


EIGHT_OCTET_DATEANDTIME = struct.Struct("HBBBBBB")
ELEVEN_OCTET_DATEANDTIME = struct.Struct("HBBBBBBcBB")
Expand Down
12 changes: 2 additions & 10 deletions python/nav/natsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,9 @@
foo = os.listdir('/path/to/bar')
foo.sort(key=natsort.split)
"""
import sys
import re
from functools import total_ordering

if sys.version_info.major == 2:
_string_types = (basestring,) # Python 2
else:
_string_types = (str,) # Python 3

_split_pattern = re.compile(r'(\d+|\D+)')


Expand All @@ -51,7 +45,7 @@ class ComparableThing(object):
"""

def __init__(self, value):
if isinstance(value, _string_types) and value.isdigit():
if isinstance(value, str) and value.isdigit():
self.value = int(value)
else:
self.value = value
Expand All @@ -67,9 +61,7 @@ def __lt__(self, other):

if isinstance(self.value, int) and not isinstance(other.value, int):
return True
if isinstance(self.value, _string_types) and not isinstance(
other.value, _string_types
):
if isinstance(self.value, str) and not isinstance(other.value, str):
return False

return self.value < other.value
Expand Down
3 changes: 0 additions & 3 deletions python/nav/statemon/RunQueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ def _start_worker_if_needed(self):
# This is quite dirty, but I really need to know how many
# threads are waiting for checkers.
waiters = getattr(self.await_work, "_waiters", None)
# Next two lines exist only for compat with Python 2 (Python < 3)
if waiters is None:
waiters = getattr(self.await_work, "_Condition__waiters")
num_waiters = len(waiters)
_logger.debug(
"Number of workers: %i Waiting workers: %i", len(self.workers), num_waiters
Expand Down
2 changes: 1 addition & 1 deletion python/nav/statemon/checker/FtpChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def execute(self):

# This cannot happen on Linux (debian)
# A bug has been reported on FreeBSD so we CYA
if sys.version_info >= (3,) and isinstance(welcome, bytes):
if isinstance(welcome, bytes):
welcome = welcome.decode('utf-8', 'replace')

# Get server version from the banner.
Expand Down
4 changes: 1 addition & 3 deletions python/nav/web/info/images/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def create_hash(something, salt=False):
data = str(something) + str(time.time()) if salt else something
try:
hash_object = hashlib.sha1(data)
except (UnicodeEncodeError, TypeError):
# UnicodeEncodeError: Only on Python<3
# TypeError: Only on Python>=3
except TypeError:
hash_object = hashlib.sha1(data.encode('utf-8'))

return hash_object.hexdigest()
Expand Down

0 comments on commit d14c74a

Please sign in to comment.