From 58b35f1aab5c2f13de3fe0dbba281d92d05c523a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 21 Apr 2020 22:25:50 +0530 Subject: [PATCH] Have the develop mode env var work for the server as well --- src/calibre/gui2/viewer/web_view.py | 4 ++-- src/calibre/srv/code.py | 14 ++++++++++---- src/pyj/read_book/globals.pyj | 3 ++- src/pyj/read_book/shortcuts.pyj | 15 ++++++++------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/calibre/gui2/viewer/web_view.py b/src/calibre/gui2/viewer/web_view.py index 1951d53617..995e3a05cb 100644 --- a/src/calibre/gui2/viewer/web_view.py +++ b/src/calibre/gui2/viewer/web_view.py @@ -219,8 +219,6 @@ def create_profile(): # DO NOT change the user agent as it is used to workaround # Qt bugs see workaround_qt_bug() in ajax.pyj ua = 'calibre-viewer {} {}'.format(__version__, osname) - if hasenv('CALIBRE_ENABLE_DEVELOP_MODE'): - ua += ' CALIBRE_ENABLE_DEVELOP_MODE' ans.setHttpUserAgent(ua) if is_running_from_develop: from calibre.utils.rapydscript import compile_viewer @@ -229,6 +227,8 @@ def create_profile(): js = P('viewer.js', data=True, allow_user_override=False) translations_json = get_translations_data() or b'null' js = js.replace(b'__TRANSLATIONS_DATA__', translations_json, 1) + if hasenv('CALIBRE_ENABLE_DEVELOP_MODE'): + js = js.replace(b'__IN_DEVELOP_MODE__', os.environ['CALIBRE_ENABLE_DEVELOP_MODE'].encode('ascii')) insert_scripts(ans, create_script('viewer.js', js)) url_handler = UrlSchemeHandler(ans) ans.installUrlSchemeHandler(QByteArray(FAKE_PROTOCOL.encode('ascii')), url_handler) diff --git a/src/calibre/srv/code.py b/src/calibre/srv/code.py index 9c7b2dc487..62795887a7 100644 --- a/src/calibre/srv/code.py +++ b/src/calibre/srv/code.py @@ -5,6 +5,7 @@ from __future__ import absolute_import, division, print_function, unicode_literals import hashlib +import os import random import shutil import sys @@ -25,18 +26,23 @@ from calibre.srv.metadata import ( from calibre.srv.routes import endpoint, json from calibre.srv.utils import get_library_data, get_use_roman from calibre.utils.config import prefs, tweaks -from calibre.utils.icu import sort_key, numeric_sort_key -from calibre.utils.localization import get_lang, lang_map_for_ui, localize_website_link +from calibre.utils.icu import numeric_sort_key, sort_key +from calibre.utils.localization import ( + get_lang, lang_map_for_ui, localize_website_link +) from calibre.utils.search_query_parser import ParseException from calibre.utils.serialize import json_dumps -from polyglot.builtins import iteritems, itervalues +from polyglot.builtins import iteritems, itervalues, hasenv POSTABLE = frozenset({'GET', 'POST', 'HEAD'}) @endpoint('', auth_required=False) def index(ctx, rd): - return lopen(P('content-server/index-generated.html'), 'rb') + ans_file = lopen(P('content-server/index-generated.html'), 'rb') + if not hasenv('CALIBRE_ENABLE_DEVELOP_MODE'): + return ans_file + return ans_file.read().replace(b'__IN_DEVELOP_MODE__', os.environ['CALIBRE_ENABLE_DEVELOP_MODE'].encode('ascii')) @endpoint('/robots.txt', auth_required=False) diff --git a/src/pyj/read_book/globals.pyj b/src/pyj/read_book/globals.pyj index b46dac889e..3b5175ee56 100644 --- a/src/pyj/read_book/globals.pyj +++ b/src/pyj/read_book/globals.pyj @@ -76,10 +76,11 @@ register_callback(def(): set_system_colors({'background': '#111', 'foreground': '#ddd', 'link': dark_link_color}) ) +IN_DEVELOP_MODE = '__IN_DEVELOP_MODE__' runtime = { 'is_standalone_viewer': False, 'viewer_in_full_screen': False, - 'in_develop_mode': window.navigator.userAgent.indexOf('CALIBRE_ENABLE_DEVELOP_MODE') > 0, + 'in_develop_mode': IN_DEVELOP_MODE is '1', } diff --git a/src/pyj/read_book/shortcuts.pyj b/src/pyj/read_book/shortcuts.pyj index e54d0cb5a5..ad4db1d50f 100644 --- a/src/pyj/read_book/shortcuts.pyj +++ b/src/pyj/read_book/shortcuts.pyj @@ -331,6 +331,14 @@ def shortcuts_definition(): ), } + if runtime.in_develop_mode: + ans.create_annotation = desc( + "a", + 'ui', + _('Create a highlight'), + ) + + return ans @@ -385,13 +393,6 @@ def add_standalone_viewer_shortcuts(): _('Toggle the toolbar'), ) - if runtime.in_develop_mode: - sc['create_annotation'] = desc( - "a", - 'ui', - _('Create a highlight'), - ) - def create_shortcut_map(custom_shortcuts): ans = {}