codekingpro/portable-devtools
114k
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"). You5# may not use this file except in compliance with the License. A copy of6# the License is located at7#8# http://aws.amazon.com/apache2.0/9#10# or in the "license" file accompanying this file. This file is11# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF12# ANY KIND, either express or implied. See the License for the specific13# language governing permissions and limitations under the License.14 15import logging16import os17import re18 19__version__ = '1.33.13'20 21 22class NullHandler(logging.Handler):23 def emit(self, record):24 pass25 26 27# Configure default logger to do nothing28log = 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 acronym36# 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 match39# 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}58# The items in this dict represent partial renames to apply globally to all59# services which might have a matching argument or operation. This way a60# common mis-translation can be fixed without having to call out each61# individual case.62ScalarTypes = ('string', 'integer', 'boolean', 'timestamp', 'float', 'double')63 64BOTOCORE_ROOT = os.path.dirname(os.path.abspath(__file__))65 66 67# Used to specify anonymous (unsigned) request signature68class UNSIGNED:69 def __copy__(self):70 return self71 72 def __deepcopy__(self, memodict):73 return self74 75 76UNSIGNED = UNSIGNED()77 78 79def xform_name(name, sep='_', _xform_cache=_xform_cache):80 """Convert camel case to a "pythonic" name.81 82 If the name contains the ``sep`` character, then it is83 returned unchanged.84 85 """86 if sep in name:87 # If the sep is in the name, assume that it's already88 # transformed and return the string unchanged.89 return name90 key = (name, sep)91 if key not in _xform_cache:92 if _special_case_transform.search(name) is not None:93 is_special = _special_case_transform.search(name)94 matched = is_special.group()95 # Replace something like ARNs, ACLs with _arns, _acls.96 name = f"{name[: -len(matched)]}{sep}{matched.lower()}"97 s1 = _first_cap_regex.sub(r'\1' + sep + r'\2', name)98 transformed = _end_cap_regex.sub(r'\1' + sep + r'\2', s1).lower()99 _xform_cache[key] = transformed100 return _xform_cache[key]101 102 103def register_initializer(callback):104 """Register an initializer function for session creation.105 106 This initializer function will be invoked whenever a new107 `botocore.session.Session` is instantiated.108 109 :type callback: callable110 :param callback: A callable that accepts a single argument111 of type `botocore.session.Session`.112 113 """114 _INITIALIZERS.append(callback)115 116 117def unregister_initializer(callback):118 """Unregister an initializer function.119 120 :type callback: callable121 :param callback: A callable that was previously registered122 with `botocore.register_initializer`.123 124 :raises ValueError: If a callback is provided that is not currently125 registered as an initializer.126 127 """128 _INITIALIZERS.remove(callback)129 130 131def invoke_initializers(session):132 """Invoke all initializers for a session.133 134 :type session: botocore.session.Session135 :param session: The session to initialize.136 137 """138 for initializer in _INITIALIZERS:139 initializer(session)140 