From a32022098d9aedf7398316ee2286aa5eee040c51 Mon Sep 17 00:00:00 2001 From: Stefan Oderbolz Date: Tue, 7 Oct 2014 23:34:11 +0200 Subject: [PATCH 1/2] Parse dates as datetime objects --- osmapi/OsmApi.py | 17 +++++++++++++--- tests/notes_tests.py | 47 ++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/osmapi/OsmApi.py b/osmapi/OsmApi.py index 5518d24..ddeebbd 100644 --- a/osmapi/OsmApi.py +++ b/osmapi/OsmApi.py @@ -10,6 +10,7 @@ import time import sys import urllib +from datetime import datetime # Python 3.x if getattr(urllib, 'urlencode', None) is None: @@ -1043,7 +1044,7 @@ def _DomGetComments(self, DomElement): result = [] for t in DomElement.getElementsByTagName("comment"): comment = {} - comment['date'] = self._GetXmlValue(t, "date") + comment['date'] = self._ParseDate(self._GetXmlValue(t, "date")) comment['action'] = self._GetXmlValue(t, "action") comment['text'] = self._GetXmlValue(t, "text") comment['html'] = self._GetXmlValue(t, "html") @@ -1103,12 +1104,22 @@ def _DomParseNote(self, DomElement): result["id"] = self._GetXmlValue(DomElement, "id") result["status"] = self._GetXmlValue(DomElement, "status") - result["date_created"] = self._GetXmlValue(DomElement, "date_created") - result["date_closed"] = self._GetXmlValue(DomElement, "date_closed") + result["date_created"] = self._ParseDate( + self._GetXmlValue(DomElement, "date_created") + ) + result["date_closed"] = self._ParseDate( + self._GetXmlValue(DomElement, "date_closed") + ) result["comments"] = self._DomGetComments(DomElement) return result + def _ParseDate(self, DateString): + try: + return datetime.strptime(DateString, "%Y-%m-%d %H:%M:%S UTC") + except: + return None + ################################################## # Internal xml builder # ################################################## diff --git a/tests/notes_tests.py b/tests/notes_tests.py index 768b3d3..06770b3 100644 --- a/tests/notes_tests.py +++ b/tests/notes_tests.py @@ -1,6 +1,7 @@ from __future__ import (unicode_literals, absolute_import) from nose.tools import * # noqa from . import osmapi_tests +from datetime import datetime try: import urlparse @@ -36,12 +37,12 @@ def test_NotesGet(self): 'id': '231775', 'lon': -1.4929605, 'lat': 52.4107312, - 'date_created': '2014-08-28 19:25:37 UTC', - 'date_closed': '2014-09-27 09:21:41 UTC', + 'date_created': datetime(2014, 8, 28, 19, 25, 37), + 'date_closed': datetime(2014, 9, 27, 9, 21, 41), 'status': 'closed', 'comments': [ { - 'date': '2014-08-28 19:25:37 UTC', + 'date': datetime(2014, 8, 28, 19, 25, 37), 'action': 'opened', 'text': "Is it Paynes or Payne's", 'html': "

Is it Paynes or Payne's

", @@ -49,7 +50,7 @@ def test_NotesGet(self): 'user': 'Wyken Seagrave' }, { - 'date': '2014-09-26 13:05:33 UTC', + 'date': datetime(2014, 9, 26, 13, 5, 33), 'action': 'commented', 'text': "Royal Mail's postcode finder has PAYNES LANE", 'html': @@ -76,12 +77,12 @@ def test_NoteGet(self): 'id': '1111', 'lon': 12.3133135, 'lat': 37.9305489, - 'date_created': '2013-05-01 20:58:21 UTC', - 'date_closed': '2013-08-21 16:43:26 UTC', + 'date_created': datetime(2013, 5, 1, 20, 58, 21), + 'date_closed': datetime(2013, 8, 21, 16, 43, 26), 'status': 'closed', 'comments': [ { - 'date': '2013-05-01 20:58:21 UTC', + 'date': datetime(2013, 5, 1, 20, 58, 21), 'action': 'opened', 'text': "It does not exist this path", 'html': "

It does not exist this path

", @@ -89,7 +90,7 @@ def test_NoteGet(self): 'user': 'giuseppemari' }, { - 'date': '2013-08-21 16:43:26 UTC', + 'date': datetime(2013, 8, 21, 16, 43, 26), 'action': 'closed', 'text': "there is no path signed", 'html': "

there is no path signed

", @@ -122,12 +123,12 @@ def test_NoteCreate(self): 'id': '816', 'lat': 47.123, 'lon': 8.432, - 'date_created': '2014-10-03 15:21:21 UTC', + 'date_created': datetime(2014, 10, 3, 15, 21, 21), 'date_closed': None, 'status': 'open', 'comments': [ { - 'date': '2014-10-03 15:21:22 UTC', + 'date': datetime(2014, 10, 3, 15, 21, 22), 'action': 'opened', 'text': "This is a test", 'html': "

This is a test

", @@ -153,12 +154,12 @@ def test_NoteComment(self): 'id': '812', 'lat': 47.123, 'lon': 8.432, - 'date_created': '2014-10-03 15:11:05 UTC', + 'date_created': datetime(2014, 10, 3, 15, 11, 5), 'date_closed': None, 'status': 'open', 'comments': [ { - 'date': '2014-10-03 15:11:05 UTC', + 'date': datetime(2014, 10, 3, 15, 11, 5), 'action': 'opened', 'text': "This is a test", 'html': "

This is a test

