2014-04-28 23:33:30 +02:00
|
|
|
from itertools import groupby
|
2019-09-01 11:45:47 +02:00
|
|
|
from operator import attrgetter
|
2019-09-01 11:13:45 +02:00
|
|
|
from typing import Dict, List, Type
|
2019-08-31 18:33:54 +02:00
|
|
|
|
2013-09-21 23:46:15 +02:00
|
|
|
from pkg_resources import iter_entry_points
|
2019-09-01 11:13:45 +02:00
|
|
|
|
2021-05-05 14:13:39 +02:00
|
|
|
from . import AuthPlugin, ConverterPlugin, FormatterPlugin
|
|
|
|
from .base import BasePlugin, TransportPlugin
|
2013-09-21 23:46:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
ENTRY_POINT_NAMES = [
|
2014-04-28 23:33:30 +02:00
|
|
|
'httpie.plugins.auth.v1',
|
|
|
|
'httpie.plugins.formatter.v1',
|
|
|
|
'httpie.plugins.converter.v1',
|
2015-02-05 15:25:00 +01:00
|
|
|
'httpie.plugins.transport.v1',
|
2013-09-21 23:46:15 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-09-01 11:13:45 +02:00
|
|
|
class PluginManager(list):
|
2013-09-21 23:46:15 +02:00
|
|
|
|
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
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
def load_installed_plugins(self):
|
|
|
|
for entry_point_name in ENTRY_POINT_NAMES:
|
|
|
|
for entry_point in iter_entry_points(entry_point_name):
|
|
|
|
plugin = entry_point.load()
|
|
|
|
plugin.package_name = entry_point.dist.key
|
|
|
|
self.register(entry_point.load())
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'<PluginManager: {list(self)}>'
|