|
4 | 4 | """ |
5 | 5 |
|
6 | 6 |
|
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. |
9 | 9 |
|
10 | 10 | Non-abstract subclasses should implement: |
11 | | - - CUSTOM_METADATA_FIELDS |
| 11 | + - CUSTOM_PROPERTY_ACCESSORS |
12 | 12 | - connection |
13 | 13 | - path |
14 | 14 | """ |
15 | 15 |
|
16 | | - CUSTOM_METADATA_FIELDS = None |
| 16 | + CUSTOM_PROPERTY_ACCESSORS = None |
17 | 17 | """Mapping of field name -> accessor for fields w/ custom accessors. |
18 | 18 |
|
19 | 19 | 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. |
22 | 22 | """ |
23 | 23 |
|
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 | | - |
36 | 24 | @property |
37 | 25 | def connection(self): |
38 | 26 | """Abstract getter for the connection to use.""" |
@@ -43,90 +31,91 @@ def path(self): |
43 | 31 | """Abstract getter for the object path.""" |
44 | 32 | raise NotImplementedError |
45 | 33 |
|
46 | | - def has_metadata(self, field=None): |
47 | | - """Check if metadata is available. |
| 34 | + def __init__(self, name=None, properties=None): |
| 35 | + """_PropertyMixin constructor. |
48 | 36 |
|
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. |
51 | 39 |
|
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. |
54 | 42 | """ |
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) |
61 | 47 |
|
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() |
64 | 57 |
|
65 | | - :rtype: :class:`_MetadataMixin` |
| 58 | + def _reload_properties(self): |
| 59 | + """Reload properties from Cloud Storage. |
| 60 | +
|
| 61 | + :rtype: :class:`_PropertyMixin` |
66 | 62 | :returns: The object you just reloaded data for. |
67 | 63 | """ |
68 | 64 | # Pass only '?projection=noAcl' here because 'acl' and related |
69 | 65 | # are handled via 'get_acl()' etc. |
70 | 66 | query_params = {'projection': 'noAcl'} |
71 | | - self.metadata = self.connection.api_request( |
| 67 | + self._properties = self.connection.api_request( |
72 | 68 | method='GET', path=self.path, query_params=query_params) |
73 | 69 | return self |
74 | 70 |
|
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. |
77 | 95 |
|
78 | 96 | If you request a field that isn't available, and that field can |
79 | 97 | 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`. |
81 | 99 |
|
82 | 100 | :type field: string |
83 | | - :param field: (optional) A particular field to retrieve from metadata. |
| 101 | + :param field: A particular field to retrieve from properties. |
84 | 102 |
|
85 | 103 | :type default: anything |
86 | 104 | :param default: The value to return if the field provided wasn't found. |
87 | 105 |
|
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. |
92 | 108 | """ |
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) |
96 | 111 | if custom is not None: |
97 | | - message = 'Use %s or related methods instead.' % custom |
| 112 | + message = "Use '%s' or related methods instead." % custom |
98 | 113 | raise KeyError((field, message)) |
99 | 114 |
|
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) |
127 | 116 |
|
128 | 117 | def get_acl(self): |
129 | | - """Get ACL metadata as an object. |
| 118 | + """Get ACL as an object. |
130 | 119 |
|
131 | 120 | :returns: An ACL object for the current object. |
132 | 121 | """ |
|