mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Get console_print() to work
This commit is contained in:
parent
00334e9f1b
commit
aadacfdbfd
@ -10,7 +10,7 @@ from calibre import as_unicode
|
|||||||
from calibre.customize.ui import available_input_formats
|
from calibre.customize.ui import available_input_formats
|
||||||
from calibre.db.view import sanitize_sort_field_name
|
from calibre.db.view import sanitize_sort_field_name
|
||||||
from calibre.srv.ajax import search_result
|
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.metadata import book_as_json, categories_as_json, icon_map, categories_settings
|
||||||
from calibre.srv.routes import endpoint, json
|
from calibre.srv.routes import endpoint, json
|
||||||
from calibre.srv.utils import get_library_data, get_use_roman
|
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))
|
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', ))
|
@endpoint('/console-print', methods=('POST', ))
|
||||||
def console_print(ctx, rd):
|
def console_print(ctx, rd):
|
||||||
if not getattr(rd.opts, 'allow_console_print', False):
|
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)
|
shutil.copyfileobj(rd.request_body_file, sys.stdout)
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
@ -109,7 +115,6 @@ def basic_interface_data(ctx, rd):
|
|||||||
tweaks['gui_last_modified_display_format'],
|
tweaks['gui_last_modified_display_format'],
|
||||||
'use_roman_numerals_for_series_number': get_use_roman(),
|
'use_roman_numerals_for_series_number': get_use_roman(),
|
||||||
'translations': get_translations(),
|
'translations': get_translations(),
|
||||||
'allow_console_print': getattr(rd.opts, 'allow_console_print', False),
|
|
||||||
'icon_map': icon_map(),
|
'icon_map': icon_map(),
|
||||||
'icon_path': ctx.url_for('/icon', which=''),
|
'icon_path': ctx.url_for('/icon', which=''),
|
||||||
}
|
}
|
||||||
|
@ -103,18 +103,11 @@ def ajax_send(path, data, on_complete, on_progress=None, query=None, timeout=30*
|
|||||||
return xhr
|
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):
|
def console_print(*args):
|
||||||
print(*args)
|
ρσ_print(*args) # noqa: undef
|
||||||
if allow_console_print:
|
data = ' '.join(map(str, args)) + '\n'
|
||||||
data = ' '.join(map(str, args)) + '\n'
|
xhr = ajax('console-print', def():pass;, method='POST', progress_totals_needed=False)
|
||||||
xhr = ajax('console-print', def():pass;, method='POST', progress_totals_needed=False)
|
xhr.send(data)
|
||||||
xhr.send(data)
|
|
||||||
|
|
||||||
# TODO: Implement AJAX based switch user by:
|
# TODO: Implement AJAX based switch user by:
|
||||||
# 1) POST to a logout URL with an incorrect username and password
|
# 1) POST to a logout URL with an incorrect username and password
|
||||||
|
@ -161,7 +161,6 @@ default_interface_data = {
|
|||||||
'gui_timestamp_display_format': 'dd MMM yyyy',
|
'gui_timestamp_display_format': 'dd MMM yyyy',
|
||||||
'gui_last_modified_display_format': 'dd MMM yyyy',
|
'gui_last_modified_display_format': 'dd MMM yyyy',
|
||||||
'use_roman_numerals_for_series_number': True,
|
'use_roman_numerals_for_series_number': True,
|
||||||
'allow_console_print':False,
|
|
||||||
'default_library_id': None,
|
'default_library_id': None,
|
||||||
'library_map': None,
|
'library_map': None,
|
||||||
'icon_map': {},
|
'icon_map': {},
|
||||||
|
@ -5,7 +5,7 @@ from __python__ import hash_literals
|
|||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
|
|
||||||
import initialize # noqa: unused-import
|
import initialize # noqa: unused-import
|
||||||
from ajax import ajax
|
from ajax import ajax, console_print
|
||||||
from autoreload import create_auto_reload_watcher
|
from autoreload import create_auto_reload_watcher
|
||||||
from book_list.globals import main_js
|
from book_list.globals import main_js
|
||||||
from book_list.main import main
|
from book_list.main import main
|
||||||
@ -45,3 +45,9 @@ else:
|
|||||||
autoreload_enabled = True
|
autoreload_enabled = True
|
||||||
create_auto_reload_watcher(port)
|
create_auto_reload_watcher(port)
|
||||||
).send() # We must bypass cache as otherwise we could get stale port info
|
).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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user