Skip to content

Commit 9d6efcf

Browse files
committed
Merge pull request #373 from tseaver/338-make_acl_lazy_load-remove_get_acl
Fix #338: make 'acl' lazy load, remove 'get-acl()'
2 parents 53db759 + 7e4acb0 commit 9d6efcf

File tree

7 files changed

+140
-55
lines changed

7 files changed

+140
-55
lines changed

gcloud/storage/_helpers.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,6 @@ def _get_property(self, field, default=None):
138138

139139
return self.properties.get(field, default)
140140

141-
def get_acl(self):
142-
"""Get ACL as an object.
143-
144-
:returns: An ACL object for the current object.
145-
"""
146-
if not self.acl.loaded:
147-
self.acl.reload()
148-
return self.acl
149-
150141

151142
class _PropertyBatch(object):
152143
"""Context manager: Batch updates to object's ``_patch_properties``

gcloud/storage/acl.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
33
:class:`gcloud.storage.bucket.Bucket` has a getting method that creates
44
an ACL object under the hood, and you can interact with that using
5-
:func:`gcloud.storage.bucket.Bucket.get_acl`::
5+
:func:`gcloud.storage.bucket.Bucket.acl`::
66
77
>>> from gcloud import storage
88
>>> connection = storage.get_connection(project, email, key_path)
99
>>> bucket = connection.get_bucket(bucket_name)
10-
>>> acl = bucket.get_acl()
10+
>>> acl = bucket.acl
1111
1212
Adding and removing permissions can be done with the following methods
1313
(in increasing order of granularity):
@@ -172,12 +172,19 @@ class ACL(object):
172172
def __init__(self):
173173
self.entities = {}
174174

175+
def _ensure_loaded(self):
176+
"""Load if not already loaded."""
177+
if not self.loaded:
178+
self.reload()
179+
175180
def reset(self):
176181
"""Remove all entities from the ACL, and clear the ``loaded`` flag."""
177182
self.entities.clear()
178183
self.loaded = False
179184

180185
def __iter__(self):
186+
self._ensure_loaded()
187+
181188
for entity in self.entities.itervalues():
182189
for role in entity.get_roles():
183190
if role:
@@ -224,6 +231,7 @@ def has_entity(self, entity):
224231
:rtype: bool
225232
:returns: True of the entity exists in the ACL.
226233
"""
234+
self._ensure_loaded()
227235
return str(entity) in self.entities
228236

229237
def get_entity(self, entity, default=None):
@@ -240,6 +248,7 @@ def get_entity(self, entity, default=None):
240248
:returns: The corresponding entity or the value provided
241249
to ``default``.
242250
"""
251+
self._ensure_loaded()
243252
return self.entities.get(str(entity), default)
244253

245254
def add_entity(self, entity):
@@ -248,8 +257,8 @@ def add_entity(self, entity):
248257
:type entity: :class:`_ACLEntity`
249258
:param entity: The entity to add to this ACL.
250259
"""
260+
self._ensure_loaded()
251261
self.entities[str(entity)] = entity
252-
self.loaded = True
253262

254263
def entity(self, entity_type, identifier=None):
255264
"""Factory method for creating an Entity.
@@ -332,6 +341,7 @@ def get_entities(self):
332341
:rtype: list of :class:`_ACLEntity` objects
333342
:returns: A list of all Entity objects.
334343
"""
344+
self._ensure_loaded()
335345
return self.entities.values()
336346

337347
def reload(self):
@@ -381,12 +391,10 @@ def reload(self):
381391

382392
url_path = '%s/%s' % (self.bucket.path, self._URL_PATH_ELEM)
383393
found = self.bucket.connection.api_request(method='GET', path=url_path)
394+
self.loaded = True
384395
for entry in found['items']:
385396
self.add_entity(self.entity_from_dict(entry))
386397

387-
# Even if we fetch no entries, the ACL is still loaded.
388-
self.loaded = True
389-
390398
return self
391399

392400
def save(self, acl=None):
@@ -407,7 +415,7 @@ def save(self, acl=None):
407415
408416
>>> bucket1 = connection.get_bucket(bucket1_name)
409417
>>> bucket2 = connection.get_bucket(bucket2_name)
410-
>>> bucket2.acl.save(bucket1.get_acl())
418+
>>> bucket2.acl.save(bucket1.acl)
411419
412420
:type acl: :class:`gcloud.storage.acl.ACL`, or a compatible list.
413421
:param acl: The ACL object to save. If left blank, this will save
@@ -489,12 +497,10 @@ def reload(self):
489497

490498
url_path = '%s/acl' % self.key.path
491499
found = self.key.connection.api_request(method='GET', path=url_path)
500+
self.loaded = True
492501
for entry in found['items']:
493502
self.add_entity(self.entity_from_dict(entry))
494503

495-
# Even if we fetch no entries, the ACL is still loaded.
496-
self.loaded = True
497-
498504
return self
499505

500506
def save(self, acl=None):

gcloud/storage/bucket.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Bucket(_PropertyMixin):
5050
_iterator_class = _KeyIterator
5151

5252
CUSTOM_PROPERTY_ACCESSORS = {
53-
'acl': 'get_acl()',
53+
'acl': 'acl',
5454
'cors': 'get_cors()',
5555
'defaultObjectAcl': 'get_default_object_acl()',
5656
'etag': 'etag',
@@ -694,7 +694,7 @@ def make_public(self, recursive=False, future=False):
694694
:param future: If True, this will make all objects created in the
695695
future public as well.
696696
"""
697-
self.get_acl().all().grant_read()
697+
self.acl.all().grant_read()
698698
self.acl.save()
699699

700700
if future:
@@ -704,5 +704,5 @@ def make_public(self, recursive=False, future=False):
704704

705705
if recursive:
706706
for key in self:
707-
key.get_acl().all().grant_read()
707+
key.acl.all().grant_read()
708708
key.save_acl()

gcloud/storage/key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Key(_PropertyMixin):
1616
"""A wrapper around Cloud Storage's concept of an ``Object``."""
1717

1818
CUSTOM_PROPERTY_ACCESSORS = {
19-
'acl': 'get_acl()',
19+
'acl': 'acl',
2020
'cacheControl': 'cache_control',
2121
'contentDisposition': 'content_disposition',
2222
'contentEncoding': 'content_encoding',
@@ -383,7 +383,7 @@ def make_public(self):
383383
384384
:returns: The current object.
385385
"""
386-
self.get_acl().all().grant_read()
386+
self.acl.all().grant_read()
387387
self.acl.save()
388388
return self
389389

gcloud/storage/test__helpers.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,6 @@ def test__patch_properties(self):
117117
self.assertEqual(kw[0]['data'], {'foo': 'Foo'})
118118
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
119119

120-
def test_get_acl_not_yet_loaded(self):
121-
class ACL(object):
122-
loaded = False
123-
124-
def reload(self):
125-
self.loaded = True
126-
127-
mixin = self._makeOne()
128-
acl = mixin.acl = ACL()
129-
self.assertTrue(mixin.get_acl() is acl)
130-
self.assertTrue(acl.loaded)
131-
132-
def test_get_acl_already_loaded(self):
133-
class ACL(object):
134-
loaded = True
135-
mixin = self._makeOne()
136-
acl = mixin.acl = ACL()
137-
self.assertTrue(mixin.get_acl() is acl) # no 'reload'
138-
139120

140121
class TestPropertyBatch(unittest2.TestCase):
141122

0 commit comments

Comments
 (0)