14
14
import logging
15
15
from functools import partial
16
16
17
- from botocore import xform_name
18
-
19
17
from .action import ServiceAction
20
18
from .action import WaiterAction
21
19
from .base import ResourceMeta , ServiceResource
@@ -74,6 +72,11 @@ def load_from_definition(self, service_name, resource_name, model,
74
72
75
73
resource_model = ResourceModel (resource_name , model , resource_defs )
76
74
75
+ shape = None
76
+ if resource_model .shape :
77
+ shape = service_model .shape_for (resource_model .shape )
78
+ resource_model .load_rename_map (shape )
79
+
77
80
self ._load_identifiers (attrs , meta , resource_model )
78
81
self ._load_actions (attrs , resource_model , resource_defs ,
79
82
service_model )
@@ -98,11 +101,8 @@ def _load_identifiers(self, attrs, meta, model):
98
101
operations on the resource.
99
102
"""
100
103
for identifier in model .identifiers :
101
- snake_cased = xform_name (identifier .name )
102
- snake_cased = self ._check_allowed_name (
103
- attrs , snake_cased , 'identifier' , model .name )
104
- meta .identifiers .append (snake_cased )
105
- attrs [snake_cased ] = None
104
+ meta .identifiers .append (identifier .name )
105
+ attrs [identifier .name ] = None
106
106
107
107
def _load_actions (self , attrs , model , resource_defs , service_model ):
108
108
"""
@@ -112,16 +112,12 @@ def _load_actions(self, attrs, model, resource_defs, service_model):
112
112
"""
113
113
if model .load :
114
114
attrs ['load' ] = self ._create_action (
115
- 'load' , model .load , resource_defs , service_model ,
116
- is_load = True )
115
+ model .load , resource_defs , service_model , is_load = True )
117
116
attrs ['reload' ] = attrs ['load' ]
118
117
119
118
for action in model .actions :
120
- snake_cased = xform_name (action .name )
121
- snake_cased = self ._check_allowed_name (
122
- attrs , snake_cased , 'action' , model .name )
123
- attrs [snake_cased ] = self ._create_action (snake_cased ,
124
- action , resource_defs , service_model )
119
+ attrs [action .name ] = self ._create_action (action , resource_defs ,
120
+ service_model )
125
121
126
122
def _load_attributes (self , attrs , meta , model , service_model ):
127
123
"""
@@ -133,16 +129,9 @@ def _load_attributes(self, attrs, meta, model, service_model):
133
129
if model .shape :
134
130
shape = service_model .shape_for (model .shape )
135
131
136
- for name , member in shape .members .items ():
137
- snake_cased = xform_name (name )
138
- if snake_cased in meta .identifiers :
139
- # Skip identifiers, these are set through other means
140
- continue
141
-
142
- snake_cased = self ._check_allowed_name (
143
- attrs , snake_cased , 'attribute' , model .name )
144
- attrs [snake_cased ] = self ._create_autoload_property (name ,
145
- snake_cased )
132
+ attributes = model .get_attributes (shape )
133
+ for name , (orig_name , member ) in attributes .items ():
134
+ attrs [name ] = self ._create_autoload_property (orig_name , name )
146
135
147
136
def _load_collections (self , attrs , model , resource_defs , service_model ):
148
137
"""
@@ -152,12 +141,8 @@ def _load_collections(self, attrs, model, resource_defs, service_model):
152
141
through the collection's items.
153
142
"""
154
143
for collection_model in model .collections :
155
- snake_cased = xform_name (collection_model .name )
156
- snake_cased = self ._check_allowed_name (
157
- attrs , snake_cased , 'collection' , model .name )
158
-
159
- attrs [snake_cased ] = self ._create_collection (
160
- attrs ['meta' ].service_name , model .name , snake_cased ,
144
+ attrs [collection_model .name ] = self ._create_collection (
145
+ attrs ['meta' ].service_name , model .name ,
161
146
collection_model , resource_defs , service_model )
162
147
163
148
def _load_has_relations (self , attrs , service_name , resource_name ,
@@ -176,11 +161,8 @@ def _load_has_relations(self, attrs, service_name, resource_name,
176
161
# This is a dangling reference, i.e. we have all
177
162
# the data we need to create the resource, so
178
163
# this instance becomes an attribute on the class.
179
- snake_cased = xform_name (reference .name )
180
- snake_cased = self ._check_allowed_name (
181
- attrs , snake_cased , 'reference' , model .name )
182
- attrs [snake_cased ] = self ._create_reference (
183
- reference .resource .type , snake_cased , reference ,
164
+ attrs [reference .name ] = self ._create_reference (
165
+ reference .resource .type , reference ,
184
166
service_name , resource_name , model , resource_defs ,
185
167
service_model )
186
168
@@ -200,44 +182,7 @@ def _load_waiters(self, attrs, model):
200
182
of the resource.
201
183
"""
202
184
for waiter in model .waiters :
203
- snake_cased = xform_name (waiter .resource_waiter_name )
204
- snake_cased = self ._check_allowed_name (
205
- attrs , snake_cased , 'waiter' , model .name )
206
- attrs [snake_cased ] = self ._create_waiter (waiter , snake_cased )
207
-
208
- def _check_allowed_name (self , attrs , name , category , resource_name ):
209
- """
210
- Determine if a given name is allowed on the instance, and if not,
211
- then raise an exception. This prevents public attributes of the
212
- class from being clobbered, e.g. since we define ``Resource.meta``,
213
- no identifier may be named ``meta``. Another example: no action
214
- named ``queue_items`` may be added after an identifier of the same
215
- name has been added.
216
-
217
- One attempt is made in the event of a collision to remedy the
218
- situation. The ``category`` is appended to the name and the
219
- check is performed again. For example, if an action named
220
- ``get_frobs`` fails the test, then we try ``get_frobs_action``
221
- after logging a warning.
222
-
223
- :raises: ValueError
224
- """
225
- if name in attrs :
226
- logger .warning ('%s `%s` would clobber existing %s'
227
- ' resource attribute, going to try'
228
- ' %s instead...' , category , name ,
229
- resource_name , name + '_' + category )
230
- # TODO: Move this logic into the model and strictly
231
- # define the loading order of categories. This
232
- # will make documentation much simpler.
233
- name = name + '_' + category
234
-
235
- if name in attrs :
236
- raise ValueError ('{0} `{1}` would clobber existing '
237
- '{2} resource attribute' .format (
238
- category , name , resource_name ))
239
-
240
- return name
185
+ attrs [waiter .name ] = self ._create_waiter (waiter )
241
186
242
187
def _create_autoload_property (factory_self , name , snake_cased ):
243
188
"""
@@ -262,22 +207,22 @@ def property_loader(self):
262
207
property_loader .__doc__ = 'TODO'
263
208
return property (property_loader )
264
209
265
- def _create_waiter (factory_self , waiter_model , snake_cased ):
210
+ def _create_waiter (factory_self , waiter_model ):
266
211
"""
267
212
Creates a new wait method for each resource where both a waiter and
268
213
resource model is defined.
269
214
"""
270
- waiter = WaiterAction (waiter_model , waiter_resource_name = snake_cased )
215
+ waiter = WaiterAction (waiter_model ,
216
+ waiter_resource_name = waiter_model .name )
271
217
def do_waiter (self , * args , ** kwargs ):
272
218
waiter (self , * args , ** kwargs )
273
219
274
- do_waiter .__name__ = str (snake_cased )
220
+ do_waiter .__name__ = str (waiter_model . name )
275
221
do_waiter .__doc__ = 'TODO'
276
222
return do_waiter
277
223
278
224
def _create_collection (factory_self , service_name , resource_name ,
279
- snake_cased , collection_model ,
280
- resource_defs , service_model ):
225
+ collection_model , resource_defs , service_model ):
281
226
"""
282
227
Creates a new property on the resource to lazy-load a collection.
283
228
"""
@@ -289,13 +234,12 @@ def get_collection(self):
289
234
return cls (collection_model , self , factory_self ,
290
235
resource_defs , service_model )
291
236
292
- get_collection .__name__ = str (snake_cased )
237
+ get_collection .__name__ = str (collection_model . name )
293
238
get_collection .__doc__ = 'TODO'
294
239
return property (get_collection )
295
240
296
- def _create_reference (factory_self , name , snake_cased , reference ,
297
- service_name , resource_name , model , resource_defs ,
298
- service_model ):
241
+ def _create_reference (factory_self , name , reference , service_name ,
242
+ resource_name , model , resource_defs , service_model ):
299
243
"""
300
244
Creates a new property on the resource to lazy-load a reference.
301
245
"""
@@ -313,7 +257,7 @@ def get_reference(self):
313
257
# identifiers to instantiate the resource reference.
314
258
return handler (self , {}, {})
315
259
316
- get_reference .__name__ = str (snake_cased )
260
+ get_reference .__name__ = str (reference . name )
317
261
get_reference .__doc__ = 'TODO'
318
262
return property (get_reference )
319
263
@@ -352,7 +296,7 @@ def create_resource(self, *args, **kwargs):
352
296
create_resource .__doc__ = 'TODO'
353
297
return create_resource
354
298
355
- def _create_action (factory_self , snake_cased , action_model , resource_defs ,
299
+ def _create_action (factory_self , action_model , resource_defs ,
356
300
service_model , is_load = False ):
357
301
"""
358
302
Creates a new method which makes a request to the underlying
@@ -386,6 +330,6 @@ def do_action(self, *args, **kwargs):
386
330
387
331
return response
388
332
389
- do_action .__name__ = str (snake_cased )
333
+ do_action .__name__ = str (action_model . name )
390
334
do_action .__doc__ = 'TODO'
391
335
return do_action
0 commit comments