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