Skip to content

Commit

Permalink
Merge pull request #28 from metaodi/datetime-objects
Browse files Browse the repository at this point in the history
Parse dates as datetime objects
  • Loading branch information
metaodi committed Oct 7, 2014
2 parents 6267be5 + a320220 commit 36a2ed5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
17 changes: 14 additions & 3 deletions osmapi/OsmApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time
import sys
import urllib
from datetime import datetime

# Python 3.x
if getattr(urllib, 'urlencode', None) is None:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 #
##################################################
Expand Down
47 changes: 24 additions & 23 deletions tests/notes_tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,20 +37,20 @@ 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': "<p>Is it Paynes or Payne's</p>",
'uid': '1486336',
'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':
Expand All @@ -76,20 +77,20 @@ 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': "<p>It does not exist this path</p>",
'uid': '1363438',
'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': "<p>there is no path signed</p>",
Expand Down Expand Up @@ -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': "<p>This is a test</p>",
Expand All @@ -153,20 +154,20 @@ 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': "<p>This is a test</p>",
'uid': '1841',
'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': "<p>This is a comment</p>",
Expand All @@ -192,20 +193,20 @@ 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': "<p>This is a test</p>",
'uid': '1841',
'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': "<p>Close this note!</p>",
Expand All @@ -231,28 +232,28 @@ 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': "<p>This is a test</p>",
'uid': '1841',
'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': "<p>Close this note!</p>",
'uid': '1841',
'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': "<p>Reopen this note!</p>",
Expand Down Expand Up @@ -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': "<p>One way street:\n<br />comment</p>",
Expand Down

0 comments on commit 36a2ed5

Please sign in to comment.