Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making all modules in gcloud package pylint compliant. #210

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Module to define base Connection class used by all APIs."""

from pkg_resources import get_distribution

import httplib2


Expand All @@ -18,19 +21,22 @@ class Connection(object):
_EMPTY = object()
"""A pointer to represent an empty value for default arguments."""

# pylint: disable=maybe-no-member
USER_AGENT = "gcloud-python/{0}".format(get_distribution('gcloud').version)
# pylint: enable=maybe-no-member
"""The user agent for gcloud-python requests."""

def __init__(self, credentials=None):
""":type credentials: :class:`gcloud.credentials.Credentials`
"""
:type credentials: :class:`gcloud.credentials.Credentials`
:param credentials: The OAuth2 Credentials to use for this connection.

"""

self._credentials = credentials

@property
def credentials(self):
"""Get the connection's credentials."""
return self._credentials

@property
Expand All @@ -41,7 +47,9 @@ def http(self):
:returns: A Http object used to transport data.
"""
if not hasattr(self, '_http'):
# pylint: disable=attribute-defined-outside-init
self._http = httplib2.Http()
if self._credentials:
self._http = self._credentials.authorize(self._http)
# pylint: enable=attribute-defined-outside-init
return self._http
2 changes: 1 addition & 1 deletion gcloud/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from oauth2client import client


class Credentials(object):
class Credentials(object): # pylint: disable=too-few-public-methods
"""An object used to simplify the OAuth2 credentials library.

.. note::
Expand Down
7 changes: 6 additions & 1 deletion gcloud/demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This is demo code, globally disable docstrings.
# pylint: disable=missing-docstring
from code import interact
import itertools
import os.path
Expand Down Expand Up @@ -34,7 +36,8 @@ def run(self):

interact('(Hit CTRL-D to exit...)', local=self.LOCALS)

def wait(self):
@staticmethod
def wait():
raw_input()

@classmethod
Expand Down Expand Up @@ -106,4 +109,6 @@ def _execute_lines(self, lines):
self.wait()

# Yes, this is crazy unsafe... but it's demo code.
# pylint: disable=exec-used
exec('\n'.join(lines), self.GLOBALS, self.LOCALS)
# pylint: enable=exec-used
25 changes: 17 additions & 8 deletions gcloud/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# This is test code, globally disable docstrings.
# pylint: disable=missing-docstring
import unittest2


class TestConnection(unittest2.TestCase):
class TestConnection(unittest2.TestCase): # pylint: disable=R0904

def _getTargetClass(self):
@staticmethod
def _getTargetClass(): # pylint: disable=invalid-name
from gcloud.connection import Connection
return Connection

def _makeOne(self, *args, **kw):
def _makeOne(self, *args, **kw): # pylint: disable=invalid-name
return self._getTargetClass()(*args, **kw)

def test_ctor_defaults(self):
Expand All @@ -21,7 +24,7 @@ def test_ctor_explicit(self):

def test_http_w_existing(self):
conn = self._makeOne()
conn._http = http = object()
conn._http = http = object() # pylint: disable=protected-access
self.assertTrue(conn.http is http)

def test_http_wo_creds(self):
Expand All @@ -34,18 +37,24 @@ def test_http_w_creds(self):

authorized = object()

class Creds(object):

class Creds(object): # pylint: disable=too-few-public-methods
def authorize(self, http):
# pylint: disable=attribute-defined-outside-init
self._called_with = http
# pylint: enable=attribute-defined-outside-init
return authorized
creds = Creds()
conn = self._makeOne(creds)
self.assertTrue(conn.http is authorized)
# pylint: disable=protected-access
self.assertTrue(isinstance(creds._called_with, Http))
# pylint: enable=protected-access

