From cd085cbc0d8ce5fd659ccc01996551dcfc149b13 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Tue, 26 May 2020 10:07:34 +0200 Subject: [PATCH] Refactor built-in plugin registry to avoid circular imports Fix #925 --- httpie/cli/argparser.py | 2 +- httpie/cli/definition.py | 2 +- httpie/client.py | 2 +- httpie/core.py | 3 ++- httpie/output/processing.py | 3 ++- httpie/plugins/__init__.py | 15 --------------- httpie/plugins/registry.py | 18 ++++++++++++++++++ httpie/sessions.py | 2 +- tests/test_auth_plugins.py | 4 +++- 9 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 httpie/plugins/registry.py diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index 835df376..87e6a1bf 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -18,7 +18,7 @@ from httpie.cli.constants import ( from httpie.cli.exceptions import ParseError from httpie.cli.requestitems import RequestItems from httpie.context import Environment -from httpie.plugins import plugin_manager +from httpie.plugins.registry import plugin_manager from httpie.utils import ExplicitNullAuth, get_content_type diff --git a/httpie/cli/definition.py b/httpie/cli/definition.py index bcd8dbc5..23b59106 100644 --- a/httpie/cli/definition.py +++ b/httpie/cli/definition.py @@ -18,7 +18,7 @@ from httpie.cli.constants import ( from httpie.output.formatters.colors import ( AUTO_STYLE, AVAILABLE_STYLES, DEFAULT_STYLE, ) -from httpie.plugins import plugin_manager +from httpie.plugins.registry import plugin_manager from httpie.plugins.builtin import BuiltinAuthPlugin from httpie.sessions import DEFAULT_SESSIONS_DIR from httpie.ssl import DEFAULT_SSL_CIPHERS, AVAILABLE_SSL_VERSION_ARG_MAPPING diff --git a/httpie/client.py b/httpie/client.py index 8a6e6f05..71a95213 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -14,7 +14,7 @@ import urllib3 from httpie import __version__ from httpie.cli.dicts import RequestHeadersDict -from httpie.plugins import plugin_manager +from httpie.plugins.registry import plugin_manager from httpie.sessions import get_httpie_session from httpie.ssl import AVAILABLE_SSL_VERSION_ARG_MAPPING, HTTPieHTTPSAdapter from httpie.utils import repr_dict diff --git a/httpie/core.py b/httpie/core.py index 7c955e7e..4f85de75 100644 --- a/httpie/core.py +++ b/httpie/core.py @@ -13,10 +13,11 @@ from httpie.client import collect_messages from httpie.context import Environment from httpie.downloads import Downloader from httpie.output.writer import write_message, write_stream -from httpie.plugins import plugin_manager +from httpie.plugins.registry import plugin_manager from httpie.status import ExitStatus, http_status_to_exit_status +# noinspection PyDefaultArgument def main( args: List[Union[str, bytes]] = sys.argv, env=Environment(), diff --git a/httpie/output/processing.py b/httpie/output/processing.py index 9bee2621..263bbc0e 100644 --- a/httpie/output/processing.py +++ b/httpie/output/processing.py @@ -1,7 +1,8 @@ import re from typing import Optional, List -from httpie.plugins import plugin_manager, ConverterPlugin +from httpie.plugins import ConverterPlugin +from httpie.plugins.registry import plugin_manager from httpie.context import Environment diff --git a/httpie/plugins/__init__.py b/httpie/plugins/__init__.py index 38c2a66c..9386ca30 100644 --- a/httpie/plugins/__init__.py +++ b/httpie/plugins/__init__.py @@ -7,18 +7,3 @@ from httpie.plugins.base import ( AuthPlugin, FormatterPlugin, ConverterPlugin, TransportPlugin ) -from httpie.plugins.manager import PluginManager -from httpie.plugins.builtin import BasicAuthPlugin, DigestAuthPlugin -from httpie.output.formatters.headers import HeadersFormatter -from httpie.output.formatters.json import JSONFormatter -from httpie.output.formatters.colors import ColorFormatter - - -plugin_manager = PluginManager() -plugin_manager.register( - BasicAuthPlugin, - DigestAuthPlugin, - HeadersFormatter, - JSONFormatter, - ColorFormatter, -) diff --git a/httpie/plugins/registry.py b/httpie/plugins/registry.py new file mode 100644 index 00000000..aeb11280 --- /dev/null +++ b/httpie/plugins/registry.py @@ -0,0 +1,18 @@ +from httpie.plugins.manager import PluginManager +from httpie.plugins.builtin import BasicAuthPlugin, DigestAuthPlugin +from httpie.output.formatters.headers import HeadersFormatter +from httpie.output.formatters.json import JSONFormatter +from httpie.output.formatters.colors import ColorFormatter + + +plugin_manager = PluginManager() + + +# Register all built-in plugins. +plugin_manager.register( + BasicAuthPlugin, + DigestAuthPlugin, + HeadersFormatter, + JSONFormatter, + ColorFormatter, +) diff --git a/httpie/sessions.py b/httpie/sessions.py index 7ed7331e..948ce324 100644 --- a/httpie/sessions.py +++ b/httpie/sessions.py @@ -13,7 +13,7 @@ from requests.cookies import RequestsCookieJar, create_cookie from httpie.cli.dicts import RequestHeadersDict from httpie.config import BaseConfigDict, DEFAULT_CONFIG_DIR -from httpie.plugins import plugin_manager +from httpie.plugins.registry import plugin_manager SESSIONS_DIR_NAME = 'sessions' diff --git a/tests/test_auth_plugins.py b/tests/test_auth_plugins.py index 434d7024..b21d813e 100644 --- a/tests/test_auth_plugins.py +++ b/tests/test_auth_plugins.py @@ -1,9 +1,11 @@ from mock import mock from httpie.cli.constants import SEPARATOR_CREDENTIALS -from httpie.plugins import AuthPlugin, plugin_manager +from httpie.plugins import AuthPlugin +from httpie.plugins.registry import plugin_manager from utils import http, HTTP_OK + # TODO: run all these tests in session mode as well USERNAME = 'user'