Skip to content

Commit f2e59d0

Browse files
committed
Adding pylintrc config and custom runner and making it pass.
Some key things to notice: - The tools (pep8 / pylint) disagree about continutation lines. This is because PEP8 is ambiguous about "multi-line constructs". One of the examples for bracket indentation in the PEP is my_list = [ 1, 2, 3, 4, 5, 6, ] but the PEP doesn't explicity say whether or not my_list = [1, 2, 3, 4, 5, 6, ] would be allowed. As it turns out, pep8 (the tool) considers the latter to be a continuation while pylint does not. - I changed all indentation where this was a problem, but was intentionally inconsistent. This way we can see "all options" and discuss whichever is preferred. - I made a _MetadataMixin helper class for methods shared between storage.Bucket and storage.Key. I did not refactor the affected tests but they do still give 100% coverage. - To silence docstrings warnings I added many bogus docstrings without actually documenting the given objects. - I factored out part of storage.connection.Connection.generate_signed_url into a _get_expiration_seconds() method and added tests. This method is untested (has pragma no cover) and is still very long. I refactored because pylint complained about too many locals. - I made sure tox -e lint and tox -e cover passed. - I increased the maximum number of arguments from 5 to 10 and the maximum public methods from 20 to 30. Offenders were: - storage.connection.Connection.make_request (6 arguments) - storage.connection.Connection.api_request (9 arguments) - storage.connection.Connection.generate_signed_url (6 arguments) - storage.bucket.Bucket (27 public methods) and - storage.key.Key (23 public methods) I think refactoring these should be considered (though is not mandatory). I certainly don't know a good solution for the class public methods.
1 parent dfcc90b commit f2e59d0

38 files changed

+1088
-897
lines changed

gcloud/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
"""Totally unnecessary docstring."""
2+
3+
14
__version__ = '0.02.2'

gcloud/connection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Need module docstring."""
2+
13
from pkg_resources import get_distribution
24

35
import httplib2
@@ -27,10 +29,12 @@ def __init__(self, credentials=None):
2729
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
2830
:param credentials: The OAuth2 Credentials to use for this connection.
2931
"""
32+
self._http = None
3033
self._credentials = credentials
3134

3235
@property
3336
def credentials(self):
37+
"""Needs docstring."""
3438
return self._credentials
3539

3640
@property
@@ -40,7 +44,7 @@ def http(self):
4044
:rtype: :class:`httplib2.Http`
4145
:returns: A Http object used to transport data.
4246
"""
43-
if not hasattr(self, '_http'):
47+
if self._http is None:
4448
self._http = httplib2.Http()
4549
if self._credentials:
4650
self._http = self._credentials.authorize(self._http)

gcloud/datastore/_helpers.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,32 @@ def _get_value_from_protobuf(pb):
8888
:returns: The value provided by the Protobuf.
8989
"""
9090

91+
result = None
9192
if pb.value.HasField('timestamp_microseconds_value'):
9293
microseconds = pb.value.timestamp_microseconds_value
9394
naive = (datetime.utcfromtimestamp(0) +
9495
timedelta(microseconds=microseconds))
95-
return naive.replace(tzinfo=pytz.utc)
96+
result = naive.replace(tzinfo=pytz.utc)
9697

9798
elif pb.value.HasField('key_value'):
98-
return Key.from_protobuf(pb.value.key_value)
99+
result = Key.from_protobuf(pb.value.key_value)
99100

100101
elif pb.value.HasField('boolean_value'):
101-
return pb.value.boolean_value
102+
result = pb.value.boolean_value
102103

103104
elif pb.value.HasField('double_value'):
104-
return pb.value.double_value
105+
result = pb.value.double_value
105106

106107
elif pb.value.HasField('integer_value'):
107-
return pb.value.integer_value
108+
result = pb.value.integer_value
108109

109110
elif pb.value.HasField('string_value'):
110-
return pb.value.string_value
111+
result = pb.value.string_value
111112

