|
| 1 | +# Copyright 2025 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 | +""" |
| 14 | +NOTE: This module is considered private and is subject to abrupt breaking |
| 15 | +changes without prior announcement. Please do not use it directly. |
| 16 | +""" |
| 17 | + |
| 18 | +import importlib |
| 19 | +import logging |
| 20 | +import os |
| 21 | +from contextvars import ContextVar |
| 22 | +from dataclasses import dataclass |
| 23 | +from typing import Optional |
| 24 | + |
| 25 | +log = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class PluginContext: |
| 30 | + """ |
| 31 | + Encapsulation of plugins tracked within the `_plugin_context` context variable. |
| 32 | + """ |
| 33 | + |
| 34 | + plugins: Optional[str] = None |
| 35 | + |
| 36 | + |
| 37 | +_plugin_context = ContextVar("_plugin_context") |
| 38 | + |
| 39 | + |
| 40 | +def get_plugin_context(): |
| 41 | + """Get the current `_plugin_context` context variable if set, else None.""" |
| 42 | + return _plugin_context.get(None) |
| 43 | + |
| 44 | + |
| 45 | +def set_plugin_context(ctx): |
| 46 | + """Set the current `_plugin_context` context variable.""" |
| 47 | + token = _plugin_context.set(ctx) |
| 48 | + return token |
| 49 | + |
| 50 | + |
| 51 | +def reset_plugin_context(token): |
| 52 | + """Reset the current `_plugin_context` context variable.""" |
| 53 | + _plugin_context.reset(token) |
| 54 | + |
| 55 | + |
| 56 | +def get_botocore_plugins(): |
| 57 | + context = get_plugin_context() |
| 58 | + if context is not None: |
| 59 | + plugins = context.plugins |
| 60 | + if plugins is None: |
| 61 | + context.plugins = os.environ.get('BOTOCORE_EXPERIMENTAL__PLUGINS') |
| 62 | + else: |
| 63 | + return plugins |
| 64 | + return os.environ.get('BOTOCORE_EXPERIMENTAL__PLUGINS') |
| 65 | + |
| 66 | + |
| 67 | +def load_client_plugins(client, plugins): |
| 68 | + for plugin_name, module_name in plugins.items(): |
| 69 | + log.debug( |
| 70 | + "Importing client plugin %s from module %s", |
| 71 | + plugin_name, |
| 72 | + module_name, |
| 73 | + ) |
| 74 | + try: |
| 75 | + module = importlib.import_module(module_name) |
| 76 | + module.initialize_client_plugin(client) |
| 77 | + except ModuleNotFoundError: |
| 78 | + log.debug( |
| 79 | + "Failed to locate the following plugin module: %s.", |
| 80 | + plugin_name, |
| 81 | + ) |
| 82 | + except Exception as e: |
| 83 | + log.debug( |
| 84 | + "Error raised during the loading of %s: %s", plugin_name, e |
| 85 | + ) |
0 commit comments