1# Copyright (c) 2012-2013 Mitch Garnaat http://garnaat.org/
2# Copyright 2012-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"). You
5# may not use this file except in compliance with the License. A copy of
6# the License is located at
7#
8# http://aws.amazon.com/apache2.0/
9#
10# or in the "license" file accompanying this file. This file is
11# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12# ANY KIND, either express or implied. See the License for the specific
13# language governing permissions and limitations under the License.
14
15import logging
16import os
17import re
18from logging import NullHandler
19
20__version__ = '1.39.4'
21
22
23# Configure default logger to do nothing
24log = logging.getLogger('botocore')
25log.addHandler(NullHandler())
26
27_INITIALIZERS = []
28
29_first_cap_regex = re.compile('(.)([A-Z][a-z]+)')
30_end_cap_regex = re.compile('([a-z0-9])([A-Z])')
31# The regex below handles the special case where some acronym
32# name is pluralized, e.g GatewayARNs, ListWebACLs, SomeCNAMEs.
33_special_case_transform = re.compile('[A-Z]{2,}s$')
34# Prepopulate the cache with special cases that don't match
35# our regular transformation.
36_xform_cache = {
37 ('CreateCachediSCSIVolume', '_'): 'create_cached_iscsi_volume',
38 ('CreateCachediSCSIVolume', '-'): 'create-cached-iscsi-volume',
39 ('DescribeCachediSCSIVolumes', '_'): 'describe_cached_iscsi_volumes',
40 ('DescribeCachediSCSIVolumes', '-'): 'describe-cached-iscsi-volumes',
41 ('DescribeStorediSCSIVolumes', '_'): 'describe_stored_iscsi_volumes',
42 ('DescribeStorediSCSIVolumes', '-'): 'describe-stored-iscsi-volumes',
43 ('CreateStorediSCSIVolume', '_'): 'create_stored_iscsi_volume',
44 ('CreateStorediSCSIVolume', '-'): 'create-stored-iscsi-volume',
45 ('ListHITsForQualificationType', '_'): 'list_hits_for_qualification_type',
46 ('ListHITsForQualificationType', '-'): 'list-hits-for-qualification-type',
47 ('ExecutePartiQLStatement', '_'): 'execute_partiql_statement',
48 ('ExecutePartiQLStatement', '-'): 'execute-partiql-statement',
49 ('ExecutePartiQLTransaction', '_'): 'execute_partiql_transaction',
50 ('ExecutePartiQLTransaction', '-'): 'execute-partiql-transaction',
51 ('ExecutePartiQLBatch', '_'): 'execute_partiql_batch',
52 ('ExecutePartiQLBatch', '-'): 'execute-partiql-batch',
53 (
54 'AssociateWhatsAppBusinessAccount',
55 '_',
56 ): 'associate_whatsapp_business_account',
57 (
58 'AssociateWhatsAppBusinessAccount',
59 '-',
60 ): 'associate-whatsapp-business-account',
61 ('DeleteWhatsAppMessageMedia', '_'): 'delete_whatsapp_message_media',
62 ('DeleteWhatsAppMessageMedia', '-'): 'delete-whatsapp-message-media',
63 (
64 'DisassociateWhatsAppBusinessAccount',
65 '_',
66 ): 'disassociate_whatsapp_business_account',
67 (
68 'DisassociateWhatsAppBusinessAccount',
69 '-',
70 ): 'disassociate-whatsapp-business-account',
71 (
72 'GetLinkedWhatsAppBusinessAccount',
73 '_',
74 ): 'get_linked_whatsapp_business_account',
75 (
76 'GetLinkedWhatsAppBusinessAccount',
77 '-',
78 ): 'get-linked-whatsapp-business-account',
79 (
80 'GetLinkedWhatsAppBusinessAccountPhoneNumber',
81 '_',
82 ): 'get_linked_whatsapp_business_account_phone_number',
83 (
84 'GetLinkedWhatsAppBusinessAccountPhoneNumber',
85 '-',
86 ): 'get-linked-whatsapp-business-account-phone-number',
87 ('GetWhatsAppMessageMedia', '_'): 'get_whatsapp_message_media',
88 ('GetWhatsAppMessageMedia', '-'): 'get-whatsapp-message-media',
89 (
90 'ListLinkedWhatsAppBusinessAccounts',
91 '_',
92 ): 'list_linked_whatsapp_business_accounts',
93 (
94 'ListLinkedWhatsAppBusinessAccounts',
95 '-',
96 ): 'list-linked-whatsapp-business-accounts',
97 ('PostWhatsAppMessageMedia', '_'): 'post_whatsapp_message_media',
98 ('PostWhatsAppMessageMedia', '-'): 'post-whatsapp-message-media',
99 (
100 'PutWhatsAppBusinessAccountEventDestinations',
101 '_',
102 ): 'put_whatsapp_business_account_event_destinations',
103 (
104 'PutWhatsAppBusinessAccountEventDestinations',
105 '-',
106 ): 'put-whatsapp-business-account-event-destinations',
107 ('SendWhatsAppMessage', '_'): 'send_whatsapp_message',
108 ('SendWhatsAppMessage', '-'): 'send-whatsapp-message',
109}
110ScalarTypes = ('string', 'integer', 'boolean', 'timestamp', 'float', 'double')
111
112BOTOCORE_ROOT = os.path.dirname(os.path.abspath(__file__))
113
114
115# Used to specify anonymous (unsigned) request signature
116class UNSIGNED:
117 def __copy__(self):
118 return self
119
120 def __deepcopy__(self, memodict):
121 return self
122
123
124UNSIGNED = UNSIGNED()
125
126
127def xform_name(name, sep='_', _xform_cache=_xform_cache):
128 """Convert camel case to a "pythonic" name.
129
130 If the name contains the ``sep`` character, then it is
131 returned unchanged.
132
133 """
134 if sep in name:
135 # If the sep is in the name, assume that it's already
136 # transformed and return the string unchanged.
137 return name
138 key = (name, sep)
139 if key not in _xform_cache:
140 if _special_case_transform.search(name) is not None:
141 is_special = _special_case_transform.search(name)
142 matched = is_special.group()
143 # Replace something like ARNs, ACLs with _arns, _acls.
144 name = f"{name[: -len(matched)]}{sep}{matched.lower()}"
145 s1 = _first_cap_regex.sub(r'\1' + sep + r'\2', name)
146 transformed = _end_cap_regex.sub(r'\1' + sep + r'\2', s1).lower()
147 _xform_cache[key] = transformed
148 return _xform_cache[key]
149
150
151def register_initializer(callback):
152 """Register an initializer function for session creation.
153
154 This initializer function will be invoked whenever a new
155 `botocore.session.Session` is instantiated.
156
157 :type callback: callable
158 :param callback: A callable that accepts a single argument
159 of type `botocore.session.Session`.
160
161 """
162 _INITIALIZERS.append(callback)
163
164
165def unregister_initializer(callback):
166 """Unregister an initializer function.
167
168 :type callback: callable
169 :param callback: A callable that was previously registered
170 with `botocore.register_initializer`.
171
172 :raises ValueError: If a callback is provided that is not currently
173 registered as an initializer.
174
175 """
176 _INITIALIZERS.remove(callback)
177
178
179def invoke_initializers(session):
180 """Invoke all initializers for a session.
181
182 :type session: botocore.session.Session
183 :param session: The session to initialize.
184
185 """
186 for initializer in _INITIALIZERS:
187 initializer(session)