Merge lp:~leonardr/launchpadlib/convert-datetime-309950 into lp:~launchpad-pqm/launchpadlib/devel

Proposed by Leonard Richardson
Status: Merged
Merged at revision: not available
Proposed branch: lp:~leonardr/launchpadlib/convert-datetime-309950
Merge into: lp:~launchpad-pqm/launchpadlib/devel
To merge this branch: bzr merge lp:~leonardr/launchpadlib/convert-datetime-309950
Reviewer Review Type Date Requested Status
Edwin Grubbs (community) Approve
Gary Poster (community) code Approve
Launchpad PQM Bot Pending
Review via email: mp+3023@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Leonard Richardson (leonardr) wrote :
Download full text (5.2 KiB)

This is a launchpadlib branch that reflects changes made to a recent
wadllib branch. Some fields that used to be strings now come in as
Python datetime objects. This branch changes tests to reflect this
fact, and also makes launchpadlib capable of turning those datetime
objects back into strings when the time comes to make a PATCH or POST
request.

This branch does not deserialize dates retrieved from documents that
are the result of a named operation--that would be wadllib's job, and
right now our WADL doesn't provide the necessary tools.

=== modified file 'launchpadlib/_browser.py'
--- launchpadlib/_browser.py 2008-12-05 21:38:50 +0000
+++ launchpadlib/_browser.py 2009-01-21 21:27:08 +0000
@@ -43,7 +43,8 @@
 from launchpadlib.errors import HTTPError
 from launchpadlib._oauth.oauth import (
     OAuthRequest, OAuthSignatureMethod_PLAINTEXT)
-from launchpadlib._utils import uri
+from launchpadlib._utils import uri, json
+from launchpadlib._utils.json import DatetimeJSONEncoder

 OAUTH_REALM = 'https://api.launchpad.net'
@@ -261,5 +262,5 @@
             headers['If-Match'] = cached_etag

         self._request(
- url, simplejson.dumps(representation), 'PATCH',
- extra_headers=extra_headers)
+ url, simplejson.dumps(representation, cls=DatetimeJSONEncoder),
+ 'PATCH', extra_headers=extra_headers)

=== added file 'launchpadlib/_utils/json.py'
--- launchpadlib/_utils/json.py 1970-01-01 00:00:00 +0000
+++ launchpadlib/_utils/json.py 2009-01-21 20:59:09 +0000
@@ -0,0 +1,35 @@
+# Copyright 2009 Canonical Ltd.
+
+# This file is part of launchpadlib.
+#
+# launchpadlib is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# launchpadlib is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with launchpadlib. If not, see
+# <http://www.gnu.org/licenses/>.
+
+"""Classes for working with JSON."""
+
+__metaclass__ = type
+__all__ = ['DatetimeJSONEncoder']
+
+import datetime
+import simplejson
+
+class DatetimeJSONEncoder(simplejson.JSONEncoder):
+ """A JSON encoder that understands datetime objects.
+
+ Datetime objects are formatted according to ISO 1601.
+ """
+ def default(self, obj):
+ if isinstance(obj, datetime.datetime):
+ return obj.isoformat()
+ return simplejson.JSONEncoder.default(self, obj)

=== modified file 'launchpadlib/docs/modifications.txt'
--- launchpadlib/docs/modifications.txt 2009-01-12 13:25:47 +0000
+++ launchpadlib/docs/modifications.txt 2009-01-21 21:10:24 +0000
@@ -305,3 +305,26 @@
     A conflicting display name

+== Data types ==
+
+From the perspective of the launchpadlib user, date and date-time
+fields always look like Python datetime objects.
+
+ >>> firefox = launchpad.project...

Read more...

Revision history for this message
Gary Poster (gary) wrote :
Download full text (5.2 KiB)

*merge-conditional (Edwin)

This is a small change, but really is nice. Just about everything looks good
to me. I only have one small style niggle, below.

