Add support for basic streams api

Added supoort  for streams api w/ winchester.
Added json deserialization support for datetime & timerange types
Fixed api typo ('distinquished' vs 'distinguished')

Change-Id: Ibae617b5bc58fd9c6f41ae30e90d5d2cbc9fc3fa
This commit is contained in:
Monsyne Dragon 2014-12-02 15:48:36 +00:00
parent 146406a4f9
commit 04e8283e92
6 changed files with 49 additions and 8 deletions

5
.gitreview Normal file
View File

@ -0,0 +1,5 @@
[gerrit]
host=review.openstack.org
port=29418
project=stackforge/stacktach-klugman.git

View File

@ -27,7 +27,7 @@ def dump_response(keys, rows):
for row in rows:
x = prettytable.PrettyTable(["Property", "Value"])
for key in keys:
x.add_row([key, row[key]])
x.add_row([key, row.get(key)])
print x

31
klugman/jsonutil.py Normal file
View File

@ -0,0 +1,31 @@
import datetime
import timex
import iso8601
def decode_datetime(dct, name):
return iso8601.parse_date(dct['datetime'], default_timezone=None)
def decode_timerange(dct, name):
begin = iso8601.parse_date(dct['begin'], default_timezone=None)
end = iso8601.parse_date(dct['end'], default_timezone=None)
return timex.TimeRange(begin=begin, end=end)
def decode_timestamp(dct, name):
timestamp = iso8601.parse_date(dct['timestamp'], default_timezone=None)
return timex.Timestamp(timestamp)
DECODE_MAP = {'datetime': decode_datetime,
'timex.TimeRange': decode_timerange,
'timex.Timestamp': decode_timestamp}
def object_hook(dct):
if '__type__' in dct:
name = dct['__type__']
decoder = DECODE_MAP[name]
return decoder(dct, name)
return dct

View File

@ -15,6 +15,7 @@
import base
import jsonutil
from docopt import docopt
@ -36,7 +37,7 @@ class Streams(object):
list streams younger than datetime
--trigger_name <name>
list streams with given trigger definition
--distinquishing_traits <dtraits>
--distinguishing_traits <dtraits>
list stream with specific distriquishing traits
Stream states:
@ -58,12 +59,13 @@ class Streams(object):
response = self.do_streams(version, arguments)
# Handle cmdline output here, not in do_foo()
raw_rows = response.json()
raw_rows = response.json(object_hook=jsonutil.object_hook)
# TODO(sandy): This should come from the server-issued
# schema at some point.
keys = ['stream_id', 'state', 'last_updated', 'trigger_name',
'distinquishing_traits']
keys = ['id', 'state', 'name', 'first_event', 'last_event',
'fire_timestamp', 'expire_timestamp',
'distinguishing_traits', 'events']
base.dump_response(keys, raw_rows)
def do_streams(self, version, arguments):
@ -72,7 +74,7 @@ class Streams(object):
older = arguments.get('--older_than')
younger = arguments.get('--younger_than')
trigger = arguments.get('--trigger_name')
traits = arguments.get('--distinquishing_traits')
traits = arguments.get('--distinguishing_traits')
details = arguments.get('--details')
cmd = "streams"
@ -84,7 +86,7 @@ class Streams(object):
'older_than': older,
'younger_than': younger,
'trigger_name': trigger,
'distinquishing_traits': traits,
'distinguishing_traits': traits,
'details': details})
return base.get(version.base_url, cmd, params)

View File

@ -16,6 +16,7 @@
import base
import v1
import jsonutil
from docopt import docopt
import requests
@ -38,7 +39,7 @@ class Archives(object):
print arguments
response = self.do_archives(version, arguments)
raw_rows = response.json()
raw_rows = response.json(object_hook=jsonutil.object_hook)
keys = ['id', 'filename']
base.dump_response(keys, raw_rows)

View File

@ -1,3 +1,5 @@
docopt
prettytable
requests
timex
iso8601