From bbe753971cf6ab09044fd948cc93677ed97f540d Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Mon, 8 Apr 2013 12:18:38 +0200 Subject: [PATCH] Fix pep8+hacking compliance. --- bin/swsync | 12 ++++++------ swsync/containers.py | 13 +++++++------ swsync/objects.py | 28 +++++++++++++++------------- swsync/utils.py | 4 ++-- tests/units/test_objects.py | 28 +++++++++++++++++++--------- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/bin/swsync b/bin/swsync index 1a9e9fb..697408e 100755 --- a/bin/swsync +++ b/bin/swsync @@ -15,11 +15,11 @@ # 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 sys import optparse +import sys import swsync.accounts -from swsync.utils import parse_ini, ConfigurationError, set_logging +import swsync.utils class Main(object): @@ -36,15 +36,15 @@ class Main(object): help='Number of containers to distribute objects among') self.options, args = parser.parse_args() if args: - conf = parse_ini(args[0]) + conf = swsync.utils.parse_ini(args[0]) else: try: - conf = parse_ini() - except(ConfigurationError): + conf = swsync.utils.parse_ini() + except(swsync.utils.ConfigurationError): parser.print_help() sys.exit(1) - set_logging(self.options.log_level.lower()) + swsync.utils.set_logging(self.options.log_level.lower()) #beurk swsync.utils.CONFIG = conf swsync.accounts.main() diff --git a/swsync/containers.py b/swsync/containers.py index 9357ffa..a0b832e 100644 --- a/swsync/containers.py +++ b/swsync/containers.py @@ -16,19 +16,20 @@ # under the License. import logging -import swiftclient import eventlet +import swiftclient -from swsync.objects import sync_object, delete_object -from swsync.utils import get_config +import swsync.objects +import swsync.utils class Containers(object): """Containers sync.""" def __init__(self): - self.max_gthreads = int(get_config("sync", "max_gthreads")) - self.sync_object = sync_object - self.delete_object = delete_object + self.max_gthreads = int(swsync.utils.get_config("sync", + "max_gthreads")) + self.sync_object = swsync.objects.sync_object + self.delete_object = swsync.objects.delete_object def delete_container(self, dest_storage_cnx, dest_token, orig_containers, diff --git a/swsync/objects.py b/swsync/objects.py index ef5cae1..e4a5ad9 100644 --- a/swsync/objects.py +++ b/swsync/objects.py @@ -14,22 +14,23 @@ # 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 eventlet +import swift.common.bufferedhttp +import swift.common.http +import swift.container.sync from swiftclient import client as swiftclient -from swift.common.bufferedhttp import http_connect_raw -from swift.common.http import is_success -from swift.container.sync import _Iter2FileLikeObject -from eventlet import Timeout +import urllib import urllib2 -from urllib import quote as urllib_quote def quote(value, safe='/'): - """ - Patched version of urllib.quote that encodes utf-8 strings before quoting + """Patched version of urllib.quote. + + Encodes utf-8 strings before quoting. """ if isinstance(value, unicode): value = value.encode('utf-8') - return urllib_quote(value, safe) + return urllib.quote(value, safe) def get_object(storage_url, token, @@ -43,8 +44,8 @@ def get_object(storage_url, token, path = x.path + '/' + container_name + '/' + object_name path = quote(path) - with Timeout(conn_timeout): - conn = http_connect_raw( + with eventlet.Timeout(conn_timeout): + conn = swift.common.bufferedhttp.http_connect_raw( x.hostname, x.port, 'GET', @@ -52,10 +53,10 @@ def get_object(storage_url, token, headers=headers, ssl=False) - with Timeout(response_timeout): + with eventlet.Timeout(response_timeout): resp = conn.getresponse() - if not is_success(resp.status): + if not swift.common.http.is_success(resp.status): resp.read() raise swiftclient.ClientException( 'status %s %s' % (resp.status, resp.reason)) @@ -103,6 +104,7 @@ def sync_object(orig_storage_url, orig_token, dest_storage_url, post_headers = orig_headers post_headers['x-auth-token'] = dest_token sync_to = dest_storage_url + "/" + container_name + iterlike = swift.container.sync._Iter2FileLikeObject swiftclient.put_object(sync_to, name=object_name, headers=post_headers, - contents=_Iter2FileLikeObject(orig_body)) + contents=iterlike(orig_body)) diff --git a/swsync/utils.py b/swsync/utils.py index 1205282..db50211 100644 --- a/swsync/utils.py +++ b/swsync/utils.py @@ -14,9 +14,9 @@ # 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 os import ConfigParser import logging +import os CONFIG = None @@ -61,7 +61,7 @@ def parse_ini(inicfg=None): def get_config(section, option, default=None, _config=None): - """Get section/option from ConfigParser or print default if specified""" + """Get section/option from ConfigParser or print default if specified.""" global CONFIG if _config: CONFIG = _config diff --git a/tests/units/test_objects.py b/tests/units/test_objects.py index 231bb98..c798028 100644 --- a/tests/units/test_objects.py +++ b/tests/units/test_objects.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. import swiftclient +import swift from eventlet import sleep, Timeout import base as test_base @@ -67,7 +68,8 @@ class TestObject(test_base.TestCase): def test_get_object_not_found(self): new_connect = fake_http_connect(404) - self.stubs.Set(swobjects, 'http_connect_raw', new_connect) + self.stubs.Set(swift.common.bufferedhttp, + 'http_connect_raw', new_connect) self.assertRaises(swiftclient.ClientException, swobjects.get_object, @@ -76,12 +78,14 @@ class TestObject(test_base.TestCase): def test_sync_object(self): body = ("X" * 3) * 1024 new_connect = fake_http_connect(200, body) - self.stubs.Set(swobjects, 'http_connect_raw', new_connect) + self.stubs.Set(swift.common.bufferedhttp, + 'http_connect_raw', new_connect) def put_object(url, name=None, headers=None, contents=None): self.assertEqual('obj1', name) self.assertIn('x-auth-token', headers) - self.assertIsInstance(contents, swobjects._Iter2FileLikeObject) + self.assertIsInstance(contents, + swift.container.sync._Iter2FileLikeObject) contents_read = contents.read() self.assertEqual(len(contents_read), len(body)) @@ -95,7 +99,8 @@ class TestObject(test_base.TestCase): utf_obj = "யாமறிந்த" body = "FOO" new_connect = fake_http_connect(200, body) - self.stubs.Set(swobjects, 'http_connect_raw', new_connect) + self.stubs.Set(swift.common.bufferedhttp, + 'http_connect_raw', new_connect) def put_object(url, name=None, headers=None, contents=None): # Container is Quoted @@ -114,7 +119,8 @@ class TestObject(test_base.TestCase): body = ("X" * expected_chunk_time) * chunk_size new_connect = fake_http_connect(200, body) - self.stubs.Set(swobjects, 'http_connect_raw', new_connect) + self.stubs.Set(swift.common.bufferedhttp, + 'http_connect_raw', new_connect) headers, gen = swobjects.get_object(self.orig_storage_url, "token", "cont1", "obj1", @@ -126,7 +132,8 @@ class TestObject(test_base.TestCase): def test_get_object_full(self): new_connect = fake_http_connect(200, body='foobar') - self.stubs.Set(swobjects, 'http_connect_raw', new_connect) + self.stubs.Set(swift.common.bufferedhttp, + 'http_connect_raw', new_connect) headers, body = swobjects.get_object(self.orig_storage_url, "token", "cont1", "obj1", @@ -136,7 +143,8 @@ class TestObject(test_base.TestCase): def test_get_headers(self): headers = {'X-FOO': 'BaR'}.items() new_connect = fake_http_connect(200, headers=headers) - self.stubs.Set(swobjects, 'http_connect_raw', new_connect) + self.stubs.Set(swift.common.bufferedhttp, + 'http_connect_raw', new_connect) headers, gen = swobjects.get_object(self.orig_storage_url, "token", @@ -147,7 +155,8 @@ class TestObject(test_base.TestCase): def test_get_object_over_conn_timeout(self): new_connect = fake_http_connect(200, connect_waitfor=2) - self.stubs.Set(swobjects, 'http_connect_raw', new_connect) + self.stubs.Set(swift.common.bufferedhttp, + 'http_connect_raw', new_connect) self.assertRaises(Timeout, swobjects.get_object, self.orig_storage_url, "token", "cont1", "obj1", @@ -155,7 +164,8 @@ class TestObject(test_base.TestCase): def test_get_object_over_resp_timeout(self): new_connect = fake_http_connect(200, resp_waitfor=2) - self.stubs.Set(swobjects, 'http_connect_raw', new_connect) + self.stubs.Set(swift.common.bufferedhttp, + 'http_connect_raw', new_connect) self.assertRaises(Timeout, swobjects.get_object, self.orig_storage_url, "token", "cont1", "obj1",