Skip to content

Commit

Permalink
Making all modules in gcloud package pylint compliant.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Oct 3, 2014
1 parent 86798a4 commit 316a825
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
9 changes: 7 additions & 2 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Module to define base Connection class used by all APIs."""

import httplib2


Expand All @@ -18,15 +20,16 @@ class Connection(object):
"""A pointer to represent an empty value for default arguments."""

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 @@ -37,7 +40,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
6 changes: 5 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,7 @@ def run(self):

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

def wait(self):
def wait(self): # pylint: disable=no-self-use
raw_input()

@classmethod
Expand Down Expand Up @@ -106,4 +108,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
17 changes: 11 additions & 6 deletions gcloud/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# 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):
def _getTargetClass(self): # pylint: disable=invalid-name,no-self-use
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 +23,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,12 +36,15 @@ 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
65 changes: 38 additions & 27 deletions gcloud/test_credentials.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,75 @@
# 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):
def _getTargetClass(self): # pylint: disable=invalid-name,no-self-use
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 = 'phred@example.com'
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 = 'phred@example.com'
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

0 comments on commit 316a825

Please sign in to comment.