model.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://aws.amazon.com/apache2.0/
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. """Abstractions to interact with service models."""
  14. from collections import defaultdict
  15. from botocore.utils import (CachedProperty, instance_cache,
  16. hyphenize_service_id)
  17. from botocore.compat import OrderedDict
  18. from botocore.exceptions import MissingServiceIdError
  19. from botocore.exceptions import UndefinedModelAttributeError
  20. NOT_SET = object()
  21. class NoShapeFoundError(Exception):
  22. pass
  23. class InvalidShapeError(Exception):
  24. pass
  25. class OperationNotFoundError(Exception):
  26. pass
  27. class InvalidShapeReferenceError(Exception):
  28. pass
  29. class ServiceId(str):
  30. def hyphenize(self):
  31. return hyphenize_service_id(self)
  32. class Shape(object):
  33. """Object representing a shape from the service model."""
  34. # To simplify serialization logic, all shape params that are
  35. # related to serialization are moved from the top level hash into
  36. # a 'serialization' hash. This list below contains the names of all
  37. # the attributes that should be moved.
  38. SERIALIZED_ATTRS = ['locationName', 'queryName', 'flattened', 'location',
  39. 'payload', 'streaming', 'timestampFormat',
  40. 'xmlNamespace', 'resultWrapper', 'xmlAttribute',
  41. 'eventstream', 'event', 'eventheader', 'eventpayload',
  42. 'jsonvalue', 'timestampFormat', 'hostLabel']
  43. METADATA_ATTRS = ['required', 'min', 'max', 'sensitive', 'enum',
  44. 'idempotencyToken', 'error', 'exception',
  45. 'endpointdiscoveryid', 'retryable', 'document']
  46. MAP_TYPE = OrderedDict
  47. def __init__(self, shape_name, shape_model, shape_resolver=None):
  48. """
  49. :type shape_name: string
  50. :param shape_name: The name of the shape.
  51. :type shape_model: dict
  52. :param shape_model: The shape model. This would be the value
  53. associated with the key in the "shapes" dict of the
  54. service model (i.e ``model['shapes'][shape_name]``)
  55. :type shape_resolver: botocore.model.ShapeResolver
  56. :param shape_resolver: A shape resolver object. This is used to
  57. resolve references to other shapes. For scalar shape types
  58. (string, integer, boolean, etc.), this argument is not
  59. required. If a shape_resolver is not provided for a complex
  60. type, then a ``ValueError`` will be raised when an attempt
  61. to resolve a shape is made.
  62. """
  63. self.name = shape_name
  64. self.type_name = shape_model['type']
  65. self.documentation = shape_model.get('documentation', '')
  66. self._shape_model = shape_model
  67. if shape_resolver is None:
  68. # If a shape_resolver is not provided, we create an object
  69. # that will throw errors if you attempt to resolve
  70. # a shape. This is actually ok for scalar shapes
  71. # because they don't need to resolve shapes and shouldn't
  72. # be required to provide an object they won't use.
  73. shape_resolver = UnresolvableShapeMap()
  74. self._shape_resolver = shape_resolver
  75. self._cache = {}
  76. @CachedProperty
  77. def serialization(self):
  78. """Serialization information about the shape.
  79. This contains information that may be needed for input serialization
  80. or response parsing. This can include:
  81. * name
  82. * queryName
  83. * flattened
  84. * location
  85. * payload
  86. * streaming
  87. * xmlNamespace
  88. * resultWrapper
  89. * xmlAttribute
  90. * jsonvalue
  91. * timestampFormat
  92. :rtype: dict
  93. :return: Serialization information about the shape.
  94. """
  95. model = self._shape_model
  96. serialization = {}
  97. for attr in self.SERIALIZED_ATTRS:
  98. if attr in self._shape_model:
  99. serialization[attr] = model[attr]
  100. # For consistency, locationName is renamed to just 'name'.
  101. if 'locationName' in serialization:
  102. serialization['name'] = serialization.pop('locationName')
  103. return serialization
  104. @CachedProperty
  105. def metadata(self):
  106. """Metadata about the shape.
  107. This requires optional information about the shape, including:
  108. * min
  109. * max
  110. * enum
  111. * sensitive
  112. * required
  113. * idempotencyToken
  114. * document
  115. :rtype: dict
  116. :return: Metadata about the shape.
  117. """
  118. model = self._shape_model
  119. metadata = {}
  120. for attr in self.METADATA_ATTRS:
  121. if attr in self._shape_model:
  122. metadata[attr] = model[attr]
  123. return metadata
  124. @CachedProperty
  125. def required_members(self):
  126. """A list of members that are required.
  127. A structure shape can define members that are required.
  128. This value will return a list of required members. If there
  129. are no required members an empty list is returned.
  130. """
  131. return self.metadata.get('required', [])
  132. def _resolve_shape_ref(self, shape_ref):
  133. return self._shape_resolver.resolve_shape_ref(shape_ref)
  134. def __repr__(self):
  135. return "<%s(%s)>" % (self.__class__.__name__,
  136. self.name)
  137. @property
  138. def event_stream_name(self):
  139. return None
  140. class StructureShape(Shape):
  141. @CachedProperty
  142. def members(self):
  143. members = self._shape_model.get('members', self.MAP_TYPE())
  144. # The members dict looks like:
  145. # 'members': {
  146. # 'MemberName': {'shape': 'shapeName'},
  147. # 'MemberName2': {'shape': 'shapeName'},
  148. # }
  149. # We return a dict of member name to Shape object.
  150. shape_members = self.MAP_TYPE()
  151. for name, shape_ref in members.items():
  152. shape_members[name] = self._resolve_shape_ref(shape_ref)
  153. return shape_members
  154. @CachedProperty
  155. def event_stream_name(self):
  156. for member_name, member in self.members.items():
  157. if member.serialization.get('eventstream'):
  158. return member_name
  159. return None
  160. @CachedProperty
  161. def error_code(self):
  162. if not self.metadata.get('exception', False):
  163. return None
  164. error_metadata = self.metadata.get("error", {})
  165. code = error_metadata.get("code")
  166. if code:
  167. return code
  168. # Use the exception name if there is no explicit code modeled
  169. return self.name
  170. @CachedProperty
  171. def is_document_type(self):
  172. return self.metadata.get('document', False)
  173. class ListShape(Shape):
  174. @CachedProperty
  175. def member(self):
  176. return self._resolve_shape_ref(self._shape_model['member'])
  177. class MapShape(Shape):
  178. @CachedProperty
  179. def key(self):
  180. return self._resolve_shape_ref(self._shape_model['key'])
  181. @CachedProperty
  182. def value(self):
  183. return self._resolve_shape_ref(self._shape_model['value'])
  184. class StringShape(Shape):
  185. @CachedProperty
  186. def enum(self):
  187. return self.metadata.get('enum', [])
  188. class ServiceModel(object):
  189. """
  190. :ivar service_description: The parsed service description dictionary.
  191. """
  192. def __init__(self, service_description, service_name=None):
  193. """
  194. :type service_description: dict
  195. :param service_description: The service description model. This value
  196. is obtained from a botocore.loader.Loader, or from directly loading
  197. the file yourself::
  198. service_description = json.load(
  199. open('/path/to/service-description-model.json'))
  200. model = ServiceModel(service_description)
  201. :type service_name: str
  202. :param service_name: The name of the service. Normally this is
  203. the endpoint prefix defined in the service_description. However,
  204. you can override this value to provide a more convenient name.
  205. This is done in a few places in botocore (ses instead of email,
  206. emr instead of elasticmapreduce). If this value is not provided,
  207. it will default to the endpointPrefix defined in the model.
  208. """
  209. self._service_description = service_description
  210. # We want clients to be able to access metadata directly.
  211. self.metadata = service_description.get('metadata', {})
  212. self._shape_resolver = ShapeResolver(
  213. service_description.get('shapes', {}))
  214. self._signature_version = NOT_SET
  215. self._service_name = service_name
  216. self._instance_cache = {}
  217. def shape_for(self, shape_name, member_traits=None):
  218. return self._shape_resolver.get_shape_by_name(
  219. shape_name, member_traits)
  220. def shape_for_error_code(self, error_code):
  221. return self._error_code_cache.get(error_code, None)
  222. @CachedProperty
  223. def _error_code_cache(self):
  224. error_code_cache = {}
  225. for error_shape in self.error_shapes:
  226. code = error_shape.error_code
  227. error_code_cache[code] = error_shape
  228. return error_code_cache
  229. def resolve_shape_ref(self, shape_ref):
  230. return self._shape_resolver.resolve_shape_ref(shape_ref)
  231. @CachedProperty
  232. def shape_names(self):
  233. return list(self._service_description.get('shapes', {}))
  234. @CachedProperty
  235. def error_shapes(self):
  236. error_shapes = []
  237. for shape_name in self.shape_names:
  238. error_shape = self.shape_for(shape_name)
  239. if error_shape.metadata.get('exception', False):
  240. error_shapes.append(error_shape)
  241. return error_shapes
  242. @instance_cache
  243. def operation_model(self, operation_name):
  244. try:
  245. model = self._service_description['operations'][operation_name]
  246. except KeyError:
  247. raise OperationNotFoundError(operation_name)
  248. return OperationModel(model, self, operation_name)
  249. @CachedProperty
  250. def documentation(self):
  251. return self._service_description.get('documentation', '')
  252. @CachedProperty
  253. def operation_names(self):
  254. return list(self._service_description.get('operations', []))
  255. @CachedProperty
  256. def service_name(self):
  257. """The name of the service.
  258. This defaults to the endpointPrefix defined in the service model.
  259. However, this value can be overriden when a ``ServiceModel`` is
  260. created. If a service_name was not provided when the ``ServiceModel``
  261. was created and if there is no endpointPrefix defined in the
  262. service model, then an ``UndefinedModelAttributeError`` exception
  263. will be raised.
  264. """
  265. if self._service_name is not None:
  266. return self._service_name
  267. else:
  268. return self.endpoint_prefix
  269. @CachedProperty
  270. def service_id(self):
  271. try:
  272. return ServiceId(self._get_metadata_property('serviceId'))
  273. except UndefinedModelAttributeError:
  274. raise MissingServiceIdError(
  275. service_name=self._service_name
  276. )
  277. @CachedProperty
  278. def signing_name(self):
  279. """The name to use when computing signatures.
  280. If the model does not define a signing name, this
  281. value will be the endpoint prefix defined in the model.
  282. """
  283. signing_name = self.metadata.get('signingName')
  284. if signing_name is None:
  285. signing_name = self.endpoint_prefix
  286. return signing_name
  287. @CachedProperty
  288. def api_version(self):
  289. return self._get_metadata_property('apiVersion')
  290. @CachedProperty
  291. def protocol(self):
  292. return self._get_metadata_property('protocol')
  293. @CachedProperty
  294. def endpoint_prefix(self):
  295. return self._get_metadata_property('endpointPrefix')
  296. @CachedProperty
  297. def endpoint_discovery_operation(self):
  298. for operation in self.operation_names:
  299. model = self.operation_model(operation)
  300. if model.is_endpoint_discovery_operation:
  301. return model
  302. @CachedProperty
  303. def endpoint_discovery_required(self):
  304. for operation in self.operation_names:
  305. model = self.operation_model(operation)
  306. if (model.endpoint_discovery is not None and
  307. model.endpoint_discovery.get('required')):
  308. return True
  309. return False
  310. def _get_metadata_property(self, name):
  311. try:
  312. return self.metadata[name]
  313. except KeyError:
  314. raise UndefinedModelAttributeError(
  315. '"%s" not defined in the metadata of the model: %s' %
  316. (name, self))
  317. # Signature version is one of the rare properties
  318. # than can be modified so a CachedProperty is not used here.
  319. @property
  320. def signature_version(self):
  321. if self._signature_version is NOT_SET:
  322. signature_version = self.metadata.get('signatureVersion')
  323. self._signature_version = signature_version
  324. return self._signature_version
  325. @signature_version.setter
  326. def signature_version(self, value):
  327. self._signature_version = value
  328. def __repr__(self):
  329. return '%s(%s)' % (self.__class__.__name__, self.service_name)
  330. class OperationModel(object):
  331. def __init__(self, operation_model, service_model, name=None):
  332. """
  333. :type operation_model: dict
  334. :param operation_model: The operation model. This comes from the
  335. service model, and is the value associated with the operation
  336. name in the service model (i.e ``model['operations'][op_name]``).
  337. :type service_model: botocore.model.ServiceModel
  338. :param service_model: The service model associated with the operation.
  339. :type name: string
  340. :param name: The operation name. This is the operation name exposed to
  341. the users of this model. This can potentially be different from
  342. the "wire_name", which is the operation name that *must* by
  343. provided over the wire. For example, given::
  344. "CreateCloudFrontOriginAccessIdentity":{
  345. "name":"CreateCloudFrontOriginAccessIdentity2014_11_06",
  346. ...
  347. }
  348. The ``name`` would be ``CreateCloudFrontOriginAccessIdentity``,
  349. but the ``self.wire_name`` would be
  350. ``CreateCloudFrontOriginAccessIdentity2014_11_06``, which is the
  351. value we must send in the corresponding HTTP request.
  352. """
  353. self._operation_model = operation_model
  354. self._service_model = service_model
  355. self._api_name = name
  356. # Clients can access '.name' to get the operation name
  357. # and '.metadata' to get the top level metdata of the service.
  358. self._wire_name = operation_model.get('name')
  359. self.metadata = service_model.metadata
  360. self.http = operation_model.get('http', {})
  361. @CachedProperty
  362. def name(self):
  363. if self._api_name is not None:
  364. return self._api_name
  365. else:
  366. return self.wire_name
  367. @property
  368. def wire_name(self):
  369. """The wire name of the operation.
  370. In many situations this is the same value as the
  371. ``name``, value, but in some services, the operation name
  372. exposed to the user is different from the operaiton name
  373. we send across the wire (e.g cloudfront).
  374. Any serialization code should use ``wire_name``.
  375. """
  376. return self._operation_model.get('name')
  377. @property
  378. def service_model(self):
  379. return self._service_model
  380. @CachedProperty
  381. def documentation(self):
  382. return self._operation_model.get('documentation', '')
  383. @CachedProperty
  384. def deprecated(self):
  385. return self._operation_model.get('deprecated', False)
  386. @CachedProperty
  387. def endpoint_discovery(self):
  388. # Explicit None default. An empty dictionary for this trait means it is
  389. # enabled but not required to be used.
  390. return self._operation_model.get('endpointdiscovery', None)
  391. @CachedProperty
  392. def is_endpoint_discovery_operation(self):
  393. return self._operation_model.get('endpointoperation', False)
  394. @CachedProperty
  395. def input_shape(self):
  396. if 'input' not in self._operation_model:
  397. # Some operations do not accept any input and do not define an
  398. # input shape.
  399. return None
  400. return self._service_model.resolve_shape_ref(
  401. self._operation_model['input'])
  402. @CachedProperty
  403. def output_shape(self):
  404. if 'output' not in self._operation_model:
  405. # Some operations do not define an output shape,
  406. # in which case we return None to indicate the
  407. # operation has no expected output.
  408. return None
  409. return self._service_model.resolve_shape_ref(
  410. self._operation_model['output'])
  411. @CachedProperty
  412. def idempotent_members(self):
  413. input_shape = self.input_shape
  414. if not input_shape:
  415. return []
  416. return [name for (name, shape) in input_shape.members.items()
  417. if 'idempotencyToken' in shape.metadata and
  418. shape.metadata['idempotencyToken']]
  419. @CachedProperty
  420. def auth_type(self):
  421. return self._operation_model.get('authtype')
  422. @CachedProperty
  423. def error_shapes(self):
  424. shapes = self._operation_model.get("errors", [])
  425. return list(self._service_model.resolve_shape_ref(s) for s in shapes)
  426. @CachedProperty
  427. def endpoint(self):
  428. return self._operation_model.get('endpoint')
  429. @CachedProperty
  430. def http_checksum_required(self):
  431. return self._operation_model.get('httpChecksumRequired', False)
  432. @CachedProperty
  433. def has_event_stream_input(self):
  434. return self.get_event_stream_input() is not None
  435. @CachedProperty
  436. def has_event_stream_output(self):
  437. return self.get_event_stream_output() is not None
  438. def get_event_stream_input(self):
  439. return self._get_event_stream(self.input_shape)
  440. def get_event_stream_output(self):
  441. return self._get_event_stream(self.output_shape)
  442. def _get_event_stream(self, shape):
  443. """Returns the event stream member's shape if any or None otherwise."""
  444. if shape is None:
  445. return None
  446. event_name = shape.event_stream_name
  447. if event_name:
  448. return shape.members[event_name]
  449. return None
  450. @CachedProperty
  451. def has_streaming_input(self):
  452. return self.get_streaming_input() is not None
  453. @CachedProperty
  454. def has_streaming_output(self):
  455. return self.get_streaming_output() is not None
  456. def get_streaming_input(self):
  457. return self._get_streaming_body(self.input_shape)
  458. def get_streaming_output(self):
  459. return self._get_streaming_body(self.output_shape)
  460. def _get_streaming_body(self, shape):
  461. """Returns the streaming member's shape if any; or None otherwise."""
  462. if shape is None:
  463. return None
  464. payload = shape.serialization.get('payload')
  465. if payload is not None:
  466. payload_shape = shape.members[payload]
  467. if payload_shape.type_name == 'blob':
  468. return payload_shape
  469. return None
  470. def __repr__(self):
  471. return '%s(name=%s)' % (self.__class__.__name__, self.name)
  472. class ShapeResolver(object):
  473. """Resolves shape references."""
  474. # Any type not in this mapping will default to the Shape class.
  475. SHAPE_CLASSES = {
  476. 'structure': StructureShape,
  477. 'list': ListShape,
  478. 'map': MapShape,
  479. 'string': StringShape
  480. }
  481. def __init__(self, shape_map):
  482. self._shape_map = shape_map
  483. self._shape_cache = {}
  484. def get_shape_by_name(self, shape_name, member_traits=None):
  485. try:
  486. shape_model = self._shape_map[shape_name]
  487. except KeyError:
  488. raise NoShapeFoundError(shape_name)
  489. try:
  490. shape_cls = self.SHAPE_CLASSES.get(shape_model['type'], Shape)
  491. except KeyError:
  492. raise InvalidShapeError("Shape is missing required key 'type': %s"
  493. % shape_model)
  494. if member_traits:
  495. shape_model = shape_model.copy()
  496. shape_model.update(member_traits)
  497. result = shape_cls(shape_name, shape_model, self)
  498. return result
  499. def resolve_shape_ref(self, shape_ref):
  500. # A shape_ref is a dict that has a 'shape' key that
  501. # refers to a shape name as well as any additional
  502. # member traits that are then merged over the shape
  503. # definition. For example:
  504. # {"shape": "StringType", "locationName": "Foobar"}
  505. if len(shape_ref) == 1 and 'shape' in shape_ref:
  506. # It's just a shape ref with no member traits, we can avoid
  507. # a .copy(). This is the common case so it's specifically
  508. # called out here.
  509. return self.get_shape_by_name(shape_ref['shape'])
  510. else:
  511. member_traits = shape_ref.copy()
  512. try:
  513. shape_name = member_traits.pop('shape')
  514. except KeyError:
  515. raise InvalidShapeReferenceError(
  516. "Invalid model, missing shape reference: %s" % shape_ref)
  517. return self.get_shape_by_name(shape_name, member_traits)
  518. class UnresolvableShapeMap(object):
  519. """A ShapeResolver that will throw ValueErrors when shapes are resolved.
  520. """
  521. def get_shape_by_name(self, shape_name, member_traits=None):
  522. raise ValueError("Attempted to lookup shape '%s', but no shape "
  523. "map was provided.")
  524. def resolve_shape_ref(self, shape_ref):
  525. raise ValueError("Attempted to resolve shape '%s', but no shape "
  526. "map was provided.")
  527. class DenormalizedStructureBuilder(object):
  528. """Build a StructureShape from a denormalized model.
  529. This is a convenience builder class that makes it easy to construct
  530. ``StructureShape``s based on a denormalized model.
  531. It will handle the details of creating unique shape names and creating
  532. the appropriate shape map needed by the ``StructureShape`` class.
  533. Example usage::
  534. builder = DenormalizedStructureBuilder()
  535. shape = builder.with_members({
  536. 'A': {
  537. 'type': 'structure',
  538. 'members': {
  539. 'B': {
  540. 'type': 'structure',
  541. 'members': {
  542. 'C': {
  543. 'type': 'string',
  544. }
  545. }
  546. }
  547. }
  548. }
  549. }).build_model()
  550. # ``shape`` is now an instance of botocore.model.StructureShape
  551. :type dict_type: class
  552. :param dict_type: The dictionary type to use, allowing you to opt-in
  553. to using OrderedDict or another dict type. This can
  554. be particularly useful for testing when order
  555. matters, such as for documentation.
  556. """
  557. def __init__(self, name=None):
  558. self.members = OrderedDict()
  559. self._name_generator = ShapeNameGenerator()
  560. if name is None:
  561. self.name = self._name_generator.new_shape_name('structure')
  562. def with_members(self, members):
  563. """
  564. :type members: dict
  565. :param members: The denormalized members.
  566. :return: self
  567. """
  568. self._members = members
  569. return self
  570. def build_model(self):
  571. """Build the model based on the provided members.
  572. :rtype: botocore.model.StructureShape
  573. :return: The built StructureShape object.
  574. """
  575. shapes = OrderedDict()
  576. denormalized = {
  577. 'type': 'structure',
  578. 'members': self._members,
  579. }
  580. self._build_model(denormalized, shapes, self.name)
  581. resolver = ShapeResolver(shape_map=shapes)
  582. return StructureShape(shape_name=self.name,
  583. shape_model=shapes[self.name],
  584. shape_resolver=resolver)
  585. def _build_model(self, model, shapes, shape_name):
  586. if model['type'] == 'structure':
  587. shapes[shape_name] = self._build_structure(model, shapes)
  588. elif model['type'] == 'list':
  589. shapes[shape_name] = self._build_list(model, shapes)
  590. elif model['type'] == 'map':
  591. shapes[shape_name] = self._build_map(model, shapes)
  592. elif model['type'] in ['string', 'integer', 'boolean', 'blob', 'float',
  593. 'timestamp', 'long', 'double', 'char']:
  594. shapes[shape_name] = self._build_scalar(model)
  595. else:
  596. raise InvalidShapeError("Unknown shape type: %s" % model['type'])
  597. def _build_structure(self, model, shapes):
  598. members = OrderedDict()
  599. shape = self._build_initial_shape(model)
  600. shape['members'] = members
  601. for name, member_model in model.get('members', OrderedDict()).items():
  602. member_shape_name = self._get_shape_name(member_model)
  603. members[name] = {'shape': member_shape_name}
  604. self._build_model(member_model, shapes, member_shape_name)
  605. return shape
  606. def _build_list(self, model, shapes):
  607. member_shape_name = self._get_shape_name(model)
  608. shape = self._build_initial_shape(model)
  609. shape['member'] = {'shape': member_shape_name}
  610. self._build_model(model['member'], shapes, member_shape_name)
  611. return shape
  612. def _build_map(self, model, shapes):
  613. key_shape_name = self._get_shape_name(model['key'])
  614. value_shape_name = self._get_shape_name(model['value'])
  615. shape = self._build_initial_shape(model)
  616. shape['key'] = {'shape': key_shape_name}
  617. shape['value'] = {'shape': value_shape_name}
  618. self._build_model(model['key'], shapes, key_shape_name)
  619. self._build_model(model['value'], shapes, value_shape_name)
  620. return shape
  621. def _build_initial_shape(self, model):
  622. shape = {
  623. 'type': model['type'],
  624. }
  625. if 'documentation' in model:
  626. shape['documentation'] = model['documentation']
  627. for attr in Shape.METADATA_ATTRS:
  628. if attr in model:
  629. shape[attr] = model[attr]
  630. return shape
  631. def _build_scalar(self, model):
  632. return self._build_initial_shape(model)
  633. def _get_shape_name(self, model):
  634. if 'shape_name' in model:
  635. return model['shape_name']
  636. else:
  637. return self._name_generator.new_shape_name(model['type'])
  638. class ShapeNameGenerator(object):
  639. """Generate unique shape names for a type.
  640. This class can be used in conjunction with the DenormalizedStructureBuilder
  641. to generate unique shape names for a given type.
  642. """
  643. def __init__(self):
  644. self._name_cache = defaultdict(int)
  645. def new_shape_name(self, type_name):
  646. """Generate a unique shape name.
  647. This method will guarantee a unique shape name each time it is
  648. called with the same type.
  649. ::
  650. >>> s = ShapeNameGenerator()
  651. >>> s.new_shape_name('structure')
  652. 'StructureType1'
  653. >>> s.new_shape_name('structure')
  654. 'StructureType2'
  655. >>> s.new_shape_name('list')
  656. 'ListType1'
  657. >>> s.new_shape_name('list')
  658. 'ListType2'
  659. :type type_name: string
  660. :param type_name: The type name (structure, list, map, string, etc.)
  661. :rtype: string
  662. :return: A unique shape name for the given type
  663. """
  664. self._name_cache[type_name] += 1
  665. current_index = self._name_cache[type_name]
  666. return '%sType%s' % (type_name.capitalize(),
  667. current_index)