Add PEP8 check and fix related issues

- Add PEP8 section to tox.ini
- Add hacking to requirements to enforce OpenStack style requirements
- Fix formatting issues flagged by flake8 check
- Add copyright notices to all remaining files
- Update .gitignore file

Change-Id: Iaeee85a78a6625c6ffb711988a77e13e1b5e5dab
This commit is contained in:
Levi Blackstone 2015-05-04 14:57:47 -05:00
parent 0acfda92d8
commit d64a16bf2f
6 changed files with 95 additions and 53 deletions

4
.gitignore vendored
View File

@ -52,3 +52,7 @@ coverage.xml
# Sphinx documentation # Sphinx documentation
docs/_build/ docs/_build/
# IDE Project Files
*.project
*.pydev*
*.idea

View File

@ -15,7 +15,6 @@
import jsonutil import jsonutil
from docopt import docopt
import prettytable import prettytable
import requests import requests
@ -31,15 +30,15 @@ def dump_response(keys, rows):
for key in keys: for key in keys:
if key in row: if key in row:
x.add_row([key, row[key]]) x.add_row([key, row[key]])
print x print(x)
def get(url, cmd, params, debug=False): def get(url, cmd, params, debug=False):
final = "%s/%s" % (url, cmd) final = "%s/%s" % (url, cmd)
if debug: if debug:
print 'URL: %s' % final print('URL: %s' % final)
for item in params.items(): for item in params.items():
print " : %s='%s'" % item print(" : %s='%s'" % item)
ret = requests.get(final, params=params) ret = requests.get(final, params=params)
ret.raise_for_status() ret.raise_for_status()
return ret.json(object_hook=jsonutil.object_hook) return ret.json(object_hook=jsonutil.object_hook)

View File

@ -1,6 +1,21 @@
import datetime # Copyright (c) 2014 Dark Secret Software Inc.
import timex # Copyright (c) 2015 Rackspace
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import iso8601 import iso8601
import timex
def _present(dct, name): def _present(dct, name):

View File

