From 33fcb5716050bfaa54e44deff77ee6b9c8a9c408 Mon Sep 17 00:00:00 2001 From: Cimbali Date: Tue, 11 Jul 2017 12:21:07 +0200 Subject: [PATCH] Hide layout validation error if purposedly resetting layout config Lead to confusion on which error messages are relevant, see e.g. #32. --- pympress/ui.py | 11 +++++++---- pympress/util.py | 9 ++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pympress/ui.py b/pympress/ui.py index 6d04f7e3..71719b07 100644 --- a/pympress/ui.py +++ b/pympress/ui.py @@ -627,20 +627,23 @@ def rearrange_p_layout(self, layout): def make_pwin(self): """ Initializes the presenter window. """ + default_notes_layout = '{"resizeable":true, "orientation":"horizontal", "children":["notes", {"resizeable":false, "children":["current", "next"], "orientation":"vertical"}], "proportions": [0.60, 0.40]}' + default_plain_layout = '{"resizeable":true, "orientation":"horizontal", "children":["current", {"resizeable":true, "orientation":"vertical", "children":["next", "annotations"], "proportions":[0.55, 0.45]}], "proportions":[0.67, 0.33]}' + # Log error and keep default layout try: - self.notes_layout = pympress.util.layout_from_json(self.config.get('layout', 'notes')) + self.notes_layout = pympress.util.layout_from_json(self.config.get('layout', 'notes'), default_notes_layout) self.validate_layout(self.notes_layout, set(self.placeable_widgets.keys()) - {"annotations"}) except ValueError as e: logger.exception('Invalid layout') - self.notes_layout = pympress.util.layout_from_json('{"resizeable":true, "orientation":"horizontal", "children":["notes", {"resizeable":false, "children":["current", "next"], "orientation":"vertical"}], "proportions": [0.60, 0.40]}') + self.notes_layout = pympress.util.layout_from_json(default_notes_layout) try: - self.plain_layout = pympress.util.layout_from_json(self.config.get('layout', 'plain')) + self.plain_layout = pympress.util.layout_from_json(self.config.get('layout', 'plain'), default_plain_layout) self.validate_layout(self.plain_layout, set(self.placeable_widgets.keys()) - {"notes"}) except ValueError as e: logger.exception('Invalid layout') - self.plain_layout = pympress.util.layout_from_json('{"resizeable":true, "orientation":"horizontal", "children":["current", {"resizeable":true, "orientation":"vertical", "children":["next", "annotations"], "proportions":[0.55, 0.45]}], "proportions":[0.67, 0.33]}') + self.plain_layout = pympress.util.layout_from_json(default_plain_layout) self.rearrange_p_layout(self.notes_layout if self.notes_mode else self.plain_layout) diff --git a/pympress/util.py b/pympress/util.py index 2a265af0..8782c8db 100644 --- a/pympress/util.py +++ b/pympress/util.py @@ -200,10 +200,10 @@ def load_config(): config.set('presenter', 'show_annotations', 'off') if not config.has_option('layout', 'notes'): - config.set('layout', 'notes', '{}') + config.set('layout', 'notes', '') if not config.has_option('layout', 'plain'): - config.set('layout', 'plain', '{}') + config.set('layout', 'plain', '') if not config.has_option('scribble', 'color'): config.set('scribble', 'color', Gdk.RGBA(1., 0., 0., 1.).to_string()) @@ -228,11 +228,14 @@ def recursive_unicode_to_str(obj): return obj -def layout_from_json(layout_string): +def layout_from_json(layout_string, default_string): ''' Load the layout from config, with all strings cast to type 'str' (even on python2 where they default to 'unicode') Raises ValueError until python 3.4, json.decoder.JSONDecodeError afterwards, on invalid input. ''' + if not layout_string: + layout_string = default_string + layout = json.loads(layout_string) try: