From 79b9eccb57ff145dfce3e1aa85c8a11cef616247 Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Fri, 20 May 2016 16:15:58 -0400 Subject: [PATCH 1/4] Add ooid module and tests from socorrolib Antenna will be self-contained and won't depend on anything from socorro or socorrolib. However, there are a few things we need to use from socorrolib to make everything work. As such, my plan is to copy code from socorrolib that's very very unlikely to change. This copies over the ooid code and its tests. It also updates both so they don't make our linter sad and also to take advantage of pytest magic. --- antenna/lib/__init__.py | 4 ++ antenna/lib/ooid.py | 112 ++++++++++++++++++++++++++++++++++++++ tests/test_ooid.py | 118 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 antenna/lib/__init__.py create mode 100644 antenna/lib/ooid.py create mode 100644 tests/test_ooid.py diff --git a/antenna/lib/__init__.py b/antenna/lib/__init__.py new file mode 100644 index 00000000..5501cd4b --- /dev/null +++ b/antenna/lib/__init__.py @@ -0,0 +1,4 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + diff --git a/antenna/lib/ooid.py b/antenna/lib/ooid.py new file mode 100644 index 00000000..124b0d5e --- /dev/null +++ b/antenna/lib/ooid.py @@ -0,0 +1,112 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# This is based on socorrolib's socorrolib/lib/ooid.py module. + +# OOID is "Our opaque ID" +import datetime as dt +import uuid as uu + +from antenna.lib.datetimeutil import utc_now, UTC + + +DEFAULT_DEPTH = 2 +OLD_HARD_DEPTH = 4 + + +def create_new_ooid(timestamp=None, depth=None): + """Create a new Ooid for a given time, to be stored at a given depth + + :arg timestamp: the year-month-day is encoded in the ooid. If none, use + current day. + + :arg depth: the expected storage depth is encoded in the ooid. If non, use + the DEFAULT_DEPTH returns a new opaque id string holding 24 random hex + digits and encoded date and depth info + + """ + if not timestamp: + timestamp = utc_now().date() + if not depth: + depth = DEFAULT_DEPTH + assert depth <= 4 and depth >= 1 + uuid = str(uu.uuid4()) + return "%s%d%02d%02d%02d" % ( + uuid[:-7], depth, timestamp.year % 100, timestamp.month, timestamp.day + ) + + +def uuid_to_ooid(uuid, timestamp=None, depth=None): + """Create an ooid from a 32-hex-digit string in regular uuid format. + + :arg uuid: must be uuid in expected format: + ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxx7777777`` + + :arg timestamp: the year-month-day is encoded in the ooid. If none, use + current day + + :arg depth: the expected storage depth is encoded in the ooid. If non, use + the DEFAULT_DEPTH returns a new opaque id string holding the first 24 + digits of the provided uuid and encoded date and depth info + + """ + if not timestamp: + timestamp = utc_now().date() + if not depth: + depth = DEFAULT_DEPTH + assert depth <= 4 and depth >= 1 + return "%s%d%02d%02d%02d" % ( + uuid[:-7], depth, timestamp.year % 100, timestamp.month, timestamp.day + ) + + +def date_and_depth_from_ooid(ooid): + """Extract the encoded date and expected storage depth from an ooid. + + :arg ooid: The ooid from which to extract the info + + :returns: ``(datetime(yyyy, mm, dd), depth)`` if the ooid is in expected + format else ``(None, None)`` + + """ + year = month = day = None + try: + day = int(ooid[-2:]) + except Exception: + return None, None + try: + month = int(ooid[-4:-2]) + except Exception: + return None, None + try: + year = 2000 + int(ooid[-6:-4]) + depth = int(ooid[-7]) + if not depth: + depth = OLD_HARD_DEPTH + return (dt.datetime(year, month, day, tzinfo=UTC), depth) + except Exception: + return None, None + return None, None + + +def depth_from_ooid(ooid): + """Extract the encoded expected storage depth from an ooid. + + :arg ooid: The ooid from which to extract the info + + :returns: expected depth if the ooid is in expected format else None + + """ + return date_and_depth_from_ooid(ooid)[1] + + +def date_from_ooid(ooid): + """Extract the encoded date from an ooid. + + :arg ooid: The ooid from which to extract the info + + :returns: encoded date if the ooid is in expected format else None + + """ + return date_and_depth_from_ooid(ooid)[0] diff --git a/tests/test_ooid.py b/tests/test_ooid.py new file mode 100644 index 00000000..4350f99c --- /dev/null +++ b/tests/test_ooid.py @@ -0,0 +1,118 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# This is based on socorrolib's socorrolib/unittest/lib/testOoid.py module +# with a heavy flake8/pytest pass. + +import uuid as uu +import datetime as dt +from unittest import TestCase + +from antenna.lib.datetimeutil import utc_now, UTC +import antenna.lib.ooid as oo + + +class Test_ooid(TestCase): + def setUp(self): + self.baseDate = dt.datetime(2008, 12, 25, tzinfo=UTC) + self.rawuuids = [] + self.yyyyoids = [] + self.dyyoids = [] + self.depths = [4, 4, 3, 3, 3, 2, 2, 2, 1, 1] + self.badooid0 = "%s%s" % (str(uu.uuid4())[:-8], 'ffeea1b2') + self.badooid1 = "%s%s" % (str(uu.uuid4())[:-8], 'f3eea1b2') + + for i in range(10): + self.rawuuids.append(str(uu.uuid4())) + assert len(self.depths) == len(self.rawuuids) + + for i in self.rawuuids: + self.yyyyoids.append("%s%4d%02d%02d" % ( + i[:-8], + self.baseDate.year, + self.baseDate.month, + self.baseDate.day + )) + + for i in range(len(self.rawuuids)): + self.dyyoids.append("%s%d%02d%02d%02d" % ( + self.rawuuids[i][:-7], + self.depths[i], + self.baseDate.year % 100, + self.baseDate.month, + self.baseDate.day + )) + + today = utc_now() + self.nowstamp = dt.datetime( + today.year, today.month, today.day, tzinfo=UTC + ) + self.xmas05 = dt.datetime(2005, 12, 25, tzinfo=UTC) + + def test_create_new_ooid(self): + ooid = oo.create_new_ooid() + assert oo.date_from_ooid(ooid) == self.nowstamp + assert oo.depth_from_ooid(ooid) == oo.DEFAULT_DEPTH + + ooid = oo.create_new_ooid(timestamp=self.xmas05) + assert oo.date_from_ooid(ooid) == self.xmas05 + assert oo.depth_from_ooid(ooid) == oo.DEFAULT_DEPTH + + for d in range(1, 5): + ooid0 = oo.create_new_ooid(depth=d) + ooid1 = oo.create_new_ooid(timestamp=self.xmas05, depth=d) + + ndepth0 = oo.depth_from_ooid(ooid0) + ndepth1 = oo.depth_from_ooid(ooid1) + + assert oo.date_from_ooid(ooid0) == self.nowstamp + assert oo.date_from_ooid(ooid1) == self.xmas05 + assert ndepth0 == ndepth1 + assert ndepth0 == d + + assert oo.depth_from_ooid(self.badooid0) is None + assert oo.depth_from_ooid(self.badooid1) is None + + def test_uuid_to_ooid(self): + for i in range(len(self.rawuuids)): + u = self.rawuuids[i] + + o0 = oo.uuid_to_ooid(u) + assert oo.date_and_depth_from_ooid(o0) == ( + self.nowstamp, oo.DEFAULT_DEPTH + ) + + o1 = oo.uuid_to_ooid(u, timestamp=self.baseDate) + assert oo.date_and_depth_from_ooid(o1) == ( + self.baseDate, oo.DEFAULT_DEPTH + ) + + o2 = oo.uuid_to_ooid(u, depth=self.depths[i]) + assert oo.date_and_depth_from_ooid(o2) == ( + self.nowstamp, self.depths[i] + ) + + o3 = oo.uuid_to_ooid( + u, depth=self.depths[i], timestamp=self.xmas05 + ) + assert oo.date_and_depth_from_ooid(o3) == ( + self.xmas05, self.depths[i] + ) + + def test_date_from_ooid(self): + for ooid in self.yyyyoids: + assert oo.date_from_ooid(ooid) == self.baseDate + assert oo.depth_from_ooid(ooid) == 4 + + assert oo.date_from_ooid(self.badooid0) is None + assert oo.date_from_ooid(self.badooid1) is None + + def test_date_and_depth_from_ooid(self): + for i in range(len(self.dyyoids)): + date, depth = oo.date_and_depth_from_ooid(self.dyyoids[i]) + assert depth == self.depths[i] + assert date == self.baseDate + + assert oo.date_and_depth_from_ooid(self.badooid0) == (None, None) + assert oo.date_and_depth_from_ooid(self.badooid1) == (None, None) From ce169043892c4cabdca2f45b8d5a81ab26245a35 Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Wed, 25 May 2016 12:01:56 -0400 Subject: [PATCH 2/4] Clean up imports Fixes the code so it doesn't import and rename modules. Further, it makes the results more pep8-y. --- antenna/lib/__init__.py | 1 - tests/test_ooid.py | 101 +++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/antenna/lib/__init__.py b/antenna/lib/__init__.py index 5501cd4b..6fbe8159 100644 --- a/antenna/lib/__init__.py +++ b/antenna/lib/__init__.py @@ -1,4 +1,3 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. - diff --git a/tests/test_ooid.py b/tests/test_ooid.py index 4350f99c..b89fecc7 100644 --- a/tests/test_ooid.py +++ b/tests/test_ooid.py @@ -5,114 +5,121 @@ # This is based on socorrolib's socorrolib/unittest/lib/testOoid.py module # with a heavy flake8/pytest pass. -import uuid as uu -import datetime as dt +import uuid +from datetime import datetime from unittest import TestCase from antenna.lib.datetimeutil import utc_now, UTC -import antenna.lib.ooid as oo +from antenna.lib.ooid import ( + create_new_ooid, + date_from_ooid, + date_and_depth_from_ooid, + depth_from_ooid, + DEFAULT_DEPTH, + uuid_to_ooid, +) class Test_ooid(TestCase): def setUp(self): - self.baseDate = dt.datetime(2008, 12, 25, tzinfo=UTC) + self.base_date = datetime(2008, 12, 25, tzinfo=UTC) self.rawuuids = [] self.yyyyoids = [] self.dyyoids = [] self.depths = [4, 4, 3, 3, 3, 2, 2, 2, 1, 1] - self.badooid0 = "%s%s" % (str(uu.uuid4())[:-8], 'ffeea1b2') - self.badooid1 = "%s%s" % (str(uu.uuid4())[:-8], 'f3eea1b2') + self.badooid0 = "%s%s" % (str(uuid.uuid4())[:-8], 'ffeea1b2') + self.badooid1 = "%s%s" % (str(uuid.uuid4())[:-8], 'f3eea1b2') for i in range(10): - self.rawuuids.append(str(uu.uuid4())) + self.rawuuids.append(str(uuid.uuid4())) assert len(self.depths) == len(self.rawuuids) for i in self.rawuuids: self.yyyyoids.append("%s%4d%02d%02d" % ( i[:-8], - self.baseDate.year, - self.baseDate.month, - self.baseDate.day + self.base_date.year, + self.base_date.month, + self.base_date.day )) for i in range(len(self.rawuuids)): self.dyyoids.append("%s%d%02d%02d%02d" % ( self.rawuuids[i][:-7], self.depths[i], - self.baseDate.year % 100, - self.baseDate.month, - self.baseDate.day + self.base_date.year % 100, + self.base_date.month, + self.base_date.day )) today = utc_now() - self.nowstamp = dt.datetime( + self.nowstamp = datetime( today.year, today.month, today.day, tzinfo=UTC ) - self.xmas05 = dt.datetime(2005, 12, 25, tzinfo=UTC) + self.xmas05 = datetime(2005, 12, 25, tzinfo=UTC) def test_create_new_ooid(self): - ooid = oo.create_new_ooid() - assert oo.date_from_ooid(ooid) == self.nowstamp - assert oo.depth_from_ooid(ooid) == oo.DEFAULT_DEPTH + ooid = create_new_ooid() + assert date_from_ooid(ooid) == self.nowstamp + assert depth_from_ooid(ooid) == DEFAULT_DEPTH - ooid = oo.create_new_ooid(timestamp=self.xmas05) - assert oo.date_from_ooid(ooid) == self.xmas05 - assert oo.depth_from_ooid(ooid) == oo.DEFAULT_DEPTH + ooid = create_new_ooid(timestamp=self.xmas05) + assert date_from_ooid(ooid) == self.xmas05 + assert depth_from_ooid(ooid) == DEFAULT_DEPTH for d in range(1, 5): - ooid0 = oo.create_new_ooid(depth=d) - ooid1 = oo.create_new_ooid(timestamp=self.xmas05, depth=d) + ooid0 = create_new_ooid(depth=d) + ooid1 = create_new_ooid(timestamp=self.xmas05, depth=d) - ndepth0 = oo.depth_from_ooid(ooid0) - ndepth1 = oo.depth_from_ooid(ooid1) + ndepth0 = depth_from_ooid(ooid0) + ndepth1 = depth_from_ooid(ooid1) - assert oo.date_from_ooid(ooid0) == self.nowstamp - assert oo.date_from_ooid(ooid1) == self.xmas05 + assert date_from_ooid(ooid0) == self.nowstamp + assert date_from_ooid(ooid1) == self.xmas05 assert ndepth0 == ndepth1 assert ndepth0 == d - assert oo.depth_from_ooid(self.badooid0) is None - assert oo.depth_from_ooid(self.badooid1) is None + assert depth_from_ooid(self.badooid0) is None + assert depth_from_ooid(self.badooid1) is None def test_uuid_to_ooid(self): for i in range(len(self.rawuuids)): u = self.rawuuids[i] - o0 = oo.uuid_to_ooid(u) - assert oo.date_and_depth_from_ooid(o0) == ( - self.nowstamp, oo.DEFAULT_DEPTH + o0 = uuid_to_ooid(u) + assert date_and_depth_from_ooid(o0) == ( + self.nowstamp, DEFAULT_DEPTH ) - o1 = oo.uuid_to_ooid(u, timestamp=self.baseDate) - assert oo.date_and_depth_from_ooid(o1) == ( - self.baseDate, oo.DEFAULT_DEPTH + o1 = uuid_to_ooid(u, timestamp=self.base_date) + assert date_and_depth_from_ooid(o1) == ( + self.base_date, DEFAULT_DEPTH ) - o2 = oo.uuid_to_ooid(u, depth=self.depths[i]) - assert oo.date_and_depth_from_ooid(o2) == ( + o2 = uuid_to_ooid(u, depth=self.depths[i]) + assert date_and_depth_from_ooid(o2) == ( self.nowstamp, self.depths[i] ) - o3 = oo.uuid_to_ooid( + o3 = uuid_to_ooid( u, depth=self.depths[i], timestamp=self.xmas05 ) - assert oo.date_and_depth_from_ooid(o3) == ( + assert date_and_depth_from_ooid(o3) == ( self.xmas05, self.depths[i] ) def test_date_from_ooid(self): for ooid in self.yyyyoids: - assert oo.date_from_ooid(ooid) == self.baseDate - assert oo.depth_from_ooid(ooid) == 4 + assert date_from_ooid(ooid) == self.base_date + assert depth_from_ooid(ooid) == 4 - assert oo.date_from_ooid(self.badooid0) is None - assert oo.date_from_ooid(self.badooid1) is None + assert date_from_ooid(self.badooid0) is None + assert date_from_ooid(self.badooid1) is None def test_date_and_depth_from_ooid(self): for i in range(len(self.dyyoids)): - date, depth = oo.date_and_depth_from_ooid(self.dyyoids[i]) + date, depth = date_and_depth_from_ooid(self.dyyoids[i]) assert depth == self.depths[i] - assert date == self.baseDate + assert date == self.base_date - assert oo.date_and_depth_from_ooid(self.badooid0) == (None, None) - assert oo.date_and_depth_from_ooid(self.badooid1) == (None, None) + assert date_and_depth_from_ooid(self.badooid0) == (None, None) + assert date_and_depth_from_ooid(self.badooid1) == (None, None) From f4c44a76c7964e4cc6fa836e22772f104df8c02e Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Fri, 20 May 2016 16:36:01 -0400 Subject: [PATCH 3/4] Add datetimeutil module and tests from socorrolib Antenna will be self-contained and won't depend on anything from socorro or socorrolib. However, there are a few things we need to use from socorrolib to make everything work. As such, my plan is to copy code from socorrolib that's very very unlikely to change. This copies over the datetimeutil code and its tests. It also updates both so they don't make our linter sad and also to take advantage of pytest magic. --- antenna/lib/datetimeutil.py | 33 +++++++++++++++++++++++++++++++++ requirements.txt | 1 + tests/test_datetimeutil.py | 17 +++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 antenna/lib/datetimeutil.py create mode 100644 tests/test_datetimeutil.py diff --git a/antenna/lib/datetimeutil.py b/antenna/lib/datetimeutil.py new file mode 100644 index 00000000..ceffd0dc --- /dev/null +++ b/antenna/lib/datetimeutil.py @@ -0,0 +1,33 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# This is based on socorrolib's socorrolib/lib/datetimeutil.py module. + +import datetime + +import isodate + +UTC = isodate.UTC + + +def utc_now(): + """Return a timezone aware datetime instance in UTC timezone + + This funciton is mainly for convenience. Compare: + + >>> from datetimeutil import utc_now + >>> utc_now() + datetime.datetime(2012, 1, 5, 16, 42, 13, 639834, + tzinfo=) + + Versus: + + >>> import datetime + >>> from datetimeutil import UTC + >>> datetime.datetime.now(UTC) + datetime.datetime(2012, 1, 5, 16, 42, 13, 639834, + tzinfo=) + + """ + return datetime.datetime.now(UTC) diff --git a/requirements.txt b/requirements.txt index b1854c3e..7c6b3362 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,6 @@ falcon==1.0.0 --hash=sha256:0145eb7fc001fb943b8b8f797832ce45302cf7c1edc769e28e5b gevent==1.1.1 --hash=sha256:6ee5b9851b2acde08df7ab9b9a2903f58b4b0e555405c444f4b1dd16f71caeea greenlet==0.4.9 --hash=sha256:58b2f3a2e7075c655616bf95e82868db4980f3bb6661db70ad02a51e4ddd2252 gunicorn==19.5.0 --hash=sha256:44cda4183586467493deaa616ac16f42b2f33bfaa57d4d5c07999d6db78c40ec +isodate==0.5.4 --hash=sha256:42105c41d037246dc1987e36d96f3752ffd5c0c24834dd12e4fdbe1e79544e31 python-mimeparse==1.5.2 --hash=sha256:bef134a59598cc6aa598f84553162aa7a0c01f3f431588225bb9a208964b1827 six==1.10.0 --hash=sha256:0ff78c403d9bccf5a425a6d31a12aa6b47f1c21ca4dc2573a7e2f32a97335eb1 diff --git a/tests/test_datetimeutil.py b/tests/test_datetimeutil.py new file mode 100644 index 00000000..3c8a3ee2 --- /dev/null +++ b/tests/test_datetimeutil.py @@ -0,0 +1,17 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# This is based on socorrolib's socorrolib/unittest/lib/test_datetimeutil.py. + +from antenna.lib import datetimeutil + + +UTC = datetimeutil.UTC + + +def test_utc_now(): + res = datetimeutil.utc_now() + assert res.strftime('%Z') == 'UTC' + assert res.strftime('%z') == '+0000' + assert res.tzinfo From 0126fe2a9ff7d48cbf5130ec95da26867d61324d Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Wed, 25 May 2016 14:26:28 -0400 Subject: [PATCH 4/4] Fix more import renaming --- antenna/lib/ooid.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/antenna/lib/ooid.py b/antenna/lib/ooid.py index 124b0d5e..4087f0e0 100644 --- a/antenna/lib/ooid.py +++ b/antenna/lib/ooid.py @@ -5,8 +5,8 @@ # This is based on socorrolib's socorrolib/lib/ooid.py module. # OOID is "Our opaque ID" -import datetime as dt -import uuid as uu +import uuid +from datetime import datetime from antenna.lib.datetimeutil import utc_now, UTC @@ -31,9 +31,9 @@ def create_new_ooid(timestamp=None, depth=None): if not depth: depth = DEFAULT_DEPTH assert depth <= 4 and depth >= 1 - uuid = str(uu.uuid4()) + id_ = str(uuid.uuid4()) return "%s%d%02d%02d%02d" % ( - uuid[:-7], depth, timestamp.year % 100, timestamp.month, timestamp.day + id_[:-7], depth, timestamp.year % 100, timestamp.month, timestamp.day ) @@ -84,7 +84,7 @@ def date_and_depth_from_ooid(ooid): depth = int(ooid[-7]) if not depth: depth = OLD_HARD_DEPTH - return (dt.datetime(year, month, day, tzinfo=UTC), depth) + return (datetime(year, month, day, tzinfo=UTC), depth) except Exception: return None, None return None, None