Fix pep8+hacking compliance.

This commit is contained in:
Chmouel Boudjnah 2013-04-08 12:18:38 +02:00
parent cfb94a6bb9
commit bbe753971c
5 changed files with 49 additions and 36 deletions

View File

@ -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()

View File

@ -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,

View File

@ -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))

View File

@ -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

View File

@ -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",