@ -14,9 +14,7 @@
# limitations under the License. # limitations under the License.
import argparse
import prettytable import prettytable
import sys
import base import base
@ -26,30 +24,41 @@ class Streams(object):
self.url = url self.url = url
if subparser: if subparser:
parser = subparser.add_parser('streams') parser = subparser.add_parser('streams')
parser.add_argument('--name', metavar='trigger_name', parser.add_argument(
help='Return streams of type trigger_name.') '--name', metavar='trigger_name',
parser.add_argument('--from', metavar='datetime', dest='_from', help='Return streams of type trigger_name.')
help='Return streams last updated after datetime') parser.add_argument(
parser.add_argument('--to', metavar='datetime', '--from', metavar='datetime', dest='_from',
help='Return streams last updated before datetime') help='Return streams last updated after datetime')
parser.add_argument('--traits', metavar='trait_list', parser.add_argument(
help='Return streams with specific distinguishing traits.') '--to', metavar='datetime',
parser.add_argument('--details', action='store_true', help='Return streams last updated before datetime')
default=False, help='Return full event details.') parser.add_argument(
parser.add_argument('--state', choices=['active', 'firing', '--traits', metavar='trait_list',
'expiring', 'error', 'expire_error', 'completed', help='Return streams with specific distinguishing traits.')
'retry_fire', 'retry_expire'], parser.add_argument(
help='Only return streams in this state.') '--details', action='store_true',
default=False,
help='Return full event details.')
parser.add_argument(
'--state', choices=['active', 'firing',
'expiring', 'error',
'expire_error', 'completed',
'retry_fire',
'retry_expire'],
help='Only return streams in this state.')
group = parser.add_mutually_exclusive_group() group = parser.add_mutually_exclusive_group()
group.add_argument('--count', action='store_true', group.add_argument(
default=False, '--count', action='store_true',
help='Return a count of streams matching filter criteria.') default=False,
group.add_argument('--id', metavar='stream_id', dest='stream_id', help='Return a count of streams matching filter criteria.')
help='Return a single specific stream by id.') group.add_argument(
'--id', metavar='stream_id', dest='stream_id',
help='Return a single specific stream by id.')
def get_streams_count(self, from_datetime=None, to_datetime=None, def get_streams_count(self, from_datetime=None, to_datetime=None,
traits=None, state=None, name=None, debug=False): traits=None, state=None, name=None, debug=False):
if traits: if traits:
traits = ",".join(["%s:%s" % item for item in traits.items()]) traits = ",".join(["%s:%s" % item for item in traits.items()])
cmd = "streams/count" cmd = "streams/count"
@ -62,8 +71,8 @@ class Streams(object):
return base.get(self.url, cmd, params, debug=debug) return base.get(self.url, cmd, params, debug=debug)
def get_streams(self, from_datetime=None, to_datetime=None, def get_streams(self, from_datetime=None, to_datetime=None,
traits=None, name=None, stream_id=None, debug=False, traits=None, name=None, stream_id=None, debug=False,
state=None, details=None): state=None, details=None):
if stream_id: if stream_id:
params = base.remove_empty({'details': details}) params = base.remove_empty({'details': details})
return base.get(self.url, "streams/%s" % stream_id, params, return base.get(self.url, "streams/%s" % stream_id, params,
@ -97,9 +106,9 @@ class Streams(object):
if arguments.count: if arguments.count:
rows = self.get_streams_count(from_datetime=_from, to_datetime=_to, rows = self.get_streams_count(from_datetime=_from, to_datetime=_to,
name=_name, traits=trait_dict, name=_name, traits=trait_dict,
state=_state, debug=_debug) state=_state, debug=_debug)
print rows print(rows)
keys = ['count'] keys = ['count']
base.dump_response(keys, rows) base.dump_response(keys, rows)
return return
@ -121,17 +130,17 @@ class Streams(object):
for key, value in row['distinguishing_traits'].items(): for key, value in row['distinguishing_traits'].items():
x.add_row(["D.Trait", key, value]) x.add_row(["D.Trait", key, value])
print x print(x)
if 'events' in row.keys(): if 'events' in row.keys():
# This has detail rows ... handle those separately. # This has detail rows ... handle those separately.
print "Events:" print("Events:")
for event in row['events']: for event in row['events']:
x = prettytable.PrettyTable(["Property", "Value"]) x = prettytable.PrettyTable(["Property", "Value"])
sorted_keys = sorted(event.keys()) sorted_keys = sorted(event.keys())
for key in sorted_keys: for key in sorted_keys:
x.add_row([key, event[key]]) x.add_row([key, event[key]])
print x print(x)
class Events(object): class Events(object):
@ -139,22 +148,27 @@ class Events(object):
self.url = url self.url = url
if subparser: if subparser:
parser = subparser.add_parser('events') parser = subparser.add_parser('events')
parser.add_argument('--name', metavar='event_name', parser.add_argument(
help='Return events of type event_name.') '--name', metavar='event_name',
parser.add_argument('--from', metavar='datetime', dest='_from', help='Return events of type event_name.')
help='Return events generated before datetime') parser.add_argument(
parser.add_argument('--to', metavar='datetime', '--from', metavar='datetime', dest='_from',
help='Return events generated after datetime') help='Return events generated before datetime')
parser.add_argument('--traits', metavar='trait_list', parser.add_argument(
help='Return events with specific traits.') '--to', metavar='datetime',
help='Return events generated after datetime')
parser.add_argument(
'--traits', metavar='trait_list',
help='Return events with specific traits.')
group = parser.add_mutually_exclusive_group() group = parser.add_mutually_exclusive_group()
group.add_argument('--count', action='store_true', group.add_argument(
default=False, '--count', action='store_true',
help='Return a count of events matching filter criteria.') default=False,
group.add_argument('--msg_id', metavar='message_id', help='Return a count of events matching filter criteria.')
help='Return a single specific event by message id.') group.add_argument(
'--msg_id', metavar='message_id',
help='Return a single specific event by message id.')
def get_events_count(self, from_datetime=None, to_datetime=None, def get_events_count(self, from_datetime=None, to_datetime=None,
traits=None, name=None, debug=False): traits=None, name=None, debug=False):
@ -169,7 +183,7 @@ class Events(object):
return base.get(self.url, cmd, params, debug=debug) return base.get(self.url, cmd, params, debug=debug)
def get_events(self, from_datetime=None, to_datetime=None, def get_events(self, from_datetime=None, to_datetime=None,
traits=None, name=None, msg_id=None, debug=False): traits=None, name=None, msg_id=None, debug=False):
if msg_id: if msg_id:
return base.get(self.url, "events/%s" % msg_id, {}, debug=debug) return base.get(self.url, "events/%s" % msg_id, {}, debug=debug)
@ -232,7 +246,7 @@ class V1(object):
# This shouldn't be needed, but I'm being paranoid. # This shouldn't be needed, but I'm being paranoid.
if cmd not in self.resources: if cmd not in self.resources:
print "Unknown command: '%s'" % cmd print("Unknown command: '%s'" % cmd)
return return
self.resources[cmd]._cmdline(arguments) self.resources[cmd]._cmdline(arguments)

View File

@ -1,3 +1,4 @@
hacking>=0.10.0,<0.11
docopt docopt
prettytable prettytable
requests requests

11
tox.ini
View File

@ -1,5 +1,5 @@
[tox] [tox]
envlist = py26,py27 envlist = py26,py27,pep8
[testenv] [testenv]
deps = deps =
@ -8,3 +8,12 @@ deps =
mock mock
commands = nosetests -d -v --with-coverage --cover-inclusive --cover-package klugman [] commands = nosetests -d -v --with-coverage --cover-inclusive --cover-package klugman []
[testenv:pep8]
commands =
flake8
[flake8]
ignore =
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,*db/__init__.py,*db/migrations/versions/*_.py
show-source = True