112113
elif pb.value.HasField('entity_value'):
113-
return Entity.from_protobuf(pb.value.entity_value)
114+
result = Entity.from_protobuf(pb.value.entity_value)
114115

115-
else:
116-
return None
116+
return result
117117

118118

119119
def _set_protobuf_value(value_pb, val):
@@ -142,9 +142,9 @@ def _set_protobuf_value(value_pb, val):
142142
key = val.key()
143143
if key is not None:
144144
e_pb.key.CopyFrom(key.to_protobuf())
145-
for k, v in val.items():
145+
for item_key, value in val.iteritems():
146146
p_pb = e_pb.property.add()
147-
p_pb.name = k
148-
_set_protobuf_value(p_pb.value, v)
147+
p_pb.name = item_key
148+
_set_protobuf_value(p_pb.value, value)
149149
else: # scalar, just assign
150150
setattr(value_pb, attr, val)

gcloud/datastore/connection.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Needs module docstring."""
2+
13
from gcloud import connection
24
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
35
from gcloud.datastore import _helpers
@@ -22,7 +24,7 @@ class Connection(connection.Connection):
2224
"""A template for the URL of a particular API call."""
2325

2426
def __init__(self, credentials=None):
25-
self._credentials = credentials
27+
super(Connection, self).__init__(credentials=credentials)
2628
self._current_transaction = None
2729

2830
def _request(self, dataset_id, method, data):
@@ -59,6 +61,7 @@ def _request(self, dataset_id, method, data):
5961
return content
6062

6163
def _rpc(self, dataset_id, method, request_pb, response_pb_cls):
64+
"""Needs docstring."""
6265
response = self._request(dataset_id=dataset_id, method=method,
6366
data=request_pb.SerializeToString())
6467
return response_pb_cls.FromString(response)
@@ -94,13 +97,15 @@ def build_api_url(cls, dataset_id, method, base_url=None,
9497
dataset_id=dataset_id, method=method)
9598

9699
def transaction(self, transaction=connection.Connection._EMPTY):
100+
"""Needs docstring."""
97101
if transaction is self._EMPTY:
98102
return self._current_transaction
99103
else:
100104
self._current_transaction = transaction
101105
return self
102106

103107
def mutation(self):
108+
"""Needs docstring."""
104109
if self.transaction():
105110
return self.transaction().mutation()
106111
else:
@@ -210,8 +215,7 @@ def run_query(self, dataset_id, query_pb, namespace=None):
210215
return ([e.entity for e in response.batch.entity_result],
211216
response.batch.end_cursor,
212217
response.batch.more_results,
213-
response.batch.skipped_results,
214-
)
218+
response.batch.skipped_results)
215219

216220
def lookup(self, dataset_id, key_pbs):
217221
"""Lookup keys from a dataset in the Cloud Datastore.
@@ -278,6 +282,7 @@ def lookup(self, dataset_id, key_pbs):
278282
return results
279283

280284
def commit(self, dataset_id, mutation_pb):
285+
"""Needs docstring."""
281286
request = datastore_pb.CommitRequest()
282287

283288
if self.transaction():
@@ -365,4 +370,5 @@ def delete_entities(self, dataset_id, key_pbs):
365370
return self.commit(dataset_id, mutation)
366371

367372
def delete_entity(self, dataset_id, key_pb):
373+
"""Needs docstring."""
368374
return self.delete_entities(dataset_id, [key_pb])

gcloud/datastore/dataset.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""Needs module docstring."""
2+
3+
14
class Dataset(object):
25
"""A dataset in the Cloud Datastore.
36
@@ -58,15 +61,21 @@ def id(self):
5861
return self._id
5962

6063
def query(self, *args, **kwargs):
64+
"""Needs docstring."""
65+
# This import is here to avoid circular references.
6166
from gcloud.datastore.query import Query
6267
kwargs['dataset'] = self
6368
return Query(*args, **kwargs)
6469

