Skip to content

Commit

Permalink
Drop Python 2 compatibility code
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed Aug 15, 2020
1 parent 633e242 commit f331596
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 189 deletions.
5 changes: 1 addition & 4 deletions account/account/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ def set_servers_cookie_info(cookie, permanent):
if permanent:
delta = datetime.timedelta(days=365)
expires = datetime.datetime.now() + delta
try:
age = int(delta.total_seconds())
except AttributeError: # python 2.6
age = int(delta.days * 24 * 60 * 60 + delta.seconds)
age = int(delta.total_seconds())
else:
age = expires = None
user = cookie['user_name']
Expand Down
12 changes: 2 additions & 10 deletions python/saliweb/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,14 @@ def __init__(self, msg, job, template):
self.template = template


def _timediff_in_seconds(timediff):
"""Convert a datetime.timedelta to total number of seconds"""
try:
return int(timediff.total_seconds())
except AttributeError: # python 2.6
return timediff.days * 24*60*60 + timediff.seconds


def _format_timediff(timediff):
def _format_unit(df, unit):
return '%d %s%s' % (df, unit, '' if unit == 1 else 's')

if not timediff:
return
timediff = timediff - datetime.datetime.utcnow()
diff_sec = _timediff_in_seconds(timediff)
diff_sec = int(timediff.total_seconds())
if diff_sec < 0:
return
if diff_sec < 120:
Expand Down Expand Up @@ -116,7 +108,7 @@ def get_refresh_time(self, minseconds):
"""Get a suitable time, in seconds, to wait to refresh the
'job is still running' page. It will be at least `minseconds`."""
timediff = datetime.datetime.utcnow() - self.submit_time
return max(_timediff_in_seconds(timediff), minseconds)
return max(int(timediff.total_seconds()), minseconds)


class CompletedJob(object):
Expand Down
83 changes: 0 additions & 83 deletions python/saliweb/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,6 @@
import saliweb.backend


def _add_unittest_methods():
"""Add more modern unittest methods to Python 2.6, 3,0, or 3.1"""
import re
def assertIn(self, member, container, msg=None):
return self.assertTrue(member in container,
msg or '%s not found in %s' % (member, container))
def assertNotIn(self, member, container, msg=None):
return self.assertTrue(member not in container,
msg or '%s unexpectedly found in %s'
% (member, container))
def assertIsInstance(self, obj, cls, msg=None):
return self.assertTrue(isinstance(obj, cls),
msg or '%s is not an instance of %s' % (obj, cls))
def assertLess(self, a, b, msg=None):
return self.assertTrue(a < b, msg or '%s not less than %s' % (a, b))
def assertGreater(self, a, b, msg=None):
return self.assertTrue(a > b, msg or '%s not greater than %s' % (a, b))
def assertLessEqual(self, a, b, msg=None):
return self.assertTrue(a <= b,
msg or '%s not less than or equal to %s' % (a, b))
def assertGreaterEqual(self, a, b, msg=None):
return self.assertTrue(a >= b,
msg or '%s not greater than or equal to %s' % (a, b))
def assertIsNone(self, obj, msg=None):
return self.assertTrue(obj is None, msg or '%s is not None' % obj)
def assertIsNotNone(self, obj, msg=None):
return self.assertTrue(obj is not None, msg or 'unexpectedly None')
def assertRegex(self, text, expected_regexp, msg=None):
if isinstance(expected_regexp, basestring):
expected_regexp = re.compile(expected_regexp)
if not expected_regexp.search(text):
msg = msg or "Regexp didn't match"
msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text)
raise self.failureException(msg)
def assertNotRegex(self, text, unexpected_regexp, msg=None):
if isinstance(expected_regexp, basestring):
unexpected_regexp = re.compile(unexpected_regexp)
match = unexpected_regexp.search(text)
if match:
msg = msg or "Regexp matched"
msg = '%s: %r matches %r in %r' % (msg,
text[match.start():match.end()],
unexpected_regexp.pattern,
text)
raise self.failureException(msg)
def assertAlmostEqual(self, first, second, places=None, msg=None,
delta=None):
if delta is not None and places is not None:
raise TypeError("specify delta or places not both")
if delta is not None:
return self.assertTrue(abs(first - second) <= delta,
msg or '%s != %s within %s delta' % (str(first), str(second),
str(delta)))
else:
return self.assertTrue(round(abs(first - second), places) == 0,
msg or '%s != %s within %s places' % (str(first), str(second),
str(delta)))
return self.assertTrue(obj is not None, msg or 'unexpectedly None')
unittest.TestCase.assertIn = assertIn
unittest.TestCase.assertNotIn = assertNotIn
unittest.TestCase.assertIsInstance = assertIsInstance
unittest.TestCase.assertLess = assertLess
unittest.TestCase.assertGreater = assertGreater
unittest.TestCase.assertLessEqual = assertLessEqual
unittest.TestCase.assertGreaterEqual = assertGreaterEqual
unittest.TestCase.assertIsNone = assertIsNone
unittest.TestCase.assertIsNotNone = assertIsNotNone
unittest.TestCase.assertRegex = assertRegex
unittest.TestCase.assertNotRegex = assertNotRegex
unittest.TestCase.assertAlmostEqual = assertAlmostEqual


