Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
common modules: tidy up to meet PEP8 better
Browse files Browse the repository at this point in the history
  • Loading branch information
felixonmars committed Sep 14, 2014
1 parent d149a87 commit 6733d60
Show file tree
Hide file tree
Showing 25 changed files with 328 additions and 285 deletions.
20 changes: 8 additions & 12 deletions boto/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
from email.utils import formatdate
import hmac
import os
import sys
import time
import posixpath

from boto.compat import urllib, encodebytes
Expand Down Expand Up @@ -378,7 +376,7 @@ def canonical_uri(self, http_request):
path = http_request.auth_path
# Normalize the path
# in windows normpath('/') will be '\\' so we chane it back to '/'
normalized = posixpath.normpath(path).replace('\\','/')
normalized = posixpath.normpath(path).replace('\\', '/')
# Then urlencode whatever's left.
encoded = urllib.parse.quote(normalized)
if len(path) > 1 and path.endswith('/'):
Expand Down Expand Up @@ -474,7 +472,7 @@ def string_to_sign(self, http_request, canonical_request):
def signature(self, http_request, string_to_sign):
key = self._provider.secret_key
k_date = self._sign(('AWS4' + key).encode('utf-8'),
http_request.timestamp)
http_request.timestamp)
k_region = self._sign(k_date, http_request.region_name)
k_service = self._sign(k_region, http_request.service_name)
k_signing = self._sign(k_service, 'aws4_request')
Expand Down Expand Up @@ -570,7 +568,7 @@ def headers_to_sign(self, http_request):
# Hooray for the only difference! The main SigV4 signer only does
# ``Host`` + ``x-amz-*``. But S3 wants pretty much everything
# signed, except for authorization itself.
if not lname in ['authorization']:
if lname not in ['authorization']:
headers_to_sign[name] = value
return headers_to_sign

Expand Down Expand Up @@ -667,7 +665,7 @@ def payload(self, http_request):
return super(S3HmacAuthV4Handler, self).payload(http_request)

def add_auth(self, req, **kwargs):
if not 'x-amz-content-sha256' in req.headers:
if 'x-amz-content-sha256' not in req.headers:
if '_sha256' in req.headers:
req.headers['x-amz-content-sha256'] = req.headers.pop('_sha256')
else:
Expand Down Expand Up @@ -751,7 +749,6 @@ def _build_query_string(self, params):

def add_auth(self, http_request, **kwargs):
headers = http_request.headers
params = http_request.params
qs = self._build_query_string(
http_request.params
)
Expand Down Expand Up @@ -899,7 +896,7 @@ def add_auth(self, req, **kwargs):
# already be there, we need to get rid of that and rebuild it
req.path = req.path.split('?')[0]
req.path = (req.path + '?' + qs +
'&Signature=' + urllib.parse.quote_plus(signature))
'&Signature=' + urllib.parse.quote_plus(signature))


