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
18
19__version__ = '1.35.30'
20
21
22class NullHandler(logging.Handler):
23 def emit(self, record):
24 pass
25
26
27# Configure default logger to do nothing
28log = logging.getLogger('botocore')
29log.addHandler(NullHandler())
30
31_INITIALIZERS = []
32
33_first_cap_regex = re.compile('(.)([A-Z][a-z]+)')
34_end_cap_regex = re.compile('([a-z0-9])([A-Z])')
35# The regex below handles the special case where some acronym
36# name is pluralized, e.g GatewayARNs, ListWebACLs, SomeCNAMEs.
37_special_case_transform = re.compile('[A-Z]{2,}s$')
38# Prepopulate the cache with special cases that don't match
39# our regular transformation.
40_xform_cache = {
41 ('CreateCachediSCSIVolume', '_'): 'create_cached_iscsi_volume',
42 ('CreateCachediSCSIVolume', '-'): 'create-cached-iscsi-volume',
43 ('DescribeCachediSCSIVolumes', '_'): 'describe_cached_iscsi_volumes',
44 ('DescribeCachediSCSIVolumes', '-'): 'describe-cached-iscsi-volumes',
45 ('DescribeStorediSCSIVolumes', '_'): 'describe_stored_iscsi_volumes',
46 ('DescribeStorediSCSIVolumes', '-'): 'describe-stored-iscsi-volumes',
47 ('CreateStorediSCSIVolume', '_'): 'create_stored_iscsi_volume',
48 ('CreateStorediSCSIVolume', '-'): 'create-stored-iscsi-volume',
49 ('ListHITsForQualificationType', '_'): 'list_hits_for_qualification_type',
50 ('ListHITsForQualificationType', '-'): 'list-hits-for-qualification-type',
51 ('ExecutePartiQLStatement', '_'): 'execute_partiql_statement',
52 ('ExecutePartiQLStatement', '-'): 'execute-partiql-statement',
53 ('ExecutePartiQLTransaction', '_'): 'execute_partiql_transaction',
54 ('ExecutePartiQLTransaction', '-'): 'execute-partiql-transaction',
55 ('ExecutePartiQLBatch', '_'): 'execute_partiql_batch',
56 ('ExecutePartiQLBatch', '-'): 'execute-partiql-batch',
57}
58ScalarTypes = ('string', 'integer', 'boolean', 'timestamp', 'float', 'double')
59
60BOTOCORE_ROOT = os.path.dirname(os.path.abspath(__file__))
61
62
63# Used to specify anonymous (unsigned) request signature
64class UNSIGNED:
65 def __copy__(self):
66 return self
67
68 def __deepcopy__(self, memodict):
69 return self
70
71
72UNSIGNED = UNSIGNED()
73
74
75def xform_name(name, sep='_', _xform_cache=_xform_cache):
76 """Convert camel case to a "pythonic" name.
77
78 If the name contains the ``sep`` character, then it is
79 returned unchanged.
80
81 """
82 if sep in name:
83 # If the sep is in the name, assume that it's already
84 # transformed and return the string unchanged.
85 return name
86 key = (name, sep)
87 if key not in _xform_cache:
88 if _special_case_transform.search(name) is not None:
89 is_special = _special_case_transform.search(name)
90 matched = is_special.group()
91 # Replace something like ARNs, ACLs with _arns, _acls.
92 name = f"{name[: -len(matched)]}{sep}{matched.lower()}"
93 s1 = _first_cap_regex.sub(r'\1' + sep + r'\2', name)
94 transformed = _end_cap_regex.sub(r'\1' + sep + r'\2', s1).lower()
95 _xform_cache[key] = transformed
96 return _xform_cache[key]
97
98
99def register_initializer(callback):
100 """Register an initializer function for session creation.
101
102 This initializer function will be invoked whenever a new
103 `botocore.session.Session` is instantiated.
104
105 :type callback: callable
106 :param callback: A callable that accepts a single argument
107 of type `botocore.session.Session`.
108
109 """
110 _INITIALIZERS.append(callback)
111
112
113def unregister_initializer(callback):
114 """Unregister an initializer function.
115
116 :type callback: callable
117 :param callback: A callable that was previously registered
118 with `botocore.register_initializer`.
119
120 :raises ValueError: If a callback is provided that is not currently
121 registered as an initializer.
122
123 """
124 _INITIALIZERS.remove(callback)
125
126
127def invoke_initializers(session):
128 """Invoke all initializers for a session.
129
130 :type session: botocore.session.Session
131 :param session: The session to initialize.
132
133 """
134 for initializer in _INITIALIZERS:
135 initializer(session)