122 lines
2.8 KiB
Plaintext
122 lines
2.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Test notebook for discovering and importing plugins"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'amarillo.plugins.metrics': <module 'amarillo.plugins.metrics' from '/home/user/amarillo/amarillo-plugins/amarillo-metrics/amarillo/plugins/metrics/__init__.py'>}"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import importlib\n",
|
|
"import pkgutil\n",
|
|
"\n",
|
|
"import amarillo.plugins # FIXME this namespace does not exist if there are 0 plugins installed\n",
|
|
"\n",
|
|
"def iter_namespace(ns_pkg):\n",
|
|
" # Source: https://packaging.python.org/guides/creating-and-discovering-plugins/\n",
|
|
" return pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + \".\")\n",
|
|
"\n",
|
|
"discovered_plugins = {\n",
|
|
" name: importlib.import_module(name)\n",
|
|
" for finder, name, ispkg\n",
|
|
" in iter_namespace(amarillo.plugins)\n",
|
|
"}\n",
|
|
"\n",
|
|
"discovered_plugins"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"['__name__',\n",
|
|
" '__doc__',\n",
|
|
" '__package__',\n",
|
|
" '__loader__',\n",
|
|
" '__spec__',\n",
|
|
" '__path__',\n",
|
|
" '__file__',\n",
|
|
" '__cached__',\n",
|
|
" '__builtins__',\n",
|
|
" 'metrics',\n",
|
|
" 'json',\n",
|
|
" 'logging',\n",
|
|
" 'os',\n",
|
|
" 'random',\n",
|
|
" 'Callable',\n",
|
|
" 'APIRouter',\n",
|
|
" 'HTTPException',\n",
|
|
" 'Depends',\n",
|
|
" 'Request',\n",
|
|
" 'datetime',\n",
|
|
" 'generate_latest',\n",
|
|
" 'Gauge',\n",
|
|
" 'Counter',\n",
|
|
" 'Info',\n",
|
|
" 'FastAPI',\n",
|
|
" 'HTTPBasic',\n",
|
|
" 'HTTPBasicCredentials',\n",
|
|
" 'PlainTextResponse',\n",
|
|
" 'secrets',\n",
|
|
" 'logger',\n",
|
|
" 'security',\n",
|
|
" 'amarillo_trips_number_total',\n",
|
|
" 'router']"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"metrics = discovered_plugins['amarillo.plugins.metrics']\n",
|
|
"\n",
|
|
"metrics.__dir__()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": ".venv",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|