diff options
Diffstat (limited to 'python/subunit')
-rw-r--r-- | python/subunit/__init__.py | 7 | ||||
-rw-r--r-- | python/subunit/_output.py | 2 | ||||
-rw-r--r-- | python/subunit/iso8601.py | 128 | ||||
-rw-r--r-- | python/subunit/test_results.py | 3 | ||||
-rw-r--r-- | python/subunit/tests/test_output_filter.py | 3 | ||||
-rw-r--r-- | python/subunit/tests/test_subunit_filter.py | 5 | ||||
-rw-r--r-- | python/subunit/tests/test_test_protocol.py | 7 | ||||
-rw-r--r-- | python/subunit/tests/test_test_protocol2.py | 6 | ||||
-rw-r--r-- | python/subunit/tests/test_test_results.py | 6 | ||||
-rw-r--r-- | python/subunit/v2.py | 4 |
10 files changed, 24 insertions, 147 deletions
diff --git a/python/subunit/__init__.py b/python/subunit/__init__.py index 5e318c1..4203256 100644 --- a/python/subunit/__init__.py +++ b/python/subunit/__init__.py @@ -123,6 +123,7 @@ import unittest from io import BytesIO, StringIO from io import UnsupportedOperation as _UnsupportedOperation +import iso8601 from testtools import ExtendedToOriginalDecorator, content, content_type from testtools.compat import _b, _u from testtools.content import TracebackContent @@ -135,7 +136,7 @@ except ImportError: "_StringException, check your version.") from testtools import CopyStreamResult, testresult -from subunit import chunked, details, iso8601, test_results +from subunit import chunked, details, test_results from subunit.v2 import ByteStreamToStreamResult, StreamResultToBytes # same format as sys.version_info: "A tuple containing the five components of @@ -551,7 +552,7 @@ class TestProtocolServer(object): def _handleTime(self, offset, line): # Accept it, but do not do anything with it yet. try: - event_time = iso8601.parse_date(line[offset:-1]) + event_time = iso8601.parse_date(line[offset:-1].decode()) except TypeError: raise TypeError(_u("Failed to parse %r, got %r") % (line, sys.exec_info[1])) @@ -794,7 +795,7 @@ class TestProtocolClient(testresult.TestResult): ":param datetime: A datetime.datetime object. """ - time = a_datetime.astimezone(iso8601.Utc()) + time = a_datetime.astimezone(iso8601.UTC) self._stream.write(_b("time: %04d-%02d-%02d %02d:%02d:%02d.%06dZ\n" % ( time.year, time.month, time.day, time.hour, time.minute, time.second, time.microsecond))) diff --git a/python/subunit/_output.py b/python/subunit/_output.py index 8141dc9..48a6f7e 100644 --- a/python/subunit/_output.py +++ b/python/subunit/_output.py @@ -19,7 +19,7 @@ from functools import partial from optparse import OptionGroup, OptionParser, OptionValueError from subunit import make_stream_binary -from subunit.iso8601 import UTC +from iso8601 import UTC from subunit.v2 import StreamResultToBytes _FINAL_ACTIONS = frozenset([ diff --git a/python/subunit/iso8601.py b/python/subunit/iso8601.py deleted file mode 100644 index fa42270..0000000 --- a/python/subunit/iso8601.py +++ /dev/null @@ -1,128 +0,0 @@ -# Copyright (c) 2007 Michael Twomey -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -"""ISO 8601 date time string parsing - -Basic usage: ->>> import iso8601 ->>> iso8601.parse_date("2007-01-25T12:00:00Z") -datetime.datetime(2007, 1, 25, 12, 0, tzinfo=<iso8601.iso8601.Utc ...>) ->>> - -""" - -import re -from datetime import datetime, timedelta, tzinfo - -__all__ = ["parse_date", "ParseError"] - -# Adapted from http://delete.me.uk/2005/03/iso8601.html -ISO8601_REGEX_PATTERN = (r"(?P<year>[0-9]{4})(-(?P<month>[0-9]{1,2})(-(?P<day>[0-9]{1,2})" - r"((?P<separator>.)(?P<hour>[0-9]{2}):(?P<minute>[0-9]{2})(:(?P<second>[0-9]{2})(\.(?P<fraction>[0-9]+))?)?" - r"(?P<timezone>Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?" -) -TIMEZONE_REGEX_PATTERN = "(?P<prefix>[+-])(?P<hours>[0-9]{2}).(?P<minutes>[0-9]{2})" -ISO8601_REGEX = re.compile(ISO8601_REGEX_PATTERN.encode('utf8')) -TIMEZONE_REGEX = re.compile(TIMEZONE_REGEX_PATTERN.encode('utf8')) - -zulu = "Z".encode('latin-1') -minus = "-".encode('latin-1') - -class ParseError(Exception): - """Raised when there is a problem parsing a date string""" - -# Yoinked from python docs -ZERO = timedelta(0) -class Utc(tzinfo): - """UTC - - """ - def utcoffset(self, dt): - return ZERO - - def tzname(self, dt): - return "UTC" - - def dst(self, dt): - return ZERO -UTC = Utc() - -class FixedOffset(tzinfo): - """Fixed offset in hours and minutes from UTC - - """ - def __init__(self, offset_hours, offset_minutes, name): - self.__offset = timedelta(hours=offset_hours, minutes=offset_minutes) - self.__name = name - - def utcoffset(self, dt): - return self.__offset - - def tzname(self, dt): - return self.__name - - def dst(self, dt): - return ZERO - - def __repr__(self): - return "<FixedOffset %r>" % self.__name - -def parse_timezone(tzstring, default_timezone=UTC): - """Parses ISO 8601 time zone specs into tzinfo offsets - - """ - if tzstring == zulu: - return default_timezone - # This isn't strictly correct, but it's common to encounter dates without - # timezones so I'll assume the default (which defaults to UTC). - # Addresses issue 4. - if tzstring is None: - return default_timezone - m = TIMEZONE_REGEX.match(tzstring) - prefix, hours, minutes = m.groups() - hours, minutes = int(hours), int(minutes) - if prefix == minus: - hours = -hours - minutes = -minutes - return FixedOffset(hours, minutes, tzstring) - -def parse_date(datestring, default_timezone=UTC): - """Parses ISO 8601 dates into datetime objects - - The timezone is parsed from the date string. However it is quite common to - have dates without a timezone (not strictly correct). In this case the - default timezone specified in default_timezone is used. This is UTC by - default. - """ - if not isinstance(datestring, bytes): - raise ParseError("Expecting bytes %r" % datestring) - m = ISO8601_REGEX.match(datestring) - if not m: - raise ParseError("Unable to parse date string %r" % datestring) - groups = m.groupdict() - tz = parse_timezone(groups["timezone"], default_timezone=default_timezone) - if groups["fraction"] is None: - groups["fraction"] = 0 - else: - groups["fraction"] = int(float("0.%s" % groups["fraction"].decode()) * 1e6) - return datetime(int(groups["year"]), int(groups["month"]), int(groups["day"]), - int(groups["hour"]), int(groups["minute"]), int(groups["second"]), - int(groups["fraction"]), tz) diff --git a/python/subunit/test_results.py b/python/subunit/test_results.py index 71a983c..be081c1 100644 --- a/python/subunit/test_results.py +++ b/python/subunit/test_results.py @@ -24,6 +24,7 @@ from testtools import StreamResult from testtools.content import TracebackContent, text_content from testtools.testcase import PlaceHolder +import iso8601 import subunit from subunit import iso8601 @@ -193,7 +194,7 @@ class AutoTimingTestResultDecorator(HookedTestResultDecorator): time = self._time if time is not None: return - time = datetime.datetime.utcnow().replace(tzinfo=iso8601.Utc()) + time = datetime.datetime.utcnow().replace(tzinfo=iso8601.UTC) self.decorated.time(time) def progress(self, offset, whence): diff --git a/python/subunit/tests/test_output_filter.py b/python/subunit/tests/test_output_filter.py index 0501f79..555d70a 100644 --- a/python/subunit/tests/test_output_filter.py +++ b/python/subunit/tests/test_output_filter.py @@ -22,6 +22,8 @@ from functools import partial from io import BytesIO, StringIO, TextIOWrapper from tempfile import NamedTemporaryFile +from iso8601 import UTC + from testtools import TestCase from testtools.compat import _u from testtools.matchers import (Equals, Matcher, MatchesAny, MatchesListwise, @@ -31,7 +33,6 @@ from testtools.testresult.doubles import StreamResult import subunit._output as _o from subunit._output import (_ALL_ACTIONS, _FINAL_ACTIONS, generate_stream_results, parse_arguments) -from subunit.iso8601 import UTC from subunit.v2 import ByteStreamToStreamResult, StreamResultToBytes diff --git a/python/subunit/tests/test_subunit_filter.py b/python/subunit/tests/test_subunit_filter.py index a3baa5c..508a5df 100644 --- a/python/subunit/tests/test_subunit_filter.py +++ b/python/subunit/tests/test_subunit_filter.py @@ -27,9 +27,10 @@ from testtools import TestCase from testtools.compat import _b from testtools.testresult.doubles import ExtendedTestResult, StreamResult +import iso8601 import subunit -from subunit import ByteStreamToStreamResult, StreamResultToBytes, iso8601 -from subunit.test_results import TestResultFilter, make_tag_filter +from subunit.test_results import make_tag_filter, TestResultFilter +from subunit import ByteStreamToStreamResult, StreamResultToBytes class TestTestResultFilter(TestCase): diff --git a/python/subunit/tests/test_test_protocol.py b/python/subunit/tests/test_test_protocol.py index a40591e..7ad28d2 100644 --- a/python/subunit/tests/test_test_protocol.py +++ b/python/subunit/tests/test_test_protocol.py @@ -39,8 +39,9 @@ except ImportError: from testtools.matchers import Contains, Equals, MatchesAny +import iso8601 + import subunit -import subunit.iso8601 as iso8601 from subunit.tests import (_remote_exception_repr, _remote_exception_repr_chunked, _remote_exception_str, @@ -981,7 +982,7 @@ class TestTestProtocolServerStreamTime(unittest.TestCase): self.assertEqual(_b(""), self.stream.getvalue()) self.assertEqual([ ('time', datetime.datetime(2001, 12, 12, 12, 59, 59, 0, - iso8601.Utc())) + iso8601.UTC)) ], self.result._events) @@ -1386,7 +1387,7 @@ class TestTestProtocolClient(TestCase): def test_time(self): # Calling time() outputs a time signal immediately. self.protocol.time( - datetime.datetime(2009,10,11,12,13,14,15, iso8601.Utc())) + datetime.datetime(2009,10,11,12,13,14,15, iso8601.UTC)) self.assertEqual( _b("time: 2009-10-11 12:13:14.000015Z\n"), self.io.getvalue()) diff --git a/python/subunit/tests/test_test_protocol2.py b/python/subunit/tests/test_test_protocol2.py index 4f0eb9f..ef88092 100644 --- a/python/subunit/tests/test_test_protocol2.py +++ b/python/subunit/tests/test_test_protocol2.py @@ -32,7 +32,7 @@ from testtools.testresult.doubles import StreamResult from testtools.tests.test_testresult import TestStreamResultContract import subunit -import subunit.iso8601 as iso8601 +import iso8601 CONSTANT_ENUM = b'\xb3)\x01\x0c\x03foo\x08U_\x1b' CONSTANT_INPROGRESS = b'\xb3)\x02\x0c\x03foo\x8e\xc1-\xb5' @@ -218,7 +218,7 @@ class TestStreamResultToBytes(TestCase): def test_timestamp(self): timestamp = datetime.datetime(2001, 12, 12, 12, 59, 59, 45, - iso8601.Utc()) + iso8601.UTC) result, output = self._make_result() result.status(test_id="bar", test_status='success', timestamp=timestamp) self.assertEqual(CONSTANT_TIMESTAMP, output.getvalue()) @@ -382,7 +382,7 @@ class TestByteStreamToStreamResult(TestCase): def test_timestamp(self): timestamp = datetime.datetime(2001, 12, 12, 12, 59, 59, 45, - iso8601.Utc()) + iso8601.UTC) self.check_event(CONSTANT_TIMESTAMP, 'success', test_id='bar', timestamp=timestamp) diff --git a/python/subunit/tests/test_test_results.py b/python/subunit/tests/test_test_results.py index 765454a..fc14133 100644 --- a/python/subunit/tests/test_test_results.py +++ b/python/subunit/tests/test_test_results.py @@ -26,7 +26,7 @@ from testtools.content import TracebackContent, text_content from testtools.testresult.doubles import ExtendedTestResult import subunit -import subunit.iso8601 as iso8601 +import iso8601 import subunit.test_results @@ -174,7 +174,7 @@ class TestAutoTimingTestResultDecorator(unittest.TestCase): def test_calling_time_inhibits_automatic_time(self): # Calling time() outputs a time signal immediately and prevents # automatically adding one when other methods are called. - time = datetime.datetime(2009,10,11,12,13,14,15, iso8601.Utc()) + time = datetime.datetime(2009,10,11,12,13,14,15, iso8601.UTC) self.result.time(time) self.result.startTest(self) self.result.stopTest(self) @@ -182,7 +182,7 @@ class TestAutoTimingTestResultDecorator(unittest.TestCase): self.assertEqual(time, self.decorated._calls[0]) def test_calling_time_None_enables_automatic_time(self): - time = datetime.datetime(2009,10,11,12,13,14,15, iso8601.Utc()) + time = datetime.datetime(2009,10,11,12,13,14,15, iso8601.UTC) self.result.time(time) self.assertEqual(1, len(self.decorated._calls)) self.assertEqual(time, self.decorated._calls[0]) diff --git a/python/subunit/v2.py b/python/subunit/v2.py index d344aff..9619f3c 100644 --- a/python/subunit/v2.py +++ b/python/subunit/v2.py @@ -23,7 +23,7 @@ import sys import zlib import subunit -import subunit.iso8601 as iso8601 +import iso8601 utf_8_decode = codecs.utf_8_decode @@ -46,7 +46,7 @@ FLAG_TAGS = 0x0080 FLAG_MIME_TYPE = 0x0020 FLAG_EOF = 0x0010 FLAG_FILE_CONTENT = 0x0040 -EPOCH = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=iso8601.Utc()) +EPOCH = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=iso8601.UTC) NUL_ELEMENT = b'\0'[0] # Contains True for types for which 'nul in thing' falsely returns false. _nul_test_broken = {} |