def get_auth_handler(host, config, provider, requested_capability=None):
Expand All @@ -925,7 +922,6 @@ def get_auth_handler(host, config, provider, requested_capability=None):
"""
ready_handlers = []
auth_handlers = boto.plugin.get_plugin(AuthHandler, requested_capability)
total_handlers = len(auth_handlers)
for handler in auth_handlers:
try:
ready_handlers.append(handler(host, config, provider))
Expand All @@ -936,9 +932,9 @@ def get_auth_handler(host, config, provider, requested_capability=None):
checked_handlers = auth_handlers
names = [handler.__name__ for handler in checked_handlers]
raise boto.exception.NoAuthHandlerFound(
'No handler was ready to authenticate. %d handlers were checked.'
' %s '
'Check your credentials' % (len(names), str(names)))
'No handler was ready to authenticate. %d handlers were checked.'
' %s '
'Check your credentials' % (len(names), str(names)))

# We select the last ready auth handler that was loaded, to allow users to
# customize how auth works in environments where there are shared boto
Expand Down
10 changes: 6 additions & 4 deletions boto/auth_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
Expand All @@ -25,8 +25,10 @@

from boto.plugin import Plugin


class NotReadyToAuthenticate(Exception):
pass
pass


class AuthHandler(Plugin):

Expand All @@ -37,10 +39,10 @@ def __init__(self, host, config, provider):
:type host: string
:param host: The host to which the request is being sent.
:type config: boto.pyami.Config
:type config: boto.pyami.Config
:param config: Boto configuration.
:type provider: boto.provider.Provider
:type provider: boto.provider.Provider
:param provider: Provider details.
Raises:
Expand Down
78 changes: 39 additions & 39 deletions boto/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
PORTS_BY_SECURITY = {True: 443,
False: 80}

DEFAULT_CA_CERTS_FILE = os.path.join(os.path.dirname(os.path.abspath(boto.cacerts.__file__ )), "cacerts.txt")
DEFAULT_CA_CERTS_FILE = os.path.join(os.path.dirname(os.path.abspath(boto.cacerts.__file__)), "cacerts.txt")


class HostConnectionPool(object):
Expand Down Expand Up @@ -485,13 +485,13 @@ def __init__(self, host, aws_access_key_id=None,
validate_certs)
if self.https_validate_certificates and not HAVE_HTTPS_CONNECTION:
raise BotoClientError(
"SSL server certificate validation is enabled in boto "
"configuration, but Python dependencies required to "
"support this feature are not available. Certificate "
"validation is only supported when running under Python "
"2.6 or later.")
"SSL server certificate validation is enabled in boto "
"configuration, but Python dependencies required to "
"support this feature are not available. Certificate "
"validation is only supported when running under Python "
"2.6 or later.")
certs_file = config.get_value(
'Boto', 'ca_certificates_file', DEFAULT_CA_CERTS_FILE)
'Boto', 'ca_certificates_file', DEFAULT_CA_CERTS_FILE)
if certs_file == 'system':
certs_file = None
self.ca_certificates_file = certs_file
Expand All @@ -508,7 +508,7 @@ def __init__(self, host, aws_access_key_id=None,
self.http_unretryable_exceptions = []
if HAVE_HTTPS_CONNECTION:
self.http_unretryable_exceptions.append(
https_connection.InvalidCertificateException)
https_connection.InvalidCertificateException)

# define values in socket exceptions we don't want to catch
self.socket_exception_values = (errno.EINTR,)
Expand Down Expand Up @@ -565,7 +565,7 @@ def __init__(self, host, aws_access_key_id=None,
self._connection = (self.host, self.port, self.is_secure)
self._last_rs = None
self._auth_handler = auth.get_auth_handler(
host, config, self.provider, self._required_auth_capability())
host, config, self.provider, self._required_auth_capability())
if getattr(self, 'AuthServiceName', None) is not None:
self.auth_service_name = self.AuthServiceName
self.request_hook = None
Expand Down Expand Up @@ -667,9 +667,9 @@ def handle_proxy(self, proxy, proxy_port, proxy_user, proxy_pass):
self.proxy_pass = proxy_pass
if 'http_proxy' in os.environ and not self.proxy:
pattern = re.compile(
'(?:http://)?' \
'(?:(?P<user>[\w\-\.]+):(?P<pass>.*)@)?' \
'(?P<host>[\w\-\.]+)' \
'(?:http://)?'
'(?:(?P<user>[\w\-\.]+):(?P<pass>.*)@)?'
'(?P<host>[\w\-\.]+)'
'(?::(?P<port>\d+))?'
)
match = pattern.match(os.environ['http_proxy'])
Expand All @@ -689,8 +689,8 @@ def handle_proxy(self, proxy, proxy_port, proxy_user, proxy_pass):
self.proxy_pass = config.get_value('Boto', 'proxy_pass', None)

if not self.proxy_port and self.proxy:
print("http_proxy environment variable does not specify " \
"a port, using default")
print("http_proxy environment variable does not specify "
"a port, using default")
self.proxy_port = self.port

self.no_proxy = os.environ.get('no_proxy', '') or os.environ.get('NO_PROXY', '')
Expand Down Expand Up @@ -740,30 +740,30 @@ def new_http_connection(self, host, port, is_secure):

if is_secure:
boto.log.debug(
'establishing HTTPS connection: host=%s, kwargs=%s',
host, http_connection_kwargs)
'establishing HTTPS connection: host=%s, kwargs=%s',
host, http_connection_kwargs)
if self.use_proxy and not self.skip_proxy(host):
connection = self.proxy_ssl(host, is_secure and 443 or 80)
elif self.https_connection_factory:
connection = self.https_connection_factory(host)
elif self.https_validate_certificates and HAVE_HTTPS_CONNECTION:
connection = https_connection.CertValidatingHTTPSConnection(
host, ca_certs=self.ca_certificates_file,
**http_connection_kwargs)
host, ca_certs=self.ca_certificates_file,
**http_connection_kwargs)
else:
connection = http_client.HTTPSConnection(host,
**http_connection_kwargs)
connection = http_client.HTTPSConnection(
host, **http_connection_kwargs)
else:
boto.log.debug('establishing HTTP connection: kwargs=%s' %
http_connection_kwargs)
http_connection_kwargs)
if self.https_connection_factory:
# even though the factory says https, this is too handy
# to not be able to allow overriding for http also.
connection = self.https_connection_factory(host,
**http_connection_kwargs)
connection = self.https_connection_factory(
host, **http_connection_kwargs)
else:
connection = http_client.HTTPConnection(host,
**http_connection_kwargs)
connection = http_client.HTTPConnection(
host, **http_connection_kwargs)
if self.debug > 1:
connection.set_debuglevel(self.debug)
# self.connection must be maintained for backwards-compatibility
Expand Down Expand Up @@ -822,7 +822,7 @@ def proxy_ssl(self, host=None, port=None):
if self.https_validate_certificates and HAVE_HTTPS_CONNECTION:
msg = "wrapping ssl socket for proxied connection; "
if self.ca_certificates_file:
msg += "CA certificate file=%s" %self.ca_certificates_file
msg += "CA certificate file=%s" % self.ca_certificates_file
else:
msg += "using system provided SSL certs"
boto.log.debug(msg)
Expand All @@ -836,7 +836,7 @@ def proxy_ssl(self, host=None, port=None):
hostname = self.host.split(':', 0)[0]
if not https_connection.ValidateCertificateHostname(cert, hostname):
raise https_connection.InvalidCertificateException(
hostname, cert, 'hostname mismatch')
hostname, cert, 'hostname mismatch')
else:
# Fallback for old Python without ssl.wrap_socket
if hasattr(http_client, 'ssl'):
Expand Down Expand Up @@ -1008,8 +1008,8 @@ def _mexe(self, request, sender=None, override_num_retries=None,
'encountered unretryable %s exception, re-raising' %
e.__class__.__name__)
raise
boto.log.debug('encountered %s exception, reconnecting' % \
e.__class__.__name__)
boto.log.debug('encountered %s exception, reconnecting' %
e.__class__.__name__)
connection = self.new_http_connection(request.host, request.port,
self.is_secure)
time.sleep(next_sleep)
Expand Down Expand Up @@ -1041,8 +1041,7 @@ def build_base_http_request(self, method, path, auth_path,
headers = {}
else:
headers = headers.copy()
if (self.host_header and
not boto.utils.find_matching_headers('host', headers)):
if self.host_header and not boto.utils.find_matching_headers('host', headers):
headers['host'] = self.host_header
host = host or self.host
if self.use_proxy:
Expand Down Expand Up @@ -1085,14 +1084,15 @@ def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
proxy_user=None, proxy_pass=None, host=None, debug=0,
https_connection_factory=None, path='/', security_token=None,
validate_certs=True, profile_name=None):
super(AWSQueryConnection, self).__init__(host, aws_access_key_id,
aws_secret_access_key,
is_secure, port, proxy,
proxy_port, proxy_user, proxy_pass,
debug, https_connection_factory, path,
security_token=security_token,
validate_certs=validate_certs,
profile_name=profile_name)
super(AWSQueryConnection, self).__init__(
host, aws_access_key_id,
aws_secret_access_key,
is_secure, port, proxy,
proxy_port, proxy_user, proxy_pass,
debug, https_connection_factory, path,
security_token=security_token,
validate_certs=validate_certs,
profile_name=profile_name)

def _required_auth_capability(self):
return []
Expand Down
3 changes: 1 addition & 2 deletions boto/contrib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#

1 change: 1 addition & 0 deletions boto/contrib/ymlmessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from boto.sqs.message import Message
import yaml


class YAMLMessage(Message):
"""
The YAMLMessage class provides a YAML compatible message. Encoding and
Expand Down
Loading

0 comments on commit 6733d60

Please sign in to comment.