2021-11-30 09:12:51 +01:00
|
|
|
import sys
|
|
|
|
import os
|
2021-12-08 16:45:07 +01:00
|
|
|
import warnings
|
2021-11-30 09:12:51 +01:00
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
from itertools import groupby
|
2019-09-01 11:45:47 +02:00
|
|
|
from operator import attrgetter
|
2022-04-14 17:11:12 +02:00
|
|
|
from typing import Dict, List, Type, Iterator, Iterable, Optional, ContextManager
|
2021-11-30 09:12:51 +01:00
|
|
|
from pathlib import Path
|
2022-01-14 17:49:05 +01:00
|
|
|
from contextlib import contextmanager, nullcontext
|
2019-08-31 18:33:54 +02:00
|
|
|
|
2021-11-30 09:12:51 +01:00
|
|
|
from ..compat import importlib_metadata, find_entry_points, get_dist_name
|
2019-09-01 11:13:45 +02:00
|
|
|
|
2022-04-14 17:11:12 +02:00
|
|
|
from ..utils import repr_dict, get_site_paths
|
2021-11-30 09:12:51 +01:00
|
|
|
from . import AuthPlugin, ConverterPlugin, FormatterPlugin, TransportPlugin
|
|
|
|
from .base import BasePlugin
|
2013-09-21 23:46:15 +02:00
|
|
|
|
|
|
|
|
2021-11-30 09:12:51 +01:00
|
|
|
ENTRY_POINT_CLASSES = {
|
|
|
|
'httpie.plugins.auth.v1': AuthPlugin,
|
|
|
|
'httpie.plugins.converter.v1': ConverterPlugin,
|
|
|
|
'httpie.plugins.formatter.v1': FormatterPlugin,
|
|
|
|
'httpie.plugins.transport.v1': TransportPlugin
|
|
|
|
}
|
|
|
|
ENTRY_POINT_NAMES = list(ENTRY_POINT_CLASSES.keys())
|
2013-09-21 23:46:15 +02:00
|
|
|
|
|
|
|
|
2021-11-30 09:12:51 +01:00
|
|
|
@contextmanager
|
2022-04-14 17:11:12 +02:00
|
|
|
def _load_directories(site_dirs: Iterable[Path]) -> Iterator[None]:
|
|
|
|
plugin_dirs = [
|
|
|
|
os.fspath(site_dir)
|
|
|
|
for site_dir in site_dirs
|
|
|
|
]
|
|
|
|
sys.path.extend(plugin_dirs)
|
2021-11-30 09:12:51 +01:00
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
2022-04-14 17:11:12 +02:00
|
|
|
for plugin_dir in plugin_dirs:
|
|
|
|
sys.path.remove(plugin_dir)
|
2021-11-30 09:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def enable_plugins(plugins_dir: Optional[Path]) -> ContextManager[None]:
|
|
|
|
if plugins_dir is None:
|
|
|
|
return nullcontext()
|
|
|
|
else:
|
2022-04-14 17:11:12 +02:00
|
|
|
return _load_directories(get_site_paths(plugins_dir))
|
2021-11-30 09:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PluginManager(list):
|
2019-08-31 18:33:54 +02:00
|
|
|
def register(self, *plugins: Type[BasePlugin]):
|
2014-04-28 23:33:30 +02:00
|
|
|
for plugin in plugins:
|
2019-09-01 11:13:45 +02:00
|
|
|
self.append(plugin)
|
2013-09-21 23:46:15 +02:00
|
|
|
|
2019-08-31 18:35:24 +02:00
|
|
|
def unregister(self, plugin: Type[BasePlugin]):
|
2019-09-01 11:13:45 +02:00
|
|
|
self.remove(plugin)
|
|
|
|
|
|
|
|
def filter(self, by_type=Type[BasePlugin]):
|
|
|
|
return [plugin for plugin in self if issubclass(plugin, by_type)]
|
2016-11-23 22:01:58 +01:00
|
|
|
|
2021-11-30 09:12:51 +01:00
|
|
|
def iter_entry_points(self, directory: Optional[Path] = None):
|
|
|
|
with enable_plugins(directory):
|
|
|
|
eps = importlib_metadata.entry_points()
|
|
|
|
|
|
|
|
for entry_point_name in ENTRY_POINT_NAMES:
|
|
|
|
yield from find_entry_points(eps, group=entry_point_name)
|
|
|
|
|
|
|
|
def load_installed_plugins(self, directory: Optional[Path] = None):
|
|
|
|
for entry_point in self.iter_entry_points(directory):
|
2021-12-08 16:45:07 +01:00
|
|
|
plugin_name = get_dist_name(entry_point)
|
|
|
|
try:
|
|
|
|
plugin = entry_point.load()
|
|
|
|
except BaseException as exc:
|
|
|
|
warnings.warn(
|
2022-04-16 01:06:34 +02:00
|
|
|
f'While loading "{plugin_name}", an error occurred: {exc}\n'
|
2021-12-08 16:45:07 +01:00
|
|
|
f'For uninstallations, please use either "httpie plugins uninstall {plugin_name}" '
|
|
|
|
f'or "pip uninstall {plugin_name}" (depending on how you installed it in the first '
|
|
|
|
'place).'
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
plugin.package_name = plugin_name
|
|
|
|
self.register(plugin)
|
2014-04-28 23:33:30 +02:00
|
|
|
|
|
|
|
# Auth
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_auth_plugins(self) -> List[Type[AuthPlugin]]:
|
2019-09-01 11:13:45 +02:00
|
|
|
return self.filter(AuthPlugin)
|
2013-09-21 23:46:15 +02:00
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_auth_plugin_mapping(self) -> Dict[str, Type[AuthPlugin]]:
|
|
|
|
return {
|
|
|
|
plugin.auth_type: plugin for plugin in self.get_auth_plugins()
|
|
|
|
}
|
2013-09-21 23:46:15 +02:00
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_auth_plugin(self, auth_type: str) -> Type[AuthPlugin]:
|
2013-09-21 23:46:15 +02:00
|
|
|
return self.get_auth_plugin_mapping()[auth_type]
|
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
# Output processing
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_formatters(self) -> List[Type[FormatterPlugin]]:
|
2019-09-01 11:13:45 +02:00
|
|
|
return self.filter(FormatterPlugin)
|
2014-04-28 23:33:30 +02:00
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_formatters_grouped(self) -> Dict[str, List[Type[FormatterPlugin]]]:
|
2019-09-01 11:45:47 +02:00
|
|
|
return {
|
|
|
|
group_name: list(group)
|
|
|
|
for group_name, group
|
|
|
|
in groupby(self.get_formatters(), key=attrgetter('group_name'))
|
|
|
|
}
|
2014-04-28 23:33:30 +02:00
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_converters(self) -> List[Type[ConverterPlugin]]:
|
2019-09-01 11:13:45 +02:00
|
|
|
return self.filter(ConverterPlugin)
|
2015-02-05 15:25:00 +01:00
|
|
|
|
|
|
|
# Adapters
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_transport_plugins(self) -> List[Type[TransportPlugin]]:
|
2019-09-01 11:13:45 +02:00
|
|
|
return self.filter(TransportPlugin)
|
|
|
|
|
2021-09-23 17:15:14 +02:00
|
|
|
def __str__(self):
|
|
|
|
return repr_dict({
|
|
|
|
'adapters': self.get_transport_plugins(),
|
|
|
|
'auth': self.get_auth_plugins(),
|
|
|
|
'converters': self.get_converters(),
|
|
|
|
'formatters': self.get_formatters(),
|
|
|
|
})
|
|
|
|
|
2019-09-01 11:13:45 +02:00
|
|
|
def __repr__(self):
|
2021-09-23 17:15:14 +02:00
|
|
|
return f'<{type(self).__name__} {self}>'
|