6570
def entity(self, kind):
71+
"""Needs docstring."""
72+
# This import is here to avoid circular references.
6673
from gcloud.datastore.entity import Entity
6774
return Entity(dataset=self, kind=kind)
6875

6976
def transaction(self, *args, **kwargs):
77+
"""Needs docstring."""
78+
# This import is here to avoid circular references.
7079
from gcloud.datastore.transaction import Transaction
7180
kwargs['dataset'] = self
7281
return Transaction(*args, **kwargs)
@@ -86,6 +95,7 @@ def get_entity(self, key):
8695
return entities[0]
8796

8897
def get_entities(self, keys):
98+
"""Needs docstring."""
8999
# This import is here to avoid circular references.
90100
from gcloud.datastore.entity import Entity
91101

gcloud/datastore/key.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Needs module docstring."""
2+
13
import copy
24
from itertools import izip
35

@@ -157,8 +159,9 @@ def is_partial(self):
157159
:returns: True if the last element of the key's path does not have
158160
an 'id' or a 'name'.
159161
"""
160-
return (self.id_or_name() is None)
162+
return self.id_or_name() is None
161163

164+
# NOTE: This is identical to datastore.query.Query.dataset.
162165
def dataset(self, dataset=None):
163166
"""Dataset setter / getter.
164167
@@ -229,19 +232,19 @@ def kind(self, kind=None):
229232
elif self.path():
230233
return self._path[-1]['kind']
231234

232-
def id(self, id=None):
235+
def id(self, id_to_set=None):
233236
"""ID setter / getter. Based on the last element of path.
234237
235-
:type kind: :class:`str`
236-
:param kind: The new kind for the key.
238+
:type id_to_set: :class:`int`
239+
:param id_to_set: The new ID for the key.
237240
238241
:rtype: :class:`Key` (for setter); or :class:`int` (for getter)
239242
:returns: a new key, cloned from self., with the given id (setter);
240-
or self's id (getter).
243+
or self's id (getter).
241244
"""
242-
if id:
245+
if id_to_set:
243246
clone = self._clone()
244-
clone._path[-1]['id'] = id
247+
clone._path[-1]['id'] = id_to_set
245248
return clone
246249
elif self.path():
247250
return self._path[-1].get('id')

gcloud/datastore/query.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Needs module docstring."""
2+
13
import base64
24
import copy
35

@@ -60,6 +62,7 @@ def __init__(self, kind=None, dataset=None):
6062
self._pb.kind.add().name = kind
6163

6264
def _clone(self):
65+
"""Needs docstring."""
6366
clone = copy.deepcopy(self)
6467
clone._dataset = self._dataset # Shallow copy the dataset.
6568
return clone
@@ -308,11 +311,9 @@ def fetch(self, limit=None):
308311
if limit:
309312
clone = self.limit(limit)
310313