# If we're using Python 2.6, 3.0, or 3.1, add in more modern unittest
# convenience methods
if not hasattr(unittest.TestCase, 'assertIsInstance'):
_add_unittest_methods()
# Provide assert(Not)Regex for Python 2 users (assertRegexpMatches is
# deprecated in Python 3)
if not hasattr(unittest.TestCase, 'assertRegex'):
unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches
unittest.TestCase.assertNotRegex = unittest.TestCase.assertNotRegexpMatches


class RunInDir(object):
"""Change to the given directory, and change back when this object
goes out of scope."""
Expand Down
91 changes: 0 additions & 91 deletions test/build/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,97 +7,6 @@
import unittest


# If we're using Python 2.6, 3.0, or 3.1, add in more modern unittest
# convenience methods
if not hasattr(unittest.TestCase, 'assertIsInstance'):
import re
def assertIn(self, member, container, msg=None):
return self.assertTrue(member in container,
msg or '%s not found in %s' % (member, container))
def assertNotIn(self, member, container, msg=None):
return self.assertTrue(member not in container,
msg or '%s unexpectedly found in %s'
% (member, container))
def assertIs(self, a, b, msg=None):
return self.assertTrue(a is b, msg or '%s is not %s' % (a, b))
def assertIsNot(self, a, b, msg=None):
return self.assertTrue(a is not b, msg or '%s is %s' % (a, b))
def assertIsInstance(self, obj, cls, msg=None):
return self.assertTrue(isinstance(obj, cls),
msg or '%s is not an instance of %s' % (obj, cls))
def assertLess(self, a, b, msg=None):
return self.assertTrue(a < b, msg or '%s not less than %s' % (a, b))
def assertGreater(self, a, b, msg=None):
return self.assertTrue(a > b, msg or '%s not greater than %s' % (a, b))
def assertLessEqual(self, a, b, msg=None):
return self.assertTrue(a <= b,
msg or '%s not less than or equal to %s' % (a, b))
def assertGreaterEqual(self, a, b, msg=None):
return self.assertTrue(a >= b,
msg or '%s not greater than or equal to %s' % (a, b))
def assertIsNone(self, obj, msg=None):
return self.assertTrue(obj is None, msg or '%s is not None' % obj)
def assertIsNotNone(self, obj, msg=None):
return self.assertTrue(obj is not None, msg or 'unexpectedly None')
def assertRegex(self, text, expected_regexp, msg=None):
if isinstance(expected_regexp, basestring):
expected_regexp = re.compile(expected_regexp)
if not expected_regexp.search(text):
msg = msg or "Regexp didn't match"
msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text)
raise self.failureException(msg)
def assertNotRegex(self, text, unexpected_regexp, msg=None):
if isinstance(expected_regexp, basestring):
unexpected_regexp = re.compile(unexpected_regexp)
match = unexpected_regexp.search(text)
if match:
msg = msg or "Regexp matched"
msg = '%s: %r matches %r in %r' % (msg,
text[match.start():match.end()],
unexpected_regexp.pattern,
text)
raise self.failureException(msg)
def assertAlmostEqual(self, first, second, places=None, msg=None,
delta=None):
if first == second:
return
if delta is not None and places is not None:
raise TypeError("specify delta or places not both")
diff = abs(first - second)
if delta is not None:
if diff <= delta:
return
standard_msg = ("%s != %s within %s delta (%s difference)"
% (first, second, delta, diff))
else:
if places is None:
places = 7
if round(diff, places) == 0:
return
standard_msg = ("%s != %s within %r places (%s difference)"
% (first, second, places, diff))
raise self.failureException(msg or standard_msg)
unittest.TestCase.assertIn = assertIn
unittest.TestCase.assertNotIn = assertNotIn
unittest.TestCase.assertIs = assertIs
unittest.TestCase.assertIsNot = assertIsNot
unittest.TestCase.assertIsInstance = assertIsInstance
unittest.TestCase.assertLess = assertLess
unittest.TestCase.assertGreater = assertGreater
unittest.TestCase.assertLessEqual = assertLessEqual
unittest.TestCase.assertGreaterEqual = assertGreaterEqual
unittest.TestCase.assertIsNone = assertIsNone
unittest.TestCase.assertIsNotNone = assertIsNotNone
unittest.TestCase.assertAlmostEqual = assertAlmostEqual
unittest.TestCase.assertRegex = assertRegex
unittest.TestCase.assertNotRegex = assertNotRegex
# Provide assert(Not)Regex for Python 2 users (assertRegexpMatches is
# deprecated in Python 3)
if not hasattr(unittest.TestCase, 'assertRegex'):
unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches
unittest.TestCase.assertNotRegex = unittest.TestCase.assertNotRegexpMatches


def run_catch_warnings(method, *args, **keys):
"""Run a method and return both its own return value and a list of any
warnings raised."""
Expand Down
1 change: 0 additions & 1 deletion test/pyfrontend/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import contextlib
import tempfile
import shutil
import saliweb.test # for python 2.6 support


@contextlib.contextmanager
Expand Down

0 comments on commit f331596

Please sign in to comment.