GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #347 from tseaver/314-omnibus · googleapis/google-cloud-python@4a8f6b4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • gcloud/storage
    • _helpers.py
    • bucket.py
    • key.py
    • test__helpers.py
    • test_bucket.py
    • test_key.py
  • pylintrc_default
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎gcloud/storage/_helpers.py‎

Copy file name to clipboardExpand all lines: gcloud/storage/_helpers.py
+63-74Lines changed: 63 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,23 @@
44
"""
55

66

7-
class _MetadataMixin(object):
8-
"""Abstract mixin for cloud storage classes with associated metadata.
7+
class _PropertyMixin(object):
8+
"""Abstract mixin for cloud storage classes with associated propertties.
99
1010
Non-abstract subclasses should implement:
11-
- CUSTOM_METADATA_FIELDS
11+
- CUSTOM_PROPERTY_ACCESSORS
1212
- connection
1313
- path
1414
"""
1515

16-
CUSTOM_METADATA_FIELDS = None
16+
CUSTOM_PROPERTY_ACCESSORS = None
1717
"""Mapping of field name -> accessor for fields w/ custom accessors.
1818
1919
Expected to be set by subclasses. Fields in this mapping will cause
20-
`get_metadata()` to raise a KeyError with a message to use the relevant
21-
accessor methods.
20+
:meth:`_get_property()` to raise a KeyError with a message to use the
21+
relevant accessor methods.
2222
"""
2323

24-
def __init__(self, name=None, metadata=None):
25-
"""_MetadataMixin constructor.
26-
27-
:type name: string
28-
:param name: The name of the object.
29-
30-
:type metadata: dict
31-
:param metadata: All the other data provided by Cloud Storage.
32-
"""
33-
self.name = name
34-
self.metadata = metadata
35-
3624
@property
3725
def connection(self):
3826
"""Abstract getter for the connection to use."""
@@ -43,90 +31,91 @@ def path(self):
4331
"""Abstract getter for the object path."""
4432
raise NotImplementedError
4533

46-
def has_metadata(self, field=None):
47-
"""Check if metadata is available.
34+
def __init__(self, name=None, properties=None):
35+
"""_PropertyMixin constructor.
4836
49-
:type field: string
50-
:param field: (optional) the particular field to check for.
37+
:type name: string
38+
:param name: The name of the object.
5139
52-
:rtype: bool
53-
:returns: Whether metadata is available locally.
40+
:type properties: dict
41+
:param properties: All the other data provided by Cloud Storage.
5442
"""
55-
if not self.metadata:
56-
return False
57-
elif field and field not in self.metadata:
58-
return False
59-
else:
60-
return True
43+
self.name = name
44+
self._properties = {}
45+
if properties is not None:
46+
self._properties.update(properties)
6147

62-
def reload_metadata(self):
63-
"""Reload metadata from Cloud Storage.
48+
@property
49+
def properties(self):
50+
"""Ensure properties are loaded, and return a copy.
51+
52+
:rtype: dict
53+
"""
54+
if not self._properties:
55+
self._reload_properties()
56+
return self._properties.copy()
6457

65-
:rtype: :class:`_MetadataMixin`
58+
def _reload_properties(self):
59+
"""Reload properties from Cloud Storage.
60+
61+
:rtype: :class:`_PropertyMixin`
6662
:returns: The object you just reloaded data for.
6763
"""
6864
# Pass only '?projection=noAcl' here because 'acl' and related
6965
# are handled via 'get_acl()' etc.
7066
query_params = {'projection': 'noAcl'}
71-
self.metadata = self.connection.api_request(
67+
self._properties = self.connection.api_request(
7268
method='GET', path=self.path, query_params=query_params)
7369
return self
7470

75-
def get_metadata(self, field=None, default=None):
76-
"""Get all metadata or a specific field.
71+
def _patch_properties(self, properties):
72+
"""Update particular fields of this object's properties.
73+
74+
This method will only update the fields provided and will not
75+
touch the other fields.
76+
77+
It will also reload the properties locally based on the server's
78+
response.
79+
80+
:type properties: dict
81+
:param properties: The dictionary of values to update.
82+
83+
:rtype: :class:`_PropertyMixin`
84+
:returns: The current object.
85+
"""
86+
# Pass '?projection=full' here because 'PATCH' documented not
87+
# to work properly w/ 'noAcl'.
88+
self._properties = self.connection.api_request(
89+
method='PATCH', path=self.path, data=properties,
90+
query_params={'projection': 'full'})
91+
return self
92+
93+
def _get_property(self, field, default=None):
94+
"""Return the value of a field from the server-side representation.
7795
7896
If you request a field that isn't available, and that field can
7997
be retrieved by refreshing data from Cloud Storage, this method
80-
will reload the data using :func:`_MetadataMixin.reload_metadata`.
98+
will reload the data using :func:`_PropertyMixin._reload_properties`.
8199
82100
:type field: string
83-
:param field: (optional) A particular field to retrieve from metadata.
101+
:param field: A particular field to retrieve from properties.
84102
85103
:type default: anything
86104
:param default: The value to return if the field provided wasn't found.
87105
88-
:rtype: dict or anything
89-
:returns: All metadata or the value of the specific field.
90-
91-
:raises: :class:`KeyError` if the field is in CUSTOM_METADATA_FIELDS.
106+
:rtype: anything
107+
:returns: value of the specific field, or the default if not found.
92108
"""
93-
# We ignore 'acl' and related fields because they are meant to be
94-
# handled via 'get_acl()' and related methods.
95-
custom = self.CUSTOM_METADATA_FIELDS.get(field)
109+
# Raise for fields which have custom accessors.
110+
custom = self.CUSTOM_PROPERTY_ACCESSORS.get(field)
96111
if custom is not None:
97-
message = 'Use %s or related methods instead.' % custom
112+
message = "Use '%s' or related methods instead." % custom
98113
raise KeyError((field, message))
99114

100-
if not self.has_metadata(field=field):
101-
self.reload_metadata()
102-
103-
if field:
104-
return self.metadata.get(field, default)
105-
else:
106-
return self.metadata
107-
108-
def patch_metadata(self, metadata):
109-
"""Update particular fields of this object's metadata.
110-
111-
This method will only update the fields provided and will not
112-
touch the other fields.
113-
114-
It will also reload the metadata locally based on the server's
115-
response.
116-
117-
:type metadata: dict
118-
:param metadata: The dictionary of values to update.
119-
120-
:rtype: :class:`_MetadataMixin`
121-
:returns: The current object.
122-
"""
123-
self.metadata = self.connection.api_request(
124-
method='PATCH', path=self.path, data=metadata,
125-
query_params={'projection': 'full'})
126-
return self
115+
return self.properties.get(field, default)
127116

128117
def get_acl(self):
129-
"""Get ACL metadata as an object.
118+
"""Get ACL as an object.
130119
131120
:returns: An ACL object for the current object.
132121
"""

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.