From 6b6fe59bc06614899f73026a1a25032f4c9e3b07 Mon Sep 17 00:00:00 2001 From: kgriffs Date: Wed, 20 Mar 2013 17:02:20 -0400 Subject: [PATCH] style: Enable pep8 warnings/errors and fix ensuing chaos Change-Id: I8dc2ccc148357019d822bc7e747608abc713e9e0 --- marconi/common/config.py | 10 ++-- marconi/storage/sqlite/controllers.py | 82 +++++++++++++++++---------- marconi/storage/sqlite/driver.py | 2 +- marconi/tests/test_config.py | 4 +- marconi/tests/test_sqlite.py | 30 +++++----- tools/run_pep8.sh | 3 +- tox.ini | 2 - 7 files changed, 78 insertions(+), 55 deletions(-) diff --git a/marconi/common/config.py b/marconi/common/config.py index 5b9bda1e1..d08cb7d87 100644 --- a/marconi/common/config.py +++ b/marconi/common/config.py @@ -168,12 +168,12 @@ def _make_opt(name, default): """ deduction = { - str: cfg.StrOpt, - bool: cfg.BoolOpt, - int: cfg.IntOpt, - long: cfg.IntOpt, + str: cfg.StrOpt, + bool: cfg.BoolOpt, + int: cfg.IntOpt, + long: cfg.IntOpt, float: cfg.FloatOpt, - list: cfg.ListOpt, + list: cfg.ListOpt, } if type(default) is tuple: diff --git a/marconi/storage/sqlite/controllers.py b/marconi/storage/sqlite/controllers.py index 0d5839d87..11a2baf97 100644 --- a/marconi/storage/sqlite/controllers.py +++ b/marconi/storage/sqlite/controllers.py @@ -37,39 +37,51 @@ class Queue(base.QueueBase): def list(self, tenant): records = self.driver._run('''select name, metadata from Queues where - tenant = ?''', tenant) + tenant = ?''', tenant) + for k, v in records: yield {'name': k, 'metadata': v} def get(self, name, tenant): + sql = '''select metadata from Queues where + tenant = ? and name = ?''' + try: - return json.loads( - self.driver._get('''select metadata from Queues where - tenant = ? and name = ?''', tenant, name)[0]) + return json.loads(self.driver._get(sql, tenant, name)[0]) except TypeError: msg = (_("Queue %(name)s does not exist for tenant %(tenant)s") - % dict(name=name, tenant=tenant)) + % dict(name=name, tenant=tenant)) + raise exceptions.DoesNotExist(msg) def upsert(self, name, metadata, tenant): with self.driver: - rc = self.driver._get('''select metadata from Queues where - tenant = ? and name = ?''', tenant, name) is None - self.driver._run('''replace into Queues values - (null, ?, ?, ?)''', tenant, name, - json.dumps(metadata, ensure_ascii=False)) - return rc + sql_select = '''select metadata from Queues where + tenant = ? and name = ?''' + previous_record = self.driver._get(sql_select, tenant, name) + + sql_replace = '''replace into Queues + values (null, ?, ?, ?)''' + doc = json.dumps(metadata, ensure_ascii=False) + self.driver._run(sql_replace, tenant, name, doc) + + return previous_record is None def delete(self, name, tenant): self.driver._run('''delete from Queues where - tenant = ? and name = ?''', tenant, name) + tenant = ? and name = ?''', + tenant, name) def stats(self, name, tenant): - return {'messages': self.driver._get('''select count(id) - from Messages where - qid = (select id from Queues where - tenant = ? and name = ?)''', tenant, name)[0], - 'actions': 0} + sql = '''select count(id) + from Messages where + qid = (select id from Queues where + tenant = ? and name = ?)''' + + return { + 'messages': self.driver._get(sql, tenant, name)[0], + 'actions': 0, + } def actions(self, name, tenant, marker=None, limit=10): pass @@ -78,15 +90,17 @@ class Queue(base.QueueBase): class Message(base.MessageBase): def __init__(self, driver): self.driver = driver - self.driver._run('''create table if not exists Messages ( - id INTEGER, - qid INTEGER, - ttl INTEGER, - content TEXT, - created DATETIME, - PRIMARY KEY(id), - FOREIGN KEY(qid) references Queues(id) on delete cascade - )''') + self.driver._run(''' + create table if not exists Messages ( + id INTEGER, + qid INTEGER, + ttl INTEGER, + content TEXT, + created DATETIME, + PRIMARY KEY(id), + FOREIGN KEY(qid) references Queues(id) on delete cascade + ) + ''') def get(self, queue, tenant=None, message_id=None, marker=None, echo=False, client_uuid=None): @@ -96,28 +110,34 @@ class Message(base.MessageBase): with self.driver: try: qid, = self.driver._get('''select id from Queues where - tenant = ? and name = ?''', tenant, queue) + tenant = ? and name = ?''', + tenant, queue) except TypeError: msg = (_("Queue %(name)s does not exist for tenant %(tenant)s") - % dict(name=queue, tenant=tenant)) + % dict(name=queue, tenant=tenant)) + raise exceptions.DoesNotExist(msg) # executemany() sets lastrowid to None, so no matter we manually # generate the IDs or not, we still need to query for it. try: unused, = self.driver._get('''select id + 1 from Messages - where id = (select max(id) from Messages)''') + where id = (select max(id) + from Messages)''') except TypeError: unused, = 1001, def it(newid): for m in messages: yield (newid, qid, m['ttl'], - json.dumps(m, ensure_ascii=False)) + json.dumps(m, ensure_ascii=False)) + newid += 1 self.driver._run_multiple('''insert into Messages values - (?, ?, ?, ?, datetime())''', it(unused)) + (?, ?, ?, ?, datetime())''', + it(unused)) + return [str(x) for x in range(unused, unused + len(messages))] def delete(self, queue, message_id, tenant=None, claim=None): diff --git a/marconi/storage/sqlite/driver.py b/marconi/storage/sqlite/driver.py index b95036e96..160f1bcdb 100644 --- a/marconi/storage/sqlite/driver.py +++ b/marconi/storage/sqlite/driver.py @@ -22,7 +22,7 @@ from marconi.storage.sqlite import controllers cfg = config.namespace('drivers:storage:sqlite').from_options( - database=':memory:') + database=':memory:') class Driver(storage.DriverBase): diff --git a/marconi/tests/test_config.py b/marconi/tests/test_config.py index a4d56902a..5e070f492 100644 --- a/marconi/tests/test_config.py +++ b/marconi/tests/test_config.py @@ -21,8 +21,8 @@ from marconi.tests import util as testing cfg_handle = config.project() cfg = cfg_handle.from_options( - without_help=3, - with_help=(None, "nonsense")) + without_help=3, + with_help=(None, "nonsense")) class TestConfig(testing.TestBase): diff --git a/marconi/tests/test_sqlite.py b/marconi/tests/test_sqlite.py index 339ee747f..f455eda30 100644 --- a/marconi/tests/test_sqlite.py +++ b/marconi/tests/test_sqlite.py @@ -1,13 +1,13 @@ # Copyright (c) 2013 Rackspace, Inc. # -# Licensed under the Apache License, Version 2.0 (the "License"); +# 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, +# 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 @@ -28,21 +28,25 @@ class TestSqlite(testing.TestBase): q = storage.queue_controller q.upsert('fizbit', {'_message_ttl': 40}, '480924') m = storage.message_controller + d = [ - {"body": { - "event": "BackupStarted", - "backupId": "c378813c-3f0b-11e2-ad92-7823d2b0f3ce" + { + 'body': { + 'event': 'BackupStarted', + 'backupId': 'c378813c-3f0b-11e2-ad92-7823d2b0f3ce', }, - 'ttl': 30 - }, - {"body": { - "event": "BackupProgress", - "currentBytes": "0", - "totalBytes": "99614720" + 'ttl': 30, + }, + { + 'body': { + 'event': 'BackupProgress', + 'currentBytes': '0', + 'totalBytes': '99614720', }, 'ttl': 10 - } - ] + }, + ] + n = q.stats('fizbit', '480924')['messages'] l1 = m.post('fizbit', d, '480924') l2 = m.post('fizbit', d, '480924') diff --git a/tools/run_pep8.sh b/tools/run_pep8.sh index 77a4abaeb..96e42eb8a 100755 --- a/tools/run_pep8.sh +++ b/tools/run_pep8.sh @@ -5,9 +5,10 @@ set -e python tools/hacking.py --doctest # Until all these issues get fixed, ignore. -PEP8='python tools/hacking.py --ignore=E12,E711,E721,E712,N303,N403,N404' +PEP8='python tools/hacking.py --ignore=N404' EXCLUDE='--exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*' +EXCLUDE+=',./tools' EXCLUDE+=',*egg,build' ${PEP8} ${EXCLUDE} . diff --git a/tox.ini b/tox.ini index fc04467e5..44cf61a61 100644 --- a/tox.ini +++ b/tox.ini @@ -19,8 +19,6 @@ downloadcache = ~/cache/pip [testenv:pep8] # deps = pep8==1.3.3 -# commands = -# pep8 --ignore=E125,E126,E711,E712 --repeat --show-source --exclude=.venv,.tox,dist,doc,openstack . commands = {toxinidir}/tools/run_pep8.sh [testenv:cover]