本文整理汇总了Python中botocore.hooks.HierarchicalEmitter.register方法的典型用法代码示例。如果您正苦于以下问题:Python HierarchicalEmitter.register方法的具体用法?Python HierarchicalEmitter.register怎么用?Python HierarchicalEmitter.register使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类botocore.hooks.HierarchicalEmitter
的用法示例。
在下文中一共展示了HierarchicalEmitter.register方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cli_driver_changes_args
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
def test_cli_driver_changes_args(self):
emitter = HierarchicalEmitter()
emitter.register("process-cli-arg.s3.list-objects", self.serialize_param)
self.session.emitter = emitter
driver = CLIDriver(session=self.session)
driver.main("s3 list-objects --bucket foo".split())
self.assertIn(mock.call.paginate(mock.ANY, bucket="foo-altered!"), self.session.operation.method_calls)
示例2: test_emitter_can_be_passed_in
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
def test_emitter_can_be_passed_in(self):
events = HierarchicalEmitter()
session = create_session(event_hooks=events)
calls = []
handler = lambda **kwargs: calls.append(kwargs)
events.register('foo', handler)
session.emit('foo')
self.assertEqual(len(calls), 1)
示例3: TestStopProcessing
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
class TestStopProcessing(unittest.TestCase):
def setUp(self):
self.emitter = HierarchicalEmitter()
self.hook_calls = []
def hook1(self, **kwargs):
self.hook_calls.append('hook1')
def hook2(self, **kwargs):
self.hook_calls.append('hook2')
return 'hook2-response'
def hook3(self, **kwargs):
self.hook_calls.append('hook3')
return 'hook3-response'
def test_all_hooks(self):
# Here we register three hooks and sanity check
# that all three would be called by a normal emit.
# This ensures our hook calls are setup properly for
# later tests.
self.emitter.register('foo', self.hook1)
self.emitter.register('foo', self.hook2)
self.emitter.register('foo', self.hook3)
self.emitter.emit('foo')
self.assertEqual(self.hook_calls, ['hook1', 'hook2', 'hook3'])
def test_stop_processing_after_first_response(self):
# Here we register three hooks, but only the first
# two should ever execute.
self.emitter.register('foo', self.hook1)
self.emitter.register('foo', self.hook2)
self.emitter.register('foo', self.hook3)
handler, response = self.emitter.emit_until_response('foo')
self.assertEqual(response, 'hook2-response')
self.assertEqual(self.hook_calls, ['hook1', 'hook2'])
def test_no_responses(self):
# Here we register a handler that will not return a response
# and ensure we get back proper values.
self.emitter.register('foo', self.hook1)
responses = self.emitter.emit('foo')
self.assertEqual(self.hook_calls, ['hook1'])
self.assertEqual(responses, [(self.hook1, None)])
def test_no_handlers(self):
# Here we have no handlers, but still expect a tuple of return
# values.
handler, response = self.emitter.emit_until_response('foo')
self.assertIsNone(handler)
self.assertIsNone(response)
示例4: getEnvironment
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
def getEnvironment(self, profile=None):
"""Return environment variables that should be set for the profile."""
eventHooks = HierarchicalEmitter()
session = Session(event_hooks=eventHooks)
if profile:
session.set_config_variable('profile', profile)
eventHooks.register('session-initialized',
inject_assume_role_provider_cache,
unique_id='inject_assume_role_cred_provider_cache')
session.emit('session-initialized', session=session)
creds = session.get_credentials()
env = {}
def set(key, value):
if value:
env[key] = value
set('AWS_ACCESS_KEY_ID', creds.access_key)
set('AWS_SECRET_ACCESS_KEY', creds.secret_key)
# AWS_SESSION_TOKEN is the ostensibly the standard:
# http://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-environment
set('AWS_SESSION_TOKEN', creds.token)
# ...but boto expects AWS_SECURITY_TOKEN. Set both for compatibility.
# https://github.com/boto/boto/blob/b016c07d834df5bce75141c4b9d2f3d30352e1b8/boto/connection.py#L438
set('AWS_SECURITY_TOKEN', creds.token)
set('AWS_DEFAULT_REGION', session.get_config_variable('region'))
return env
示例5: TestHierarchicalEventEmitter
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
class TestHierarchicalEventEmitter(unittest.TestCase):
def setUp(self):
self.emitter = HierarchicalEmitter()
self.hook_calls = []
def hook(self, **kwargs):
self.hook_calls.append(kwargs)
def test_non_dot_behavior(self):
self.emitter.register('no-dot', self.hook)
self.emitter.emit('no-dot')
self.assertEqual(len(self.hook_calls), 1)
def test_with_dots(self):
self.emitter.register('foo.bar.baz', self.hook)
self.emitter.emit('foo.bar.baz')
self.assertEqual(len(self.hook_calls), 1)
def test_catch_all_hook(self):
self.emitter.register('foo', self.hook)
self.emitter.register('foo.bar', self.hook)
self.emitter.register('foo.bar.baz', self.hook)
self.emitter.emit('foo.bar.baz')
self.assertEqual(len(self.hook_calls), 3, self.hook_calls)
# The hook is called with the same event name three times.
self.assertEqual([e['event_name'] for e in self.hook_calls],
['foo.bar.baz', 'foo.bar.baz', 'foo.bar.baz'])
def test_hook_called_in_proper_order(self):
# We should call the hooks from most specific to least
# specific.
calls = []
self.emitter.register('foo', lambda **kwargs: calls.append('foo'))
self.emitter.register('foo.bar',
lambda **kwargs: calls.append('foo.bar'))
self.emitter.register('foo.bar.baz',
lambda **kwargs: calls.append('foo.bar.baz'))
self.emitter.emit('foo.bar.baz')
self.assertEqual(calls, ['foo.bar.baz', 'foo.bar', 'foo'])
示例6: TestWildcardHandlers
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
class TestWildcardHandlers(unittest.TestCase):
def setUp(self):
self.emitter = HierarchicalEmitter()
self.hook_calls = []
def hook(self, **kwargs):
self.hook_calls.append(kwargs)
def register(self, event_name):
func = partial(self.hook, registered_with=event_name)
self.emitter.register(event_name, func)
return func
def assert_hook_is_called_given_event(self, event):
starting = len(self.hook_calls)
self.emitter.emit(event)
after = len(self.hook_calls)
if not after > starting:
self.fail("Handler was not called for event: %s" % event)
self.assertEqual(self.hook_calls[-1]['event_name'], event)
def assert_hook_is_not_called_given_event(self, event):
starting = len(self.hook_calls)
self.emitter.emit(event)
after = len(self.hook_calls)
if not after == starting:
self.fail("Handler was called for event but was not "
"suppose to be called: %s, last_event: %s" %
(event, self.hook_calls[-1]))
def test_one_level_wildcard_handler(self):
self.emitter.register('foo.*.baz', self.hook)
# Also register for a number of other events to check
# for false positives.
self.emitter.register('other.bar.baz', self.hook)
self.emitter.register('qqq.baz', self.hook)
self.emitter.register('dont.call.me', self.hook)
self.emitter.register('dont', self.hook)
# These calls should trigger our hook.
self.assert_hook_is_called_given_event('foo.bar.baz')
self.assert_hook_is_called_given_event('foo.qux.baz')
self.assert_hook_is_called_given_event('foo.anything.baz')
# These calls should not match our hook.
self.assert_hook_is_not_called_given_event('foo')
self.assert_hook_is_not_called_given_event('foo.bar')
self.assert_hook_is_not_called_given_event('bar.qux.baz')
self.assert_hook_is_not_called_given_event('foo-bar')
def test_hierarchical_wildcard_handler(self):
self.emitter.register('foo.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.qux')
self.assert_hook_is_called_given_event('foo.bar.baz.qux.foo')
self.assert_hook_is_called_given_event('foo.qux.baz.qux')
self.assert_hook_is_called_given_event('foo.qux.baz.qux.foo')
self.assert_hook_is_not_called_given_event('bar.qux.baz.foo')
def test_multiple_wildcard_events(self):
self.emitter.register('foo.*.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.ANY.THING.baz')
self.assert_hook_is_called_given_event('foo.AT.ALL.baz')
# More specific than what we registered for.
self.assert_hook_is_called_given_event('foo.bar.baz.baz.extra')
self.assert_hook_is_called_given_event('foo.bar.baz.baz.extra.stuff')
# Too short:
self.assert_hook_is_not_called_given_event('foo')
self.assert_hook_is_not_called_given_event('foo.bar')
self.assert_hook_is_not_called_given_event('foo.bar.baz')
# Bad ending segment.
self.assert_hook_is_not_called_given_event('foo.ANY.THING.notbaz')
self.assert_hook_is_not_called_given_event('foo.ANY.THING.stillnotbaz')
def test_can_unregister_for_wildcard_events(self):
self.emitter.register('foo.*.*.baz', self.hook)
# Call multiple times to verify caching behavior.
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.emitter.unregister('foo.*.*.baz', self.hook)
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.emitter.register('foo.*.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
def test_unregister_does_not_exist(self):
self.emitter.register('foo.*.*.baz', self.hook)
self.emitter.unregister('foo.*.*.baz', self.hook)
self.emitter.unregister('foo.*.*.baz', self.hook)
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
def test_cache_cleared_properly(self):
#.........这里部分代码省略.........
示例7: Session
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
class Session(object):
"""
The Session object collects together useful functionality
from `botocore` as well as important data such as configuration
information and credentials into a single, easy-to-use object.
:ivar available_profiles: A list of profiles defined in the config
file associated with this session.
:ivar profile: The current profile.
"""
#: A default dictionary that maps the logical names for session variables
#: to the specific environment variables and configuration file names
#: that contain the values for these variables.
#: When creating a new Session object, you can pass in your own dictionary
#: to remap the logical names or to add new logical names. You can then
#: get the current value for these variables by using the
#: ``get_config_variable`` method of the :class:`botocore.session.Session`
#: class.
#: These form the keys of the dictionary. The values in the dictionary
#: are tuples of (<config_name>, <environment variable>, <default value>,
#: <conversion func>).
#: The conversion func is a function that takes the configuration value
#: as an arugment and returns the converted value. If this value is
#: None, then the configuration value is returned unmodified. This
#: conversion function that be used to type convert config values to
#: values other than the default values of strings.
#: The ``profile`` and ``config_file`` variables should always have a
#: None value for the first entry in the tuple because it doesn't make
#: sense to look inside the config file for the location of the config
#: file or for the default profile to use.
#: The ``config_name`` is the name to look for in the configuration file,
#: the ``env var`` is the OS environment variable (``os.environ``) to
#: use, and ``default_value`` is the value to use if no value is otherwise
#: found.
SESSION_VARIABLES = {
# logical: config_file, env_var, default_value, conversion_func
"profile": (None, ["AWS_DEFAULT_PROFILE", "AWS_PROFILE"], None, None),
"region": ("region", "AWS_DEFAULT_REGION", None, None),
"data_path": ("data_path", "AWS_DATA_PATH", None, None),
"config_file": (None, "AWS_CONFIG_FILE", "~/.aws/config", None),
"ca_bundle": ("ca_bundle", "AWS_CA_BUNDLE", None, None),
# This is the shared credentials file amongst sdks.
"credentials_file": (None, "AWS_SHARED_CREDENTIALS_FILE", "~/.aws/credentials", None),
# These variables only exist in the config file.
# This is the number of seconds until we time out a request to
# the instance metadata service.
"metadata_service_timeout": ("metadata_service_timeout", "AWS_METADATA_SERVICE_TIMEOUT", 1, int),
# This is the number of request attempts we make until we give
# up trying to retrieve data from the instance metadata service.
"metadata_service_num_attempts": ("metadata_service_num_attempts", "AWS_METADATA_SERVICE_NUM_ATTEMPTS", 1, int),
}
#: The default format string to use when configuring the botocore logger.
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
def __init__(self, session_vars=None, event_hooks=None, include_builtin_handlers=True, profile=None):
"""
Create a new Session object.
:type session_vars: dict
:param session_vars: A dictionary that is used to override some or all
of the environment variables associated with this session. The
key/value pairs defined in this dictionary will override the
corresponding variables defined in ``SESSION_VARIABLES``.
:type event_hooks: BaseEventHooks
:param event_hooks: The event hooks object to use. If one is not
provided, an event hooks object will be automatically created
for you.
:type include_builtin_handlers: bool
:param include_builtin_handlers: Indicates whether or not to
automatically register builtin handlers.
:type profile: str
:param profile: The name of the profile to use for this
session. Note that the profile can only be set when
the session is created.
"""
self.session_var_map = copy.copy(self.SESSION_VARIABLES)
if session_vars:
self.session_var_map.update(session_vars)
if event_hooks is None:
self._events = HierarchicalEmitter()
else:
self._events = event_hooks
if include_builtin_handlers:
self._register_builtin_handlers(self._events)
self.user_agent_name = "Botocore"
self.user_agent_version = __version__
self.user_agent_extra = ""
# The _profile attribute is just used to cache the value
# of the current profile to avoid going through the normal
# config lookup process each access time.
self._profile = None
self._config = None
self._credentials = None
self._profile_map = None
#.........这里部分代码省略.........
示例8: Session
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
#.........这里部分代码省略.........
These form the keys of the dictionary. The values in the dictionary
are tuples of (<config_name>, <environment variable>, <default value).
The ``profile`` and ``config_file`` variables should always have a
None value for the first entry in the tuple because it doesn't make
sense to look inside the config file for the location of the config
file or for the default profile to use.
The ``config_name`` is the name to look for in the configuration file,
the ``env var`` is the OS environment variable (``os.environ``) to
use, and ``default_value`` is the value to use if no value is otherwise
found.
"""
FmtString = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
def __init__(self, session_vars=None, event_hooks=None,
include_builtin_handlers=True, loader=None):
"""
Create a new Session object.
:type session_vars: dict
:param session_vars: A dictionary that is used to override some or all
of the environment variables associated with this session. The
key/value pairs defined in this dictionary will override the
corresponding variables defined in ``SessionVariables``.
:type event_hooks: BaseEventHooks
:param event_hooks: The event hooks object to use. If one is not
provided, an event hooks object will be automatically created
for you.
:type include_builtin_handlers: bool
:param include_builtin_handlers: Indicates whether or not to
automatically register builtin handlers.
"""
self.session_var_map = copy.copy(self.SessionVariables)
if session_vars:
self.session_var_map.update(session_vars)
if event_hooks is None:
self._events = HierarchicalEmitter()
else:
self._events = event_hooks
if include_builtin_handlers:
self._register_builtin_handlers(self._events)
self.user_agent_name = 'Botocore'
self.user_agent_version = __version__
self.user_agent_extra = ''
self._profile = None
self._config = None
self._credentials = None
self._profile_map = None
self._provider = None
# This is a dict that stores per session specific config variable
# overrides via set_config_variable().
self._session_instance_vars = {}
# _data_paths_added is used to track whether or not we added
# extra paths to the loader. We will do this lazily
# only when we ask for the loader.
self._data_paths_added = False
self._components = ComponentLocator()
self._register_components()
def _register_components(self):
self._register_credential_provider()
self._register_data_loader()
self._register_endpoint_resolver()
示例9: Session
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
class Session(object):
"""
The Session object collects together useful functionality
from `botocore` as well as important data such as configuration
information and credentials into a single, easy-to-use object.
:ivar available_profiles: A list of profiles defined in the config
file associated with this session.
:ivar profile: The current profile.
"""
FmtString = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
def __init__(self, env_vars=None, event_hooks=None,
include_builtin_handlers=True):
"""
Create a new Session object.
:type env_vars: dict
:param env_vars: A dictionary that is used to override some or all
of the environment variables associated with this session. The
key/value pairs defined in this dictionary will override the
corresponding variables defined in ``EnvironmentVariables``.
:type event_hooks: BaseEventHooks
:param event_hooks: The event hooks object to use. If one is not
provided, an event hooks object will be automatically created
for you.
:type include_builtin_handlers: bool
:param include_builtin_handlers: Indicates whether or not to
automatically register builtin handlers.
"""
self.env_vars = copy.copy(EnvironmentVariables)
if env_vars:
self.env_vars.update(env_vars)
if event_hooks is None:
self._events = HierarchicalEmitter()
else:
self._events = event_hooks
if include_builtin_handlers:
self._register_builtin_handlers(self._events)
self.user_agent_name = 'Botocore'
self.user_agent_version = __version__
self._profile = None
self._config = None
self._credentials = None
self._profile_map = None
self._provider = None
def _register_builtin_handlers(self, events):
for event_name, handler in handlers.BUILTIN_HANDLERS:
self.register(event_name, handler)
@property
def provider(self):
if self._provider is None:
self._provider = get_provider(self, self.get_variable('provider'))
return self._provider
@property
def available_profiles(self):
return list(self._build_profile_map().keys())
def _build_profile_map(self):
# This will build the profile map if it has not been created,
# otherwise it will return the cached value. The profile map
# is a list of profile names, to the config values for the profile.
if self._profile_map is None:
profile_map = {}
for key, values in self.full_config.items():
if key.startswith("profile"):
try:
parts = shlex.split(key)
except ValueError:
continue
if len(parts) == 2:
profile_map[parts[1]] = values
elif key == 'default':
# default section is special and is considered a profile
# name but we don't require you use 'profile "default"'
# as a section.
profile_map[key] = values
self._profile_map = profile_map
return self._profile_map
@property
def profile(self):
return self._profile
@profile.setter
def profile(self, profile):
# Since provider can be specified in profile, changing the
# profile should reset the provider.
self._provider = None
self._profile = profile
def get_variable(self, logical_name, methods=('env', 'config')):
"""
Retrieve the value associated with the specified logical_name
#.........这里部分代码省略.........
示例10: TestWildcardHandlers
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
class TestWildcardHandlers(unittest.TestCase):
def setUp(self):
self.emitter = HierarchicalEmitter()
self.hook_calls = []
def hook(self, **kwargs):
self.hook_calls.append(kwargs)
def register(self, event_name):
func = partial(self.hook, registered_with=event_name)
self.emitter.register(event_name, func)
return func
def assert_hook_is_called_given_event(self, event):
starting = len(self.hook_calls)
self.emitter.emit(event)
after = len(self.hook_calls)
if not after > starting:
self.fail("Handler was not called for event: %s" % event)
self.assertEqual(self.hook_calls[-1]['event_name'], event)
def assert_hook_is_not_called_given_event(self, event):
starting = len(self.hook_calls)
self.emitter.emit(event)
after = len(self.hook_calls)
if not after == starting:
self.fail("Handler was called for event but was not "
"suppose to be called: %s, last_event: %s" %
(event, self.hook_calls[-1]))
def test_one_level_wildcard_handler(self):
self.emitter.register('foo.*.baz', self.hook)
# Also register for a number of other events to check
# for false positives.
self.emitter.register('other.bar.baz', self.hook)
self.emitter.register('qqq.baz', self.hook)
self.emitter.register('dont.call.me', self.hook)
self.emitter.register('dont', self.hook)
# These calls should trigger our hook.
self.assert_hook_is_called_given_event('foo.bar.baz')
self.assert_hook_is_called_given_event('foo.qux.baz')
self.assert_hook_is_called_given_event('foo.anything.baz')
# These calls should not match our hook.
self.assert_hook_is_not_called_given_event('foo')
self.assert_hook_is_not_called_given_event('foo.bar')
self.assert_hook_is_not_called_given_event('bar.qux.baz')
self.assert_hook_is_not_called_given_event('foo-bar')
def test_hierarchical_wildcard_handler(self):
self.emitter.register('foo.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.qux')
self.assert_hook_is_called_given_event('foo.bar.baz.qux.foo')
self.assert_hook_is_called_given_event('foo.qux.baz.qux')
self.assert_hook_is_called_given_event('foo.qux.baz.qux.foo')
self.assert_hook_is_not_called_given_event('bar.qux.baz.foo')
def test_multiple_wildcard_events(self):
self.emitter.register('foo.*.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.ANY.THING.baz')
self.assert_hook_is_called_given_event('foo.AT.ALL.baz')
# More specific than what we registered for.
self.assert_hook_is_called_given_event('foo.bar.baz.baz.extra')
self.assert_hook_is_called_given_event('foo.bar.baz.baz.extra.stuff')
# Too short:
self.assert_hook_is_not_called_given_event('foo')
self.assert_hook_is_not_called_given_event('foo.bar')
self.assert_hook_is_not_called_given_event('foo.bar.baz')
# Bad ending segment.
self.assert_hook_is_not_called_given_event('foo.ANY.THING.notbaz')
self.assert_hook_is_not_called_given_event('foo.ANY.THING.stillnotbaz')
def test_can_unregister_for_wildcard_events(self):
self.emitter.register('foo.*.*.baz', self.hook)
# Call multiple times to verify caching behavior.
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.emitter.unregister('foo.*.*.baz', self.hook)
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.emitter.register('foo.*.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
def test_unregister_does_not_exist(self):
self.emitter.register('foo.*.*.baz', self.hook)
self.emitter.unregister('foo.*.*.baz', self.hook)
self.emitter.unregister('foo.*.*.baz', self.hook)
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
def test_cache_cleared_properly(self):
#.........这里部分代码省略.........
示例11: Session
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
#.........这里部分代码省略.........
These form the keys of the dictionary. The values in the dictionary
are tuples of (<config_name>, <environment variable>, <default value).
The ``profile`` and ``config_file`` variables should always have a
None value for the first entry in the tuple because it doesn't make
sense to look inside the config file for the location of the config
file or for the default profile to use.
The ``config_name`` is the name to look for in the configuration file,
the ``env var`` is the OS environment variable (``os.environ``) to
use, and ``default_value`` is the value to use if no value is otherwise
found.
"""
FmtString = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
def __init__(self, session_vars=None, event_hooks=None,
include_builtin_handlers=True, loader=None):
"""
Create a new Session object.
:type session_vars: dict
:param session_vars: A dictionary that is used to override some or all
of the environment variables associated with this session. The
key/value pairs defined in this dictionary will override the
corresponding variables defined in ``SessionVariables``.
:type event_hooks: BaseEventHooks
:param event_hooks: The event hooks object to use. If one is not
provided, an event hooks object will be automatically created
for you.
:type include_builtin_handlers: bool
:param include_builtin_handlers: Indicates whether or not to
automatically register builtin handlers.
"""
self.session_var_map = copy.copy(self.SessionVariables)
if session_vars:
self.session_var_map.update(session_vars)
if event_hooks is None:
self._events = HierarchicalEmitter()
else:
self._events = event_hooks
if include_builtin_handlers:
self._register_builtin_handlers(self._events)
self.user_agent_name = 'Botocore'
self.user_agent_version = __version__
self.user_agent_extra = ''
self._profile = None
self._config = None
self._credentials = None
self._profile_map = None
self._provider = None
# This is a dict that stores per session specific config variable
# overrides via set_config_variable().
self._session_instance_vars = {}
if loader is None:
loader = Loader()
self._loader = loader
# _data_paths_added is used to track whether or not we added
# extra paths to the loader. We will do this lazily
# only when we ask for the loader.
self._data_paths_added = False
@property
def loader(self):
if not self._data_paths_added:
示例12: TestWildcardHandlers
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
class TestWildcardHandlers(unittest.TestCase):
def setUp(self):
self.emitter = HierarchicalEmitter()
self.hook_calls = []
def hook(self, **kwargs):
self.hook_calls.append(kwargs)
def register(self, event_name):
func = partial(self.hook, registered_with=event_name)
self.emitter.register(event_name, func)
return func
def assert_hook_is_called_given_event(self, event):
starting = len(self.hook_calls)
self.emitter.emit(event)
after = len(self.hook_calls)
if not after > starting:
self.fail("Handler was not called for event: %s" % event)
self.assertEqual(self.hook_calls[-1]['event_name'], event)
def assert_hook_is_not_called_given_event(self, event):
starting = len(self.hook_calls)
self.emitter.emit(event)
after = len(self.hook_calls)
if not after == starting:
self.fail("Handler was called for event but was not "
"suppose to be called: %s, last_event: %s" %
(event, self.hook_calls[-1]))
def test_one_level_wildcard_handler(self):
self.emitter.register('foo.*.baz', self.hook)
# Also register for a number of other events to check
# for false positives.
self.emitter.register('other.bar.baz', self.hook)
self.emitter.register('qqq.baz', self.hook)
self.emitter.register('dont.call.me', self.hook)
self.emitter.register('dont', self.hook)
# These calls should trigger our hook.
self.assert_hook_is_called_given_event('foo.bar.baz')
self.assert_hook_is_called_given_event('foo.qux.baz')
self.assert_hook_is_called_given_event('foo.anything.baz')
# These calls should not match our hook.
self.assert_hook_is_not_called_given_event('foo')
self.assert_hook_is_not_called_given_event('foo.bar')
self.assert_hook_is_not_called_given_event('bar.qux.baz')
self.assert_hook_is_not_called_given_event('foo-bar')
def test_hierarchical_wildcard_handler(self):
self.emitter.register('foo.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.qux')
self.assert_hook_is_called_given_event('foo.bar.baz.qux.foo')
self.assert_hook_is_called_given_event('foo.qux.baz.qux')
self.assert_hook_is_called_given_event('foo.qux.baz.qux.foo')
self.assert_hook_is_not_called_given_event('bar.qux.baz.foo')
def test_multiple_wildcard_events(self):
self.emitter.register('foo.*.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.ANY.THING.baz')
self.assert_hook_is_called_given_event('foo.AT.ALL.baz')
# More specific than what we registered for.
self.assert_hook_is_called_given_event('foo.bar.baz.baz.extra')
self.assert_hook_is_called_given_event('foo.bar.baz.baz.extra.stuff')
# Too short:
self.assert_hook_is_not_called_given_event('foo')
self.assert_hook_is_not_called_given_event('foo.bar')
self.assert_hook_is_not_called_given_event('foo.bar.baz')
# Bad ending segment.
self.assert_hook_is_not_called_given_event('foo.ANY.THING.notbaz')
self.assert_hook_is_not_called_given_event('foo.ANY.THING.stillnotbaz')
def test_can_unregister_for_wildcard_events(self):
self.emitter.register('foo.*.*.baz', self.hook)
# Call multiple times to verify caching behavior.
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.emitter.unregister('foo.*.*.baz', self.hook)
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.emitter.register('foo.*.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
def test_unregister_does_not_exist(self):
self.emitter.register('foo.*.*.baz', self.hook)
self.emitter.unregister('foo.*.*.baz', self.hook)
self.emitter.unregister('foo.*.*.baz', self.hook)
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
def test_cache_cleared_properly(self):
#.........这里部分代码省略.........
示例13: TestWildcardHandlers
# 需要导入模块: from botocore.hooks import HierarchicalEmitter [as 别名]
# 或者: from botocore.hooks.HierarchicalEmitter import register [as 别名]
class TestWildcardHandlers(unittest.TestCase):
def setUp(self):
self.emitter = HierarchicalEmitter()
self.hook_calls = []
def hook(self, **kwargs):
self.hook_calls.append(kwargs)
def register(self, event_name):
func = partial(self.hook, registered_with=event_name)
self.emitter.register(event_name, func)
return func
def assert_hook_is_called_given_event(self, event):
starting = len(self.hook_calls)
self.emitter.emit(event)
after = len(self.hook_calls)
if not after > starting:
self.fail("Handler was not called for event: %s" % event)
self.assertEqual(self.hook_calls[-1]['event_name'], event)
def assert_hook_is_not_called_given_event(self, event):
starting = len(self.hook_calls)
self.emitter.emit(event)
after = len(self.hook_calls)
if not after == starting:
self.fail("Handler was called for event but was not "
"suppose to be called: %s, last_event: %s" %
(event, self.hook_calls[-1]))
def test_one_level_wildcard_handler(self):
self.emitter.register('foo.*.baz', self.hook)
# Also register for a number of other events to check
# for false positives.
self.emitter.register('other.bar.baz', self.hook)
self.emitter.register('qqq.baz', self.hook)
self.emitter.register('dont.call.me', self.hook)
self.emitter.register('dont', self.hook)
# These calls should trigger our hook.
self.assert_hook_is_called_given_event('foo.bar.baz')
self.assert_hook_is_called_given_event('foo.qux.baz')
self.assert_hook_is_called_given_event('foo.anything.baz')
# These calls should not match our hook.
self.assert_hook_is_not_called_given_event('foo')
self.assert_hook_is_not_called_given_event('foo.bar')
self.assert_hook_is_not_called_given_event('bar.qux.baz')
self.assert_hook_is_not_called_given_event('foo-bar')
def test_hierarchical_wildcard_handler(self):
self.emitter.register('foo.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.qux')
self.assert_hook_is_called_given_event('foo.bar.baz.qux.foo')
self.assert_hook_is_called_given_event('foo.qux.baz.qux')
self.assert_hook_is_called_given_event('foo.qux.baz.qux.foo')
self.assert_hook_is_not_called_given_event('bar.qux.baz.foo')
def test_multiple_wildcard_events(self):
self.emitter.register('foo.*.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.ANY.THING.baz')
self.assert_hook_is_called_given_event('foo.AT.ALL.baz')
# More specific than what we registered for.
self.assert_hook_is_called_given_event('foo.bar.baz.baz.extra')
self.assert_hook_is_called_given_event('foo.bar.baz.baz.extra.stuff')
# Too short:
self.assert_hook_is_not_called_given_event('foo')
self.assert_hook_is_not_called_given_event('foo.bar')
self.assert_hook_is_not_called_given_event('foo.bar.baz')
# Bad ending segment.
self.assert_hook_is_not_called_given_event('foo.ANY.THING.notbaz')
self.assert_hook_is_not_called_given_event('foo.ANY.THING.stillnotbaz')
def test_can_unregister_for_wildcard_events(self):
self.emitter.register('foo.*.*.baz', self.hook)
# Call multiple times to verify caching behavior.
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.emitter.unregister('foo.*.*.baz', self.hook)
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
self.emitter.register('foo.*.*.baz', self.hook)
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
self.assert_hook_is_called_given_event('foo.bar.baz.baz')
def test_unregister_does_not_exist(self):
self.emitter.register('foo.*.*.baz', self.hook)
self.emitter.unregister('foo.*.*.baz', self.hook)
self.emitter.unregister('foo.*.*.baz', self.hook)
self.assert_hook_is_not_called_given_event('foo.bar.baz.baz')
def test_cache_cleared_properly(self):
#.........这里部分代码省略.........