From e8eaf34662ab6d2361d39db68a877dd028ede885 Mon Sep 17 00:00:00 2001 From: Monsyne Dragon Date: Tue, 2 Dec 2014 15:54:15 +0000 Subject: [PATCH] Add fixes for basic stream api support Fixed api typo 'distinquished' Added proper json serialization for datetime and timerange Change-Id: I6af8bb15b5e9e65e7c6965cd80e1483b3df04b99 --- .gitreview | 5 +++++ quincy/jsonutil.py | 35 +++++++++++++++++++++++++++++++++++ quincy/v1_api.py | 12 ++++++------ quincy/v1_impl.py | 6 +++--- requirements.txt | 1 + 5 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 .gitreview create mode 100644 quincy/jsonutil.py diff --git a/.gitreview b/.gitreview new file mode 100644 index 0000000..7fab225 --- /dev/null +++ b/.gitreview @@ -0,0 +1,5 @@ +[gerrit] +host=review.openstack.org +port=29418 +project=stackforge/stacktach-quincy.git + diff --git a/quincy/jsonutil.py b/quincy/jsonutil.py new file mode 100644 index 0000000..7894000 --- /dev/null +++ b/quincy/jsonutil.py @@ -0,0 +1,35 @@ +import json +import datetime +import timex + + +class ObjectEncoder(json.JSONEncoder): + + ENCODE_MAP = {datetime.datetime: "datetime", + timex.TimeRange: "timex.TimeRange", + timex.Timestamp: "timex.Timestamp"} + + def default(self, obj): + if type(obj) in self.ENCODE_MAP: + typename = self.ENCODE_MAP[type(obj)] + encoder = getattr(self, '_encode_' + typename.replace('.', '_').lower()) + return encoder(obj, typename) + return super(ObjectEncoder, self).default(obj) + + def _encode_datetime(self, obj, name): + return {'__type__' : name, + 'datetime': obj.isoformat()} + + def _encode_timex_timestamp(self, obj, name): + return {'__type__' : name, + 'timestamp': obj.timestamp.isoformat()} + + def _encode_timex_timerange(self, obj, name): + return {'__type__' : name, + 'begin': obj.begin.isoformat(), + 'end': obj.end.isoformat()} + + +def dumps(obj, **kw): + kw['cls'] = ObjectEncoder + return json.dumps(obj, **kw) diff --git a/quincy/v1_api.py b/quincy/v1_api.py index 4fe517f..0704b1f 100644 --- a/quincy/v1_api.py +++ b/quincy/v1_api.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from quincy import jsonutil from dateutil import parser @@ -31,7 +31,7 @@ class StreamCollection(common.FalconBase): # younger_than # state # trigger_name - # distinquishing_traits - find stream by dtrait values. + # distinguishing_traits - find stream by dtrait values. # # Actions on a Stream: # details - get full details on stream (including events & @@ -42,7 +42,7 @@ class StreamCollection(common.FalconBase): younger_than = req.get_param('younger_than') state = req.get_param('state') trigger = req.get_param('trigger_name') - traits = req.get_param('distinquishing_traits') + traits = req.get_param('distinguishing_traits') if older_than: older_than = parser.parse(older_than) @@ -54,15 +54,15 @@ class StreamCollection(common.FalconBase): younger_than=younger_than, state=state, trigger_name=trigger, - distinquishing_traits=traits) - resp.body = json.dumps(streams) + distinguishing_traits=traits) + resp.body = jsonutil.dumps(streams) class StreamItem(common.FalconBase): def on_get(self, req, resp, stream_id, action=None): details = req.get_param('details') streams = self.impl.get_stream(stream_id, details) - resp.body = json.dumps(streams) + resp.body = jsonutil.dumps(streams) def on_delete(self, req, resp, stream_id): self.impl.delete_stream(stream_id) diff --git a/quincy/v1_impl.py b/quincy/v1_impl.py index 8234e77..ae18dd5 100644 --- a/quincy/v1_impl.py +++ b/quincy/v1_impl.py @@ -23,14 +23,14 @@ class Stream(object): self.stream_id = stream_id self.trigger_name = trigger_name self.state = state - self.distinquishing_traits = [] + self.distinguishing_traits = [] def to_dict(self): return {"last_updated": str(self.last_updated), "stream_id": self.stream_id, "trigger_name": self.trigger_name, "state": self.state, - "distinquishing_traits": self.distinquishing_traits} + "distinguishing_traits": self.distinguishing_traits} class Impl(object): @@ -44,7 +44,7 @@ class Impl(object): younger_than state trigger_name - distinquishing_traits + distinguishing_traits """ x = [Stream(str(uuid.uuid4()), "EOD-Exists", "Collecting"), Stream(str(uuid.uuid4()), "EOD-Exists", "Error"), diff --git a/requirements.txt b/requirements.txt index d82fa8e..03136a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ python-dateutil falcon simport >= 0.0.dev0 +timex