From aadacfdbfdfd4ec86f1ae03868616abc86b8090c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 27 May 2017 16:44:04 +0530 Subject: [PATCH] Get console_print() to work --- src/calibre/srv/code.py | 11 ++++++++--- src/pyj/ajax.pyj | 15 ++++----------- src/pyj/session.pyj | 1 - src/pyj/srv.pyj | 8 +++++++- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/calibre/srv/code.py b/src/calibre/srv/code.py index c368ac399e..f536b0b3fb 100644 --- a/src/calibre/srv/code.py +++ b/src/calibre/srv/code.py @@ -10,7 +10,7 @@ from calibre import as_unicode from calibre.customize.ui import available_input_formats from calibre.db.view import sanitize_sort_field_name from calibre.srv.ajax import search_result -from calibre.srv.errors import HTTPNotFound, HTTPBadRequest, BookNotFound +from calibre.srv.errors import HTTPNotFound, HTTPBadRequest, BookNotFound, HTTPForbidden from calibre.srv.metadata import book_as_json, categories_as_json, icon_map, categories_settings from calibre.srv.routes import endpoint, json from calibre.srv.utils import get_library_data, get_use_roman @@ -44,10 +44,16 @@ def auto_reload(ctx, rd): return str(max(0, auto_reload_port)) +@endpoint('/allow-console-print', cache_control='no-cache', auth_required=False) +def allow_console_print(ctx, rd): + return 'y' if getattr(rd.opts, 'allow_console_print', False) else 'n' + + @endpoint('/console-print', methods=('POST', )) def console_print(ctx, rd): if not getattr(rd.opts, 'allow_console_print', False): - raise HTTPNotFound('console printing is not allowed') + raise HTTPForbidden('console printing is not allowed') + print(rd.remote_addr, end=' ') shutil.copyfileobj(rd.request_body_file, sys.stdout) return '' @@ -109,7 +115,6 @@ def basic_interface_data(ctx, rd): tweaks['gui_last_modified_display_format'], 'use_roman_numerals_for_series_number': get_use_roman(), 'translations': get_translations(), - 'allow_console_print': getattr(rd.opts, 'allow_console_print', False), 'icon_map': icon_map(), 'icon_path': ctx.url_for('/icon', which=''), } diff --git a/src/pyj/ajax.pyj b/src/pyj/ajax.pyj index c291253fd7..578d2318e2 100644 --- a/src/pyj/ajax.pyj +++ b/src/pyj/ajax.pyj @@ -103,18 +103,11 @@ def ajax_send(path, data, on_complete, on_progress=None, query=None, timeout=30* return xhr -allow_console_print = False - -def set_allow_console_print(val): - nonlocal allow_console_print - allow_console_print = bool(val) - def console_print(*args): - print(*args) - if allow_console_print: - data = ' '.join(map(str, args)) + '\n' - xhr = ajax('console-print', def():pass;, method='POST', progress_totals_needed=False) - xhr.send(data) + ρσ_print(*args) # noqa: undef + data = ' '.join(map(str, args)) + '\n' + xhr = ajax('console-print', def():pass;, method='POST', progress_totals_needed=False) + xhr.send(data) # TODO: Implement AJAX based switch user by: # 1) POST to a logout URL with an incorrect username and password diff --git a/src/pyj/session.pyj b/src/pyj/session.pyj index c3fae0bf6b..89c9d2bf10 100644 --- a/src/pyj/session.pyj +++ b/src/pyj/session.pyj @@ -161,7 +161,6 @@ default_interface_data = { 'gui_timestamp_display_format': 'dd MMM yyyy', 'gui_last_modified_display_format': 'dd MMM yyyy', 'use_roman_numerals_for_series_number': True, - 'allow_console_print':False, 'default_library_id': None, 'library_map': None, 'icon_map': {}, diff --git a/src/pyj/srv.pyj b/src/pyj/srv.pyj index c85ca6da44..531325c1c1 100644 --- a/src/pyj/srv.pyj +++ b/src/pyj/srv.pyj @@ -5,7 +5,7 @@ from __python__ import hash_literals from gettext import gettext as _ import initialize # noqa: unused-import -from ajax import ajax +from ajax import ajax, console_print from autoreload import create_auto_reload_watcher from book_list.globals import main_js from book_list.main import main @@ -45,3 +45,9 @@ else: autoreload_enabled = True create_auto_reload_watcher(port) ).send() # We must bypass cache as otherwise we could get stale port info + ajax('allow-console-print', def(end_type, xhr, event): + nonlocal print + if end_type is 'load': + if xhr.responseText == 'y': + print = console_print + ).send()