2014-04-28 23:33:30 +02:00
|
|
|
from itertools import groupby
|
2013-09-21 23:46:15 +02:00
|
|
|
from pkg_resources import iter_entry_points
|
2014-04-28 23:33:30 +02:00
|
|
|
from httpie.plugins import AuthPlugin, FormatterPlugin, ConverterPlugin
|
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',
|
2013-09-21 23:46:15 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class PluginManager(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._plugins = []
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._plugins)
|
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
def register(self, *plugins):
|
|
|
|
for plugin in plugins:
|
|
|
|
self._plugins.append(plugin)
|
2013-09-21 23:46:15 +02: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
|
2013-09-21 23:46:15 +02:00
|
|
|
def get_auth_plugins(self):
|
2014-04-28 23:33:30 +02:00
|
|
|
return [plugin for plugin in self if issubclass(plugin, AuthPlugin)]
|
2013-09-21 23:46:15 +02:00
|
|
|
|
|
|
|
def get_auth_plugin_mapping(self):
|
2014-04-28 23:33:30 +02:00
|
|
|
return dict((plugin.auth_type, plugin)
|
|
|
|
for plugin in self.get_auth_plugins())
|
2013-09-21 23:46:15 +02:00
|
|
|
|
|
|
|
def get_auth_plugin(self, auth_type):
|
|
|
|
return self.get_auth_plugin_mapping()[auth_type]
|
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
# Output processing
|
|
|
|
def get_formatters(self):
|
|
|
|
return [plugin for plugin in self
|
|
|
|
if issubclass(plugin, FormatterPlugin)]
|
|
|
|
|
|
|
|
def get_formatters_grouped(self):
|
|
|
|
groups = {}
|
|
|
|
for group_name, group in groupby(
|
|
|
|
self.get_formatters(),
|
|
|
|
key=lambda p: getattr(p, 'group_name', 'format')):
|
|
|
|
groups[group_name] = list(group)
|
|
|
|
return groups
|
|
|
|
|
|
|
|
def get_converters(self):
|
|
|
|
return [plugin for plugin in self
|
|
|
|
if issubclass(plugin, ConverterPlugin)]
|