Get console_print() to work

This commit is contained in:
Kovid Goyal 2017-05-27 16:44:04 +05:30
parent 00334e9f1b
commit aadacfdbfd
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 19 additions and 16 deletions

View File

@ -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=''),
}

View File

@ -103,15 +103,8 @@ 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:
ρσ_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)

View File

@ -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': {},

View File

@ -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()