[WebUI] Handle missing script files and fallback to available files

* To help user's encountering a blank web page, log warnings if script
files for a selected mode are missing and attempt to fallback to a working mode.
 * There is no logging for dev version detection to prevent spamming output.
 * Add slimit dependency to tox
This commit is contained in:
bendikro 2016-05-18 17:57:09 +02:00 committed by Calum Lind
commit 6ce9f77e17
2 changed files with 44 additions and 37 deletions

View file

@ -243,20 +243,21 @@ class ScriptResource(resource.Resource, component.Component):
def __init__(self): def __init__(self):
resource.Resource.__init__(self) resource.Resource.__init__(self)
component.Component.__init__(self, "Scripts") component.Component.__init__(self, "Scripts")
self.__scripts = { self.__scripts = {}
"normal": { for script_type in ["normal", "debug", "dev"]:
"scripts": {}, self.__scripts[script_type] = {"scripts": {}, "order": [], "files_exist": True}
"order": []
}, def has_script_type_files(self, script_type):
"debug": { """Returns whether all the script files exist for this script type.
"scripts": {},
"order": [] Args:
}, script_type (str): The script type to check (normal, debug, dev).
"dev": {
"scripts": {}, Returns:
"order": [] bool: True if the files for this script type exist, otherwise False.
}
} """
return self.__scripts[script_type]["files_exist"]
def add_script(self, path, filepath, script_type=None): def add_script(self, path, filepath, script_type=None):
""" """
@ -274,6 +275,8 @@ class ScriptResource(resource.Resource, component.Component):
self.__scripts[script_type]["scripts"][path] = filepath self.__scripts[script_type]["scripts"][path] = filepath
self.__scripts[script_type]["order"].append(path) self.__scripts[script_type]["order"].append(path)
if not os.path.isfile(filepath):
self.__scripts[script_type]["files_exist"] = False
def add_script_folder(self, path, filepath, script_type=None, recurse=True): def add_script_folder(self, path, filepath, script_type=None, recurse=True):
""" """
@ -293,6 +296,8 @@ class ScriptResource(resource.Resource, component.Component):
self.__scripts[script_type]["scripts"][path] = (filepath, recurse) self.__scripts[script_type]["scripts"][path] = (filepath, recurse)
self.__scripts[script_type]["order"].append(path) self.__scripts[script_type]["order"].append(path)
if not os.path.isdir(filepath):
self.__scripts[script_type]["files_exist"] = False
def remove_script(self, path, script_type=None): def remove_script(self, path, script_type=None):
""" """
@ -428,8 +433,8 @@ class TopLevel(resource.Resource):
js.add_script("ext-extensions.js", rpath("js", "extjs", "ext-extensions.js")) js.add_script("ext-extensions.js", rpath("js", "extjs", "ext-extensions.js"))
js.add_script("deluge-all.js", rpath("js", "deluge-all.js")) js.add_script("deluge-all.js", rpath("js", "deluge-all.js"))
self.js = js
self.putChild("js", js) self.putChild("js", js)
self.putChild("json", JSON()) self.putChild("json", JSON())
self.putChild("upload", Upload()) self.putChild("upload", Upload())
self.putChild("render", Render()) self.putChild("render", Render())
@ -493,30 +498,30 @@ class TopLevel(resource.Resource):
return resource.Resource.getChildWithDefault(self, path, request) return resource.Resource.getChildWithDefault(self, path, request)
def render(self, request): def render(self, request):
debug = False uri_true = ("true", "yes", "1")
if 'debug' in request.args: debug_arg = request.args.get("debug", [""])[-1] in uri_true
debug_arg = request.args.get('debug')[-1] dev_arg = request.args.get("dev", [""])[-1] in uri_true
if debug_arg in ('true', 'yes', '1'): dev_ver = "dev" in common.get_version()
debug = True
else:
debug = False
dev = 'dev' in common.get_version() script_type = "normal"
if 'dev' in request.args: if debug_arg:
dev_arg = request.args.get('dev')[-1] script_type = "debug"
if dev_arg in ('true', 'yes' '1'): # Override debug if dev arg or version.
dev = True if dev_arg or dev_ver:
else: script_type = "dev"
dev = False
if dev: if not self.js.has_script_type_files(script_type):
mode = 'dev' if not dev_ver:
elif debug: log.warning("Failed to enable WebUI '%s' mode, script files are missing!", script_type)
mode = 'debug' # Fallback to checking other types in order and selecting first with files available.
else: for alt_script_type in [x for x in ["normal", "debug", "dev"] if x != script_type]:
mode = None if self.js.has_script_type_files(alt_script_type):
script_type = alt_script_type
if not dev_ver:
log.warning("WebUI falling back to '%s' mode.", script_type)
break
scripts = component.get("Scripts").get_scripts(mode) scripts = component.get("Scripts").get_scripts(script_type)
scripts.insert(0, "gettext.js") scripts.insert(0, "gettext.js")
template = Template(filename=rpath("index.html")) template = Template(filename=rpath("index.html"))
@ -526,8 +531,9 @@ class TopLevel(resource.Resource):
web_config["base"] = request.base web_config["base"] = request.base
config = dict([(key, web_config[key]) for key in UI_CONFIG_KEYS]) config = dict([(key, web_config[key]) for key in UI_CONFIG_KEYS])
js_config = json.dumps(config) js_config = json.dumps(config)
# Insert the values into 'index.html' and return.
return template.render(scripts=scripts, stylesheets=self.stylesheets, return template.render(scripts=scripts, stylesheets=self.stylesheets,
debug=debug, base=request.base, js_config=js_config) debug=debug_arg, base=request.base, js_config=js_config)
class DelugeWeb(component.Component): class DelugeWeb(component.Component):

View file

@ -104,6 +104,7 @@ deps =
{[testenv]deps} {[testenv]deps}
flake8 flake8
pep8-naming pep8-naming
slimit
commands = commands =
flake8 --version flake8 --version
flake8 flake8