forked from extern/httpie-cli
Refactor PluginManager
This commit is contained in:
parent
30624e66ec
commit
a34b3d9d87
@ -1,9 +1,10 @@
|
|||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
from typing import Type, Iterable, List, Dict
|
from typing import Dict, List, Type
|
||||||
|
|
||||||
from pkg_resources import iter_entry_points
|
from pkg_resources import iter_entry_points
|
||||||
from httpie.plugins import AuthPlugin, FormatterPlugin, ConverterPlugin
|
|
||||||
from httpie.plugins.base import TransportPlugin, BasePlugin
|
from httpie.plugins import AuthPlugin, ConverterPlugin, FormatterPlugin
|
||||||
|
from httpie.plugins.base import BasePlugin, TransportPlugin
|
||||||
|
|
||||||
|
|
||||||
ENTRY_POINT_NAMES = [
|
ENTRY_POINT_NAMES = [
|
||||||
@ -14,20 +15,17 @@ ENTRY_POINT_NAMES = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class PluginManager:
|
class PluginManager(list):
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self._plugins = []
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return iter(self._plugins)
|
|
||||||
|
|
||||||
def register(self, *plugins: Type[BasePlugin]):
|
def register(self, *plugins: Type[BasePlugin]):
|
||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
self._plugins.append(plugin)
|
self.append(plugin)
|
||||||
|
|
||||||
def unregister(self, plugin: Type[BasePlugin]):
|
def unregister(self, plugin: Type[BasePlugin]):
|
||||||
self._plugins.remove(plugin)
|
self.remove(plugin)
|
||||||
|
|
||||||
|
def filter(self, by_type=Type[BasePlugin]):
|
||||||
|
return [plugin for plugin in self if issubclass(plugin, by_type)]
|
||||||
|
|
||||||
def load_installed_plugins(self):
|
def load_installed_plugins(self):
|
||||||
for entry_point_name in ENTRY_POINT_NAMES:
|
for entry_point_name in ENTRY_POINT_NAMES:
|
||||||
@ -38,7 +36,7 @@ class PluginManager:
|
|||||||
|
|
||||||
# Auth
|
# Auth
|
||||||
def get_auth_plugins(self) -> List[Type[AuthPlugin]]:
|
def get_auth_plugins(self) -> List[Type[AuthPlugin]]:
|
||||||
return [plugin for plugin in self if issubclass(plugin, AuthPlugin)]
|
return self.filter(AuthPlugin)
|
||||||
|
|
||||||
def get_auth_plugin_mapping(self) -> Dict[str, Type[AuthPlugin]]:
|
def get_auth_plugin_mapping(self) -> Dict[str, Type[AuthPlugin]]:
|
||||||
return {
|
return {
|
||||||
@ -50,28 +48,22 @@ class PluginManager:
|
|||||||
|
|
||||||
# Output processing
|
# Output processing
|
||||||
def get_formatters(self) -> List[Type[FormatterPlugin]]:
|
def get_formatters(self) -> List[Type[FormatterPlugin]]:
|
||||||
return [
|
return self.filter(FormatterPlugin)
|
||||||
plugin for plugin in self
|
|
||||||
if issubclass(plugin, FormatterPlugin)
|
|
||||||
]
|
|
||||||
|
|
||||||
def get_formatters_grouped(self) -> Dict[str, List[Type[FormatterPlugin]]]:
|
def get_formatters_grouped(self) -> Dict[str, List[Type[FormatterPlugin]]]:
|
||||||
groups = {}
|
groups = {}
|
||||||
for group_name, group in groupby(
|
for group_name, group in groupby(
|
||||||
self.get_formatters(),
|
self.get_formatters(),
|
||||||
key=lambda p: getattr(p, 'group_name', 'format')):
|
key=lambda p: getattr(p, 'group_name', 'format')):
|
||||||
groups[group_name] = list(group)
|
groups[group_name] = list(group)
|
||||||
return groups
|
return groups
|
||||||
|
|
||||||
def get_converters(self) -> List[Type[ConverterPlugin]]:
|
def get_converters(self) -> List[Type[ConverterPlugin]]:
|
||||||
return [
|
return self.filter(ConverterPlugin)
|
||||||
plugin for plugin in self
|
|
||||||
if issubclass(plugin, ConverterPlugin)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Adapters
|
# Adapters
|
||||||
def get_transport_plugins(self) -> List[Type[TransportPlugin]]:
|
def get_transport_plugins(self) -> List[Type[TransportPlugin]]:
|
||||||
return [
|
return self.filter(TransportPlugin)
|
||||||
plugin for plugin in self
|
|
||||||
if issubclass(plugin, TransportPlugin)
|
def __repr__(self):
|
||||||
]
|
return f'<PluginManager: {list(self)}>'
|
||||||
|
Loading…
Reference in New Issue
Block a user