> === modified file 'launchpadlib/_browser.py'
> --- launchpadlib/_browser.py 2008-12-05 21:38:50 +0000
> +++ launchpadlib/_browser.py 2009-01-21 21:38:08 +0000
> @@ -43,7 +43,8 @@
> from launchpadlib.errors import HTTPError
> from launchpadlib._oauth.oauth import (
> OAuthRequest, OAuthSignatureMethod_PLAINTEXT)
> -from launchpadlib._utils import uri
> +from launchpadlib._utils import uri, json
> +from launchpadlib._utils.json import DatetimeJSONEncoder
>
>
> OAUTH_REALM = 'https://api.launchpad.net'
> @@ -261,5 +262,5 @@
> headers['If-Match'] = cached_etag
>
> self._request(
> - url, simplejson.dumps(representation), 'PATCH',
> - extra_headers=extra_headers)
> + url, simplejson.dumps(representation, cls=DatetimeJSONEncoder),
> + 'PATCH', extra_headers=extra_headers)
>
> === added file 'launchpadlib/_utils/json.py'
> --- launchpadlib/_utils/json.py 1970-01-01 00:00:00 +0000
> +++ launchpadlib/_utils/json.py 2009-01-21 21:38:08 +0000
> @@ -0,0 +1,35 @@
> +# Copyright 2009 Canonical Ltd.
> +
> +# This file is part of launchpadlib.
> +#
> +# launchpadlib is free software: you can redistribute it and/or modify
> +# it under the terms of the GNU Lesser General Public License as
> +# published by the Free Software Foundation, either version 3 of the
> +# License, or (at your option) any later version.
> +#
> +# launchpadlib is distributed in the hope that it will be useful, but
> +# WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with launchpadlib. If not, see
> +# <http://www.gnu.org/licenses/>.
> +
> +"""Classes for working with JSON."""
> +
> +__metaclass__ = type
> +__all__ = ['DatetimeJSONEncoder']
> +
> +import datetime
> +import simplejson
> +
> +class DatetimeJSONEncoder(simplejson.JSONEncoder):
> + """A JSON encoder that understands datetime objects.
> +
> + Datetime objects are formatted according to ISO 1601.
> + """
> + def default(self, obj):
> + if isinstance(obj, datetime.datetime):
> + return obj.isoformat()
> + return simplejson.JSONEncoder.default(self, obj)
>
> === modified file 'launchpadlib/docs/modifications.txt'
> --- launchpadlib/docs/modifications.txt 2009-01-12 13:25:47 +0000
> +++ launchpadlib/docs/modifications.txt 2009-01-21 21:38:08 +0000
> @@ -305,3 +305,26 @@
> A conflicting display name
>
>
> +== Data types ==
> +
> +From the perspective of the launchpadlib user, date and date-time
> +fields always look like Python datetime objects.
> +
> + >>> firefox = launchpad.projects['firefox']
> + >>> milestone = firefox.all_milestones[0]
> + >>> milestone.date_targeted
> + datetime.datetime(2056, 10, 16,...)
> +
> +These fields can be changed and written back to the server, just like
> +objects of other t...

Read more...

review: Approve (code)
35. By Leonard Richardson

Fixed spacing issue.

Revision history for this message
Leonard Richardson (leonardr) wrote :

I've fixed the spacing.

Revision history for this message
Edwin Grubbs (edwin-grubbs) wrote :

Hi Leonard,

This is a nice change. I just have a concern about datetime.date support, which I am assuming should be included in this branch. See below.

merge-conditional

=== added file 'launchpadlib/_utils/json.py'
--- launchpadlib/_utils/json.py 1970-01-01 00:00:00 +0000
+++ launchpadlib/_utils/json.py 2009-01-21 20:59:09 +0000
@@ -0,0 +1,35 @@
+# Copyright 2009 Canonical Ltd.
+
+# This file is part of launchpadlib.
+#
+# launchpadlib is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# launchpadlib is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with launchpadlib. If not, see
+# <http://www.gnu.org/licenses/>.
+
+"""Classes for working with JSON."""
+
+__metaclass__ = type
+__all__ = ['DatetimeJSONEncoder']
+
+import datetime
+import simplejson
+
+class DatetimeJSONEncoder(simplejson.JSONEncoder):
+ """A JSON encoder that understands datetime objects.
+
+ Datetime objects are formatted according to ISO 1601.
+ """
+ def default(self, obj):
+ if isinstance(obj, datetime.datetime):
+ return obj.isoformat()
+ return simplejson.JSONEncoder.default(self, obj)

A datetime.datetime object is also an instance of the datetime.date
class, but not the other way around. You also don't have any tests
for datetime.date objects.

review: Approve
Revision history for this message
Leonard Richardson (leonardr) wrote :

> +class DatetimeJSONEncoder(simplejson.JSONEncoder):
> + """A JSON encoder that understands datetime objects.
> +
> + Datetime objects are formatted according to ISO 1601.
> + """
> + def default(self, obj):
> + if isinstance(obj, datetime.datetime):
> + return obj.isoformat()
> + return simplejson.JSONEncoder.default(self, obj)
>
>
> A datetime.datetime object is also an instance of the datetime.date
> class, but not the other way around. You also don't have any tests
> for datetime.date objects.

wadllib only gives out datetime.datetime objects, so there's no need to
write code for date objects. If you want I can do it anyway.

Leonard

Subscribers

People subscribed via source and target branches