Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/plugin.py
1566 views
1
# Copyright 2012-2013 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
import logging
14
15
from botocore.hooks import HierarchicalEmitter
16
17
log = logging.getLogger('awscli.plugin')
18
19
BUILTIN_PLUGINS = {'__builtin__': 'awscli.handlers'}
20
21
22
def load_plugins(plugin_mapping, event_hooks=None, include_builtins=True):
23
"""
24
25
:type plugin_mapping: dict
26
:param plugin_mapping: A dict of plugin name to import path,
27
e.g. ``{"plugingName": "package.modulefoo"}``.
28
29
:type event_hooks: ``EventHooks``
30
:param event_hooks: Event hook emitter. If one if not provided,
31
an emitter will be created and returned. Otherwise, the
32
passed in ``event_hooks`` will be used to initialize plugins.
33
34
:type include_builtins: bool
35
:param include_builtins: If True, the builtin awscli plugins (specified in
36
``BUILTIN_PLUGINS``) will be included in the list of plugins to load.
37
38
:rtype: HierarchicalEmitter
39
:return: An event emitter object.
40
41
"""
42
if include_builtins:
43
plugin_mapping.update(BUILTIN_PLUGINS)
44
modules = _import_plugins(plugin_mapping)
45
if event_hooks is None:
46
event_hooks = HierarchicalEmitter()
47
for name, plugin in zip(plugin_mapping.keys(), modules):
48
log.debug("Initializing plugin %s: %s", name, plugin)
49
plugin.awscli_initialize(event_hooks)
50
return event_hooks
51
52
53
def _import_plugins(plugin_names):
54
plugins = []
55
for name, path in plugin_names.items():
56
log.debug("Importing plugin %s: %s", name, path)
57
if '.' not in path:
58
plugins.append(__import__(path))
59
else:
60
package, module = path.rsplit('.', 1)
61
module = __import__(path, fromlist=[module])
62
plugins.append(module)
63
return plugins
64
65