311-
(entity_pbs,
312-
end_cursor,
313-
more_results,
314-
skipped_results) = self.dataset().connection().run_query(
314+
query_results = self.dataset().connection().run_query(
315315
query_pb=clone.to_protobuf(), dataset_id=self.dataset().id())
316+
entity_pbs, end_cursor = query_results[:2]
316317

317318
self._cursor = end_cursor
318319
return [Entity.from_protobuf(entity, dataset=self.dataset())
@@ -372,14 +373,14 @@ def order(self, *properties):
372373
"""
373374
clone = self._clone()
374375

375-
for p in properties:
376+
for prop in properties:
376377
property_order = clone._pb.order.add()
377378

378-
if p.startswith('-'):
379-
property_order.property.name = p[1:]
379+
if prop.startswith('-'):
380+
property_order.property.name = prop[1:]
380381
property_order.direction = property_order.DESCENDING
381382
else:
382-
property_order.property.name = p
383+
property_order.property.name = prop
383384
property_order.direction = property_order.ASCENDING
384385

385386
return clone

gcloud/datastore/test___init__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
class Test_get_connection(unittest2.TestCase):
55

6-
def _callFUT(self, client_email, private_key_path):
6+
@staticmethod
7+
def _callFUT(client_email, private_key_path):
78
from gcloud.datastore import get_connection
89
return get_connection(client_email, private_key_path)
910

@@ -25,16 +26,16 @@ def test_it(self):
2526
found = self._callFUT(CLIENT_EMAIL, f.name)
2627
self.assertTrue(isinstance(found, Connection))
2728
self.assertTrue(found._credentials is client._signed)
28-
self.assertEqual(client._called_with,
29-
{'service_account_name': CLIENT_EMAIL,
30-
'private_key': PRIVATE_KEY,
31-
'scope': SCOPE,
32-
})
29+
expected_called_with = {'service_account_name': CLIENT_EMAIL,
30+
'private_key': PRIVATE_KEY,
31+
'scope': SCOPE}
32+
self.assertEqual(client._called_with, expected_called_with)
3333

3434

3535
class Test_get_dataset(unittest2.TestCase):
3636

37-
def _callFUT(self, dataset_id, client_email, private_key_path):
37+
@staticmethod
38+
def _callFUT(dataset_id, client_email, private_key_path):
3839
from gcloud.datastore import get_dataset
3940
return get_dataset(dataset_id, client_email, private_key_path)
4041

@@ -59,8 +60,7 @@ def test_it(self):
5960
self.assertTrue(isinstance(found, Dataset))
6061
self.assertTrue(isinstance(found.connection(), Connection))
6162
self.assertEqual(found.id(), DATASET_ID)
62-
self.assertEqual(client._called_with,
63-
{'service_account_name': CLIENT_EMAIL,
64-
'private_key': PRIVATE_KEY,
65-
'scope': SCOPE,
66-
})
63+
expected_called_with = {'service_account_name': CLIENT_EMAIL,
64+
'private_key': PRIVATE_KEY,
65+
'scope': SCOPE}
66+
self.assertEqual(client._called_with, expected_called_with)

gcloud/datastore/test__helpers.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
class Test__get_protobuf_attribute_and_value(unittest2.TestCase):
55

6-
def _callFUT(self, val):
6+
@staticmethod
7+
def _callFUT(val):
78
from gcloud.datastore._helpers import _get_protobuf_attribute_and_value
89

910
return _get_protobuf_attribute_and_value(val)
@@ -96,12 +97,14 @@ def test_object(self):
9697

9798
class Test__get_value_from_protobuf(unittest2.TestCase):
9899

99-
def _callFUT(self, pb):
100+
@staticmethod
101+
def _callFUT(pb):
100102
from gcloud.datastore._helpers import _get_value_from_protobuf
101103

102104
return _get_value_from_protobuf(pb)
103105

104-
def _makePB(self, attr_name, value):
106+
@staticmethod
107+
def _makePB(attr_name, value):
105108
from gcloud.datastore.datastore_v1_pb2 import Property
106109

107110
prop = Property()
@@ -170,17 +173,19 @@ def test_unknown(self):
170173
from gcloud.datastore.datastore_v1_pb2 import Property
171174

172175
pb = Property()
173-
self.assertEqual(self._callFUT(pb), None) # XXX desirable?
176+
self.assertEqual(self._callFUT(pb), None)
174177

175178

176179
class Test_set_protobuf_value(unittest2.TestCase):
177180

178-
def _callFUT(self, value_pb, val):
181+
@staticmethod
182+
def _callFUT(value_pb, val):
179183
from gcloud.datastore._helpers import _set_protobuf_value
180184

181185
return _set_protobuf_value(value_pb, val)
182186

183-
def _makePB(self):
187+
@staticmethod
188+
def _makePB():
184189
from gcloud.datastore.datastore_v1_pb2 import Value
185190

186191
return Value()

0 commit comments

Comments
 (0)