diff --git a/.gitignore b/.gitignore index 51cbe85..e26c186 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,7 @@ coverage.xml # Sphinx documentation docs/_build/ +# IDE Project Files +*.project +*.pydev* +*.idea diff --git a/quincy/api.py b/quincy/api.py index 3cdb008..d7963d7 100644 --- a/quincy/api.py +++ b/quincy/api.py @@ -16,7 +16,6 @@ import ConfigParser import traceback - import falcon import simport @@ -49,7 +48,7 @@ def _initialize(enabled_versions, implementation_map): if not impl: raise NotImplemented("No implementation available for Quincy" " version %d" % version) - print "Version %d using %s" % (version, impl) + print("Version %d using %s" % (version, impl)) routes.append(klass(version, api, impl)) # TODO(sandy): We need to create the top-level /v1, ... /vN @@ -58,7 +57,8 @@ def _initialize(enabled_versions, implementation_map): def _get_api(config_location=None): - print "Using config_location=%s (None means default impl)" % config_location + print("Using config_location=%s (None means default impl)" + % config_location) # The default implementation is internal and works with # a fake/static set of data. @@ -77,8 +77,8 @@ def _get_api(config_location=None): config = ConfigParser.ConfigParser() config.read(config_location) enabled_versions = [int(x) for x in - config.get('global', 'enabled_versions') - .split(',')] + config.get('global', + 'enabled_versions').split(',')] # Rather than every implementation duplicate resources, the # scratchpad is a shared storage area all the implementations @@ -86,7 +86,7 @@ def _get_api(config_location=None): scratchpad = {} impl_map = {} _load_implementations(impl_map, enabled_versions, local_config, - scratchpad) + scratchpad) if config_location: # Overlay the impl_map with the implementations @@ -94,13 +94,12 @@ def _get_api(config_location=None): _load_implementations(impl_map, enabled_versions, config, scratchpad) - return _initialize(enabled_versions, impl_map) def get_api(config_location=None): try: return _get_api(config_location) - except Exception as e: - print "Error getting API:", traceback.format_exc() + except Exception: + print("Error getting API:", traceback.format_exc()) return None diff --git a/quincy/jsonutil.py b/quincy/jsonutil.py index 7894000..6037d03 100644 --- a/quincy/jsonutil.py +++ b/quincy/jsonutil.py @@ -1,10 +1,24 @@ -import json +# Copyright (c) 2014 Dark Secret Software Inc. +# +# 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 datetime +import json import timex class ObjectEncoder(json.JSONEncoder): - ENCODE_MAP = {datetime.datetime: "datetime", timex.TimeRange: "timex.TimeRange", timex.Timestamp: "timex.Timestamp"} @@ -12,20 +26,21 @@ class ObjectEncoder(json.JSONEncoder): def default(self, obj): if type(obj) in self.ENCODE_MAP: typename = self.ENCODE_MAP[type(obj)] - encoder = getattr(self, '_encode_' + typename.replace('.', '_').lower()) + 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, + return {'__type__': name, 'datetime': obj.isoformat()} def _encode_timex_timestamp(self, obj, name): - return {'__type__' : name, + return {'__type__': name, 'timestamp': obj.timestamp.isoformat()} def _encode_timex_timerange(self, obj, name): - return {'__type__' : name, + return {'__type__': name, 'begin': obj.begin.isoformat(), 'end': obj.end.isoformat()} diff --git a/quincy/v1_api.py b/quincy/v1_api.py index 99c6f45..a886445 100644 --- a/quincy/v1_api.py +++ b/quincy/v1_api.py @@ -19,7 +19,7 @@ from dateutil import parser import common -#set this to something reasonable. +# set this to something reasonable. DEFAULT_LIMIT = 200 @@ -58,12 +58,12 @@ def _find_streams(impl, req, resp, count=False): younger_than = parser.parse(younger_than) return impl.find_streams(count=count, - older_than=older_than, - younger_than=younger_than, - state=state, - trigger_name=trigger, - distinguishing_traits=traits, - mark=mark, limit=limit) + older_than=older_than, + younger_than=younger_than, + state=state, + trigger_name=trigger, + distinguishing_traits=traits, + mark=mark, limit=limit) def _get_stream(impl, req, resp, stream_id): @@ -86,7 +86,7 @@ class StreamCollection(common.FalconBase): # # Actions on a Stream: # details - get full details on stream (including events & - # distriquishing traits) + # distinguishing traits) def on_get(self, req, resp): streams = _find_streams(self.impl, req, resp) resp.body = jsonutil.dumps(streams) @@ -151,7 +151,7 @@ class EventCollection(common.FalconBase): # Qualifiers: # datetime are ISO-8601 format, UTC # from_datetime - events with timestamp > from_datetime - # default: now - 1hr + # default: now - 1hr # to_datetime - events with timestamp < to_datetime # default: now # event_name - events of event type @@ -199,6 +199,6 @@ class Schema(object): self.stream_item) self.api.add_route('%s/streams' % self._v(), self.stream_collection) - self.api.add_route('%s/events/{message_id}' % self._v(), self.event_item) + self.api.add_route('%s/events/{message_id}' % self._v(), + self.event_item) self.api.add_route('%s/events' % self._v(), self.event_collection) - diff --git a/quincy/v1_impl.py b/quincy/v1_impl.py index e94b8b0..caa68d4 100644 --- a/quincy/v1_impl.py +++ b/quincy/v1_impl.py @@ -95,15 +95,17 @@ class Event(object): elif dtype == 2: d[t] = { "__type__": "timex.TimeRange", - "begin": str(datetime.datetime.utcnow() - - datetime.timedelta(minutes=random.randrange(500))), + "begin": str( + datetime.datetime.utcnow() - datetime.timedelta( + minutes=random.randrange(500))), "end": str(datetime.datetime.utcnow()) } elif dtype == 3: d[t] = { "__type__": "datetime", - "datetime": str(datetime.datetime.utcnow() - - datetime.timedelta(minutes=random.randrange(500))) + "datetime": str( + datetime.datetime.utcnow() - datetime.timedelta( + minutes=random.randrange(500))) } d.update({ @@ -151,7 +153,7 @@ class Impl(object): expiry = None if state != 'completed': finish = start + datetime.timedelta( - minutes=max_duration_minutes) + minutes=max_duration_minutes) if random.randrange(2) == 0: expiry = finish else: @@ -175,13 +177,15 @@ class Impl(object): for event_id in range(100): name = random.choice(event_names) now = (datetime.datetime.utcnow() - datetime.timedelta( - minutes=random.randrange(minutes_in_48_hrs))) + minutes=random.randrange(minutes_in_48_hrs))) self.events.append(Event(event_id + 100, name, now)) return self.events def find_streams(self, **kwargs): - """kwargs may be: + """Find streams + + kwargs may be: count: True/False older_than younger_than diff --git a/quincy/v2_impl.py b/quincy/v2_impl.py index 93e5428..b931f62 100644 --- a/quincy/v2_impl.py +++ b/quincy/v2_impl.py @@ -37,5 +37,5 @@ class Impl(v1_impl.Impl): for _ in range(4): ret.append(Archive(str(uuid.uuid4()), now.strftime(filename_template))) - now += datetime.timedelta(hours = 1) + now += datetime.timedelta(hours=1) return ret diff --git a/requirements.txt b/requirements.txt index 03136a1..f0adfac 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +hacking>=0.10.0,<0.11 python-dateutil falcon simport >= 0.0.dev0 diff --git a/tox.ini b/tox.ini index ee14a0b..e0fd356 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py26,py27 +envlist = py26,py27,pep8 [testenv] deps = @@ -10,3 +10,12 @@ deps = simport commands = nosetests -d -v --with-coverage --cover-inclusive --cover-package quincy [] + +[testenv:pep8] +commands = + flake8 + +[flake8] +ignore = +exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg +show-source = True