def test_user_agent_format(self):
from pkg_resources import get_distribution
expected_ua = 'gcloud-python/{0}'.format(get_distribution('gcloud').version)
conn = self._makeOne()
# pylint: disable=maybe-no-member
expected_ua = 'gcloud-python/{0}'.format(
get_distribution('gcloud').version)
# pylint: enable=maybe-no-member
conn = self._makeOne()
self.assertEqual(conn.USER_AGENT, expected_ua)
66 changes: 39 additions & 27 deletions gcloud/test_credentials.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,76 @@
# This is test code, globally disable docstrings.
# pylint: disable=missing-docstring
import unittest2


class TestCredentials(unittest2.TestCase):
class TestCredentials(unittest2.TestCase): # pylint: disable=R0904

def _getTargetClass(self):
@staticmethod
def _getTargetClass(): # pylint: disable=invalid-name
from gcloud.credentials import Credentials
return Credentials

def test_get_for_service_account_wo_scope(self):
def test_get_for_service_account_wo_scope(self): # pylint: disable=C0103
from tempfile import NamedTemporaryFile
from gcloud import credentials
# pylint: disable=invalid-name
CLIENT_EMAIL = '[email protected]'
PRIVATE_KEY = 'SEEkR1t'
# pylint: enable=invalid-name
cls = self._getTargetClass()
client = _Client()
with _Monkey(credentials, client=client):
with NamedTemporaryFile() as f:
f.write(PRIVATE_KEY)
f.flush()
found = cls.get_for_service_account(CLIENT_EMAIL, f.name)
with NamedTemporaryFile() as file_obj:
file_obj.write(PRIVATE_KEY)
file_obj.flush()
found = cls.get_for_service_account(CLIENT_EMAIL,
file_obj.name)
# pylint: disable=protected-access
self.assertTrue(found is client._signed)
self.assertEqual(client._called_with,
{'service_account_name': CLIENT_EMAIL,
'private_key': PRIVATE_KEY,
'scope': None,
})
expected_called_with = {'service_account_name': CLIENT_EMAIL,
'private_key': PRIVATE_KEY,
'scope': None}
self.assertEqual(client._called_with, expected_called_with)
# pylint: enable=protected-access

def test_get_for_service_account_w_scope(self):
def test_get_for_service_account_w_scope(self): # pylint: disable=C0103
from tempfile import NamedTemporaryFile
from gcloud import credentials
# pylint: disable=invalid-name
CLIENT_EMAIL = '[email protected]'
PRIVATE_KEY = 'SEEkR1t'
SCOPE = 'SCOPE'
# pylint: enable=invalid-name
cls = self._getTargetClass()
client = _Client()
with _Monkey(credentials, client=client):
with NamedTemporaryFile() as f:
f.write(PRIVATE_KEY)
f.flush()
found = cls.get_for_service_account(CLIENT_EMAIL, f.name,
SCOPE)
with NamedTemporaryFile() as file_obj:
file_obj.write(PRIVATE_KEY)
file_obj.flush()
found = cls.get_for_service_account(CLIENT_EMAIL,
file_obj.name, SCOPE)
# pylint: disable=protected-access
self.assertTrue(found is client._signed)
self.assertEqual(client._called_with,
{'service_account_name': CLIENT_EMAIL,
'private_key': PRIVATE_KEY,
'scope': SCOPE,
})
expected_called_with = {'service_account_name': CLIENT_EMAIL,
'private_key': PRIVATE_KEY,
'scope': SCOPE}
self.assertEqual(client._called_with, expected_called_with)
# pylint: enable=protected-access


class _Client(object):

class _Client(object): # pylint: disable=too-few-public-methods
def __init__(self):
self._signed = object()

def SignedJwtAssertionCredentials(self, **kw):
def SignedJwtAssertionCredentials(self, **kw): # pylint: disable=C0103
# pylint: disable=attribute-defined-outside-init
self._called_with = kw
# pylint: enable=attribute-defined-outside-init
return self._signed


class _Monkey(object):
class _Monkey(object): # pylint: disable=too-few-public-methods

# context-manager for replacing module names in the scope of a test.

def __init__(self, module, **kw):
Expand Down