From 9e8cf4b2448d911a885ea85d49266798869eaf81 Mon Sep 17 00:00:00 2001 From: Dongkuo Ma Date: Fri, 25 Oct 2019 16:04:21 +0800 Subject: [PATCH] only show request log on debug mode (#3) --- src/varanus/__init__.py | 1 - src/varanus/__main__.py | 6 +++++- src/varanus/__patch__.py | 36 +++++++++++++++++++++--------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/varanus/__init__.py b/src/varanus/__init__.py index 316dd9a..078d071 100644 --- a/src/varanus/__init__.py +++ b/src/varanus/__init__.py @@ -9,7 +9,6 @@ [tool.poetry.plugins] """ from .__version__ import __version__ # noqa app version -from .__patch__ import __patches__ # noqa monkeypatches from .__main__ import main as app # noqa create cli app from .utils import see # noqa debugging from .api import * # noqa python interface diff --git a/src/varanus/__main__.py b/src/varanus/__main__.py index ddbdf34..9b7d203 100644 --- a/src/varanus/__main__.py +++ b/src/varanus/__main__.py @@ -9,6 +9,7 @@ from .__version__ import __version__ from .session import Session +from .__patch__ import apply_patch CLINS = 'varanus.clier' @@ -46,10 +47,13 @@ def clean_up(self, cmd, result, err): def main(): """ Execute the application """ - VaranusApp().run(sys.argv[1:]) + app = VaranusApp() + apply_patch(app) + app.run(sys.argv[1:]) # Make the script runnable if __name__ == "__main__": raise SystemExit(main()) + diff --git a/src/varanus/__patch__.py b/src/varanus/__patch__.py index 966f79d..0374ef2 100644 --- a/src/varanus/__patch__.py +++ b/src/varanus/__patch__.py @@ -10,6 +10,8 @@ .. todo:: Inject code instead of replacing routines. """ import logging +from functools import partialmethod + import requests import scrapinghub.legacy import scrapinghub.client @@ -25,7 +27,7 @@ } -def HubstorageClient_request(self, is_idempotent=False, **kwargs): +def HubstorageClient_request(self, is_idempotent=False, app=None, **kwargs): """Execute an HTTP request with the current client session Use the retry policy configured in the client when is_idempotent is True. @@ -34,11 +36,12 @@ def HubstorageClient_request(self, is_idempotent=False, **kwargs): def invoke_request(): r = self.session.request(**kwargs) - log_response( - r, - source=f'{__name__}:scrapinghub.client.HubstorageClient.request', - color='blue', - ) + if app.options.debug: + log_response( + r, + source=f'{__name__}:scrapinghub.client.HubstorageClient.request', + color='blue', + ) try: r.raise_for_status() return r @@ -52,7 +55,7 @@ def invoke_request(): return invoke_request() -def Connection_request(self, url, data, headers, format, raw, files=None): +def Connection_request(self, url, data, headers, format, raw, files=None, app=None): """Performs the request using and returns the content deserialized, based on given `format` @@ -74,13 +77,16 @@ def Connection_request(self, url, data, headers, format, raw, files=None): response = self._session.post(url, headers=headers, data=data, files=files, timeout=self._connection_timeout) - log_response( - response, - source=f'{__name__}:scrapinghub.legacy.Connection._request', - color='cyan', - ) - return self._decode_response(response, format, raw) + if app.options.debug: + log_response( + response, + source=f'{__name__}:scrapinghub.legacy.Connection._request', + color='cyan', + ) + return self._decode_response(response, format, raw) + +def apply_patch(app): + scrapinghub.client.HubstorageClient.request = partialmethod(HubstorageClient_request, app=app) + scrapinghub.legacy.Connection._request = partialmethod(Connection_request, app=app) -scrapinghub.client.HubstorageClient.request = HubstorageClient_request -scrapinghub.legacy.Connection._request = Connection_request