", @@ -166,7 +167,7 @@ def test_NoteComment(self): 'user': 'metaodi' }, { - 'date': '2014-10-04 22:36:35 UTC', + 'date': datetime(2014, 10, 4, 22, 36, 35), 'action': 'commented', 'text': "This is a comment", 'html': "

This is a comment

", @@ -192,12 +193,12 @@ def test_NoteClose(self): 'id': '815', 'lat': 47.123, 'lon': 8.432, - 'date_created': '2014-10-03 15:20:57 UTC', - 'date_closed': '2014-10-05 16:35:13 UTC', + 'date_created': datetime(2014, 10, 3, 15, 20, 57), + 'date_closed': datetime(2014, 10, 5, 16, 35, 13), 'status': 'closed', 'comments': [ { - 'date': '2014-10-03 15:20:57 UTC', + 'date': datetime(2014, 10, 3, 15, 20, 57), 'action': 'opened', 'text': "This is a test", 'html': "

This is a test

", @@ -205,7 +206,7 @@ def test_NoteClose(self): 'user': 'metaodi' }, { - 'date': '2014-10-05 16:35:13 UTC', + 'date': datetime(2014, 10, 5, 16, 35, 13), 'action': 'closed', 'text': "Close this note!", 'html': "

Close this note!

", @@ -231,12 +232,12 @@ def test_NoteReopen(self): 'id': '815', 'lat': 47.123, 'lon': 8.432, - 'date_created': '2014-10-03 15:20:57 UTC', + 'date_created': datetime(2014, 10, 3, 15, 20, 57), 'date_closed': None, 'status': 'open', 'comments': [ { - 'date': '2014-10-03 15:20:57 UTC', + 'date': datetime(2014, 10, 3, 15, 20, 57), 'action': 'opened', 'text': "This is a test", 'html': "

This is a test

", @@ -244,7 +245,7 @@ def test_NoteReopen(self): 'user': 'metaodi' }, { - 'date': '2014-10-05 16:35:13 UTC', + 'date': datetime(2014, 10, 5, 16, 35, 13), 'action': 'closed', 'text': "Close this note!", 'html': "

Close this note!

", @@ -252,7 +253,7 @@ def test_NoteReopen(self): 'user': 'metaodi' }, { - 'date': '2014-10-05 16:44:56 UTC', + 'date': datetime(2014, 10, 5, 16, 44, 56), 'action': 'reopened', 'text': "Reopen this note!", 'html': "

Reopen this note!

", @@ -281,12 +282,12 @@ def test_NotesSearch(self): 'id': '788', 'lon': 11.96395, 'lat': 57.70301, - 'date_created': '2014-07-16 16:12:41 UTC', + 'date_created': datetime(2014, 7, 16, 16, 12, 41), 'date_closed': None, 'status': 'open', 'comments': [ { - 'date': '2014-07-16 16:12:41 UTC', + 'date': datetime(2014, 7, 16, 16, 12, 41), 'action': 'opened', 'text': "One way street:\ncomment", 'html': "

One way street:\n
comment

", From d6847bb2f7783bc8527a0973cc0b744f5f9e363d Mon Sep 17 00:00:00 2001 From: Stefan Oderbolz Date: Wed, 8 Oct 2014 09:57:26 +0200 Subject: [PATCH 2/2] Bump version number to 0.4.1 --- osmapi/__init__.py | 2 +- tests/changeset_tests.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/osmapi/__init__.py b/osmapi/__init__.py index 6e50aba..a0381e7 100644 --- a/osmapi/__init__.py +++ b/osmapi/__init__.py @@ -1,5 +1,5 @@ from __future__ import (absolute_import, print_function, unicode_literals) -__version__ = '0.4.0' +__version__ = '0.4.1' from .OsmApi import * # noqa diff --git a/tests/changeset_tests.py b/tests/changeset_tests.py index fdaa30e..1a46e7d 100644 --- a/tests/changeset_tests.py +++ b/tests/changeset_tests.py @@ -98,10 +98,10 @@ def test_ChangesetUpdate(self): xmltosorteddict(args[3]), xmltosorteddict( b'\n' - b'\n' + b'\n' b' \n' b' \n' - b' \n' + b' \n' b' \n' b'\n' ) @@ -132,7 +132,7 @@ def test_ChangesetUpdate_with_created_by(self): xmltosorteddict(args[3]), xmltosorteddict( b'\n' - b'\n' + b'\n' b' \n' b' \n' b' \n' @@ -171,10 +171,10 @@ def test_ChangesetCreate(self): xmltosorteddict(args[3]), xmltosorteddict( b'\n' - b'\n' + b'\n' b' \n' b' \n' - b' \n' + b' \n' b' \n' b'\n' ) @@ -199,7 +199,7 @@ def test_ChangesetCreate_with_created_by(self): xmltosorteddict(args[3]), xmltosorteddict( b'\n' - b'\n' + b'\n' b' \n' b' \n' b' \n' @@ -285,7 +285,7 @@ def test_ChangesetUpload_create_node(self): xmltosorteddict(args[3]), xmltosorteddict( b'\n' - b'\n' + b'\n' b'\n' b' \n' @@ -359,7 +359,7 @@ def test_ChangesetUpload_modify_way(self): xmltosorteddict(args[3]), xmltosorteddict( b'\n' - b'\n' + b'\n' b'\n' b' \n' @@ -443,7 +443,7 @@ def test_ChangesetUpload_delete_relation(self): xmltosorteddict(args[3]), xmltosorteddict( b'\n' - b'\n' + b'\n' b'